public AgentsViewModel(IAgentRepository agentRepo, IViewModelFactory vmFactory, IUserInteraction ui) { _agentRepo = agentRepo; _vmFactory = vmFactory; var agentVms = agentRepo.GetAll().Select(x => CreateAgentViewModel(x)); Agents.ReplaceItems(agentVms); AddAgentCommand = new ActionCommand(o => { Agents.Add(CreateAgentViewModel(null)); IsDirty = true; }); DeleteAgentCommand = new ActionCommand(_ => { var selectedAgent = Agents.FirstOrDefault(x => x.IsSelected); if (selectedAgent == null) { ui.ShowWarning("No agent selected."); return; } if (!ui.AskForConfirmation($"Are you sure you want to delete the selected '{selectedAgent.Id}' agent?", "Delete agent")) { return; } Agents.Remove(selectedAgent); if (!string.IsNullOrEmpty(selectedAgent.Id)) { agentRepo.Delete(selectedAgent.Id); } }); CommitAgentsCommand = new ActionCommand(_ => CommitAgents(), _ => IsDirty && !HasErrors); ResetChangesCommand = new ActionCommand(_ => { foreach (var agent in Agents) { agent.ResetForm(); } IsDirty = false; }, _ => IsDirty); PropertiesAreInitialized = true; }
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; }
public TrackerProductViewModel(Product product, IEventBus eventBus, IAgentRepository agentRepo, IProductPriceManager productMng, IProductRepository productRepo, IProductStatusProvider statusProvider, IUserInteraction ui) { _agentRepo = agentRepo; _productMng = productMng; _productRepo = productRepo; _statusProvider = statusProvider; _product = product; _ui = ui; RefreshAgents(); RefreshCategories(); if (_product != null) { ResetForm(); Reconcile(false); } CommitProductCommand = new ActionCommand(_ => CommitProduct()); ShowInBrowserCommand = new ActionCommand(_ => ui.ShowProductInBrowser(_product.Lowest?.ProductSource)); AddSourceCommand = new ActionCommand(_ => Sources.Add(CreateSourceViewModel(null))); ResetCommand = new ActionCommand(_ => { ResetForm(); }, _ => _product != null); DropPricesCommand = new ActionCommand(_ => { if (!_ui.AskForConfirmation("Are you sure?", "Prices drop confirmation")) { return; } _productRepo.DropPrices(_product); Reconcile(true); }); RefreshPriceCommand = new ActionCommand(_ => { if (Status == ProductScanStatus.Scanning) { return; } _productMng.EnqueueScan(product.Id); }, _ => Status != ProductScanStatus.Scanning); eventBus.WhenFired <AgentsUpdatedEvent, AgentDeletedEvent>() .ObserveOnDispatcher() .Subscribe(_ => RefreshAgents() ); eventBus.WhenFired <ProductUpdatedEvent, ProductAddedEvent>() .ObserveOnDispatcher() .Subscribe(_ => RefreshCategories() ); PropertiesAreInitialized = true; }