Skip to content

Commit

Permalink
Merge pull request #2 from Vivek-abstract/alert-when-below-target
Browse files Browse the repository at this point in the history
Add alert when price of any product reaches below target
  • Loading branch information
Vivek-abstract authored Oct 20, 2021
2 parents fbe4158 + f347206 commit c6ad42f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions PCPartPriceTracker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
12 changes: 12 additions & 0 deletions ViewModels/Helpers/PriceReachedTargetEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ViewModels.Helpers
{
public class PriceReachedTargetEventArgs
{
public string Products { get; set; }

public PriceReachedTargetEventArgs(string products)
{
Products = products;
}
}
}
15 changes: 15 additions & 0 deletions ViewModels/MainVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using ViewModels.Commands;
using ViewModels.Factories;
using ViewModels.Helpers;

namespace ViewModels
{
Expand All @@ -21,6 +22,7 @@ public class MainVM : INotifyPropertyChanged

private bool _loading;


public bool Loading
{
get { return _loading; }
Expand All @@ -31,6 +33,8 @@ public bool Loading
}
}

public event EventHandler<PriceReachedTargetEventArgs> OnProductPriceReachedBelowTarget;

public event PropertyChangedEventHandler PropertyChanged;

private ProductContext _context = new ProductContext();
Expand Down Expand Up @@ -66,6 +70,7 @@ public async Task RefreshPricesAsync()
Loading = true;
var products = await _context.Products.ToListAsync();
var tasks = new List<Task<Product>>();
var productsBelowTarget = new List<Product>();

foreach (Product product in products)
{
Expand All @@ -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();
Expand All @@ -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))));
}
}
}
}

0 comments on commit c6ad42f

Please sign in to comment.