main
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.Linq;
5using gorilla.infrastructure.threading;
6using gorilla.utility;
7using solidware.financials.infrastructure;
8using solidware.financials.infrastructure.eventing;
9using solidware.financials.messages;
10using solidware.financials.windows.ui.views.dialogs;
11
12namespace solidware.financials.windows.ui.presenters
13{
14 public class StockWatchPresenter : Presenter, TimerClient, EventSubscriber<CurrentStockPrice>, EventSubscriber<StartWatchingSymbol>
15 {
16 UICommandBuilder builder;
17 Timer timer;
18
19 public StockWatchPresenter(UICommandBuilder builder, Timer timer)
20 {
21 Stocks = new ObservableCollection<StockViewModel>();
22 this.builder = builder;
23 this.timer = timer;
24 }
25
26 public virtual ICollection<StockViewModel> Stocks { get; set; }
27 public ObservableCommand AddSymbol { get; set; }
28 public ObservableCommand Refresh { get; set; }
29
30 public void present()
31 {
32 timer.start_notifying(this, new TimeSpan(0, 0, 20));
33 AddSymbol = builder.build<AddSymbolCommand>(this);
34 Refresh = builder.build<RefreshStockPricesCommand>(this);
35 }
36
37 public void notify()
38 {
39 UIThread.Run(() =>
40 {
41 Refresh.Execute(this);
42 });
43 }
44
45 public void notify(CurrentStockPrice message)
46 {
47 Stocks.Single(x => x.is_for(message.Symbol)).change_price_to(message.Price);
48 }
49
50 public void notify(StartWatchingSymbol message)
51 {
52 var presenter = new StockViewModel(symbol: message.Symbol, builder: builder);
53 presenter.present();
54 Stocks.Add(presenter);
55 }
56
57 public class AddSymbolCommand : UICommand<StockWatchPresenter>
58 {
59 DialogLauncher launcher;
60
61 public AddSymbolCommand(DialogLauncher launcher)
62 {
63 this.launcher = launcher;
64 }
65
66 public override void run(StockWatchPresenter presenter)
67 {
68 launcher.launch<AddNewStockSymbolPresenter, AddNewStockSymbolDialog>();
69 }
70 }
71
72 public class RefreshStockPricesCommand : UICommand<StockWatchPresenter>
73 {
74 ServiceBus bus;
75
76 public RefreshStockPricesCommand(ServiceBus bus)
77 {
78 this.bus = bus;
79 }
80
81 public override void run(StockWatchPresenter presenter)
82 {
83 presenter.Stocks.each(x => bus.publish(new StockPriceRequestQuery {Symbol = x.Symbol}));
84 }
85 }
86 }
87}