public MainViewModel( TrackerViewModel tracker, AgentsViewModel agents, SettingsViewModel settings, LogsViewModel logs, ITrackerScanContext scanContext, INotifyIconViewModel notifyViewModel) { Tabs = new() { tracker, agents, settings, logs }; scanContext.ScanProgress.Subscribe(args => { if (args.Status == Helpers.TrackerScanStatus.InProgress) { ProgressState = TaskbarItemProgressState.Normal; ProgressValue = args.Progress; } else if (args.Status == Helpers.TrackerScanStatus.InProgressWithErrors) { ProgressState = TaskbarItemProgressState.Paused; ProgressValue = args.Progress; } else if (args.Status == Helpers.TrackerScanStatus.Finished) { var message = scanContext.HasNewLowestPrice ? "Prices for some products have become even lower! Check it out." : "Nothing interesting has been caught."; if (scanContext.HasErrors) { message += Environment.NewLine + "NOTE: Some products could not finish scanning properly. Check the logs for details."; } notifyViewModel.ShowBalloonTip("Scan finished", message, scanContext.HasErrors ? BalloonIcon.Warning : BalloonIcon.Info); ProgressState = TaskbarItemProgressState.None; ProgressValue = 0; } else { ProgressState = TaskbarItemProgressState.None; ProgressValue = 0; } }); }
public TrackerViewModel(IEventBus eventBus, IProductRepository productRepo, IViewModelFactory vmFactory, IUserInteraction ui, ITrackerScanContext scanContext) { _eventBus = eventBus; _productRepo = productRepo; _vmFactory = vmFactory; _scanContext = scanContext; RefreshAllCommand = new ActionCommand(_ => { IsAddEditProductVisible = false; EnqueueScan(Products); }); RefreshSelectedCommand = new ActionCommand(_ => { IsAddEditProductVisible = false; EnqueueScan(Products.Where(x => x.IsSelected).ToArray()); }); OpenAddProductFlyoutCommand = new ActionCommand(o => { IsAddEditProductVisible = !IsAddEditProductVisible; if (IsAddEditProductVisible) { EditingProduct = vmFactory.CreateTrackerProduct(null); EditingProduct.CommitProductCommand.Executed .Take(1) .Subscribe(_ => { IsAddEditProductVisible = false; ReloadList(); }); } }); OpenEditProductFlyoutCommand = new ActionCommand(o => { EditingProduct = Products.FirstOrDefault(x => x.IsSelected); EditingProduct.CommitProductCommand.Executed .Take(1) .Subscribe(_ => { IsAddEditProductVisible = false; }); IsAddEditProductVisible = EditingProduct != null; }); DeleteProductCommand = new ActionCommand(_ => { IsAddEditProductVisible = false; var selectedProduct = Products.FirstOrDefault(x => x.IsSelected); if (selectedProduct == null) { ui.ShowWarning("No product selected."); return; } if (!ui.AskForConfirmation($"Are you sure you want to delete the selected '{selectedProduct.Name}' product?", "Delete product")) { return; } Products.Remove(selectedProduct); _productRepo.Delete(selectedProduct.Id); }); _eventBus.WhenFired <ProductScanStartedEvent>() .ObserveOnDispatcher() .Subscribe(ev => Products.First(x => x.Id == ev.Product.Id).Status = Core.Models.ProductScanStatus.Scanning ); _eventBus.WhenFired <ProductScannedEvent>() .Subscribe(ev => { Products.First(x => x.Id == ev.Product.Id).Reconcile(ev.LowestPriceUpdated); }); _eventBus.WhenFired <ProductScanFailedEvent>() .Subscribe(ev => { Products.First(x => x.Id == ev.Product.Id).SetFailed(ev.ErrorMessage); }); Deactivated.Executed.Subscribe(_ => IsAddEditProductVisible = false); RefreshOptions = new List <DropDownMenuItem> { new DropDownMenuItem("Refresh all", RefreshAllCommand), new DropDownMenuItem("Refresh selected", RefreshSelectedCommand), }; ReloadList(); PropertiesAreInitialized = true; }