From f3472063523fb0d98c86bc677ef104aefe1dc9ef Mon Sep 17 00:00:00 2001 From: Vivek Gawande Date: Wed, 20 Oct 2021 13:15:57 +0530 Subject: [PATCH] Add alert when price of any product reaches below target --- PCPartPriceTracker/MainWindow.xaml.cs | 8 ++++++++ ViewModels/Helpers/PriceReachedTargetEventArgs.cs | 12 ++++++++++++ ViewModels/MainVM.cs | 15 +++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 ViewModels/Helpers/PriceReachedTargetEventArgs.cs diff --git a/PCPartPriceTracker/MainWindow.xaml.cs b/PCPartPriceTracker/MainWindow.xaml.cs index 5277be2..486eaf4 100644 --- a/PCPartPriceTracker/MainWindow.xaml.cs +++ b/PCPartPriceTracker/MainWindow.xaml.cs @@ -37,8 +37,16 @@ public MainWindow() { InitializeComponent(); VM = (MainVM)FindResource("vm"); + VM.OnProductPriceReachedBelowTarget += VM_OnProductPriceReachedBelowTarget; SetupAutoRefresh(); } + + private void VM_OnProductPriceReachedBelowTarget(object sender, ViewModels.Helpers.PriceReachedTargetEventArgs e) + { + MessageBox.Show("The following products have reached below your target price and are in stock: " + e.Products, "Target Price Reached!", + MessageBoxButton.OK, MessageBoxImage.Information); + } + private void SetupAutoRefresh() { refreshTimer = new Timer(e => diff --git a/ViewModels/Helpers/PriceReachedTargetEventArgs.cs b/ViewModels/Helpers/PriceReachedTargetEventArgs.cs new file mode 100644 index 0000000..794b4fd --- /dev/null +++ b/ViewModels/Helpers/PriceReachedTargetEventArgs.cs @@ -0,0 +1,12 @@ +namespace ViewModels.Helpers +{ + public class PriceReachedTargetEventArgs + { + public string Products { get; set; } + + public PriceReachedTargetEventArgs(string products) + { + Products = products; + } + } +} \ No newline at end of file diff --git a/ViewModels/MainVM.cs b/ViewModels/MainVM.cs index 9de305b..eb5eaaf 100644 --- a/ViewModels/MainVM.cs +++ b/ViewModels/MainVM.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using ViewModels.Commands; using ViewModels.Factories; +using ViewModels.Helpers; namespace ViewModels { @@ -21,6 +22,7 @@ public class MainVM : INotifyPropertyChanged private bool _loading; + public bool Loading { get { return _loading; } @@ -31,6 +33,8 @@ public bool Loading } } + public event EventHandler OnProductPriceReachedBelowTarget; + public event PropertyChangedEventHandler PropertyChanged; private ProductContext _context = new ProductContext(); @@ -66,6 +70,7 @@ public async Task RefreshPricesAsync() Loading = true; var products = await _context.Products.ToListAsync(); var tasks = new List>(); + var productsBelowTarget = new List(); foreach (Product product in products) { @@ -79,7 +84,12 @@ public async Task RefreshPricesAsync() var updatedProduct = updatedProducts.Where(p => p.Id == product.Id).SingleOrDefault(); product.Price = updatedProduct.Price; product.InStock = updatedProduct.InStock; + if (product.Price <= product.TargetPrice && product.InStock) + { + productsBelowTarget.Add(product); + } } + await _context.SaveChangesAsync(); Products.Clear(); @@ -88,6 +98,11 @@ public async Task RefreshPricesAsync() Products.Add(product); } Loading = false; + + if (productsBelowTarget.Count > 0) + { + OnProductPriceReachedBelowTarget.Invoke(this, new PriceReachedTargetEventArgs(string.Join(",", productsBelowTarget.Select(p => p.Name)))); + } } } }