summaryrefslogtreecommitdiff
path: root/product/desktop.ui/presenters/StockWatchPresenter.cs
blob: b6da4ecb86feeba93a1867e1723597f0a7395008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using gorilla.infrastructure.threading;
using gorilla.utility;
using solidware.financials.infrastructure;
using solidware.financials.infrastructure.eventing;
using solidware.financials.messages;
using solidware.financials.windows.ui.views.dialogs;

namespace solidware.financials.windows.ui.presenters
{
    public class StockWatchPresenter : Presenter, TimerClient, EventSubscriber<CurrentStockPrice>, EventSubscriber<StartWatchingSymbol>
    {
        UICommandBuilder builder;
        Timer timer;

        public StockWatchPresenter(UICommandBuilder builder, Timer timer)
        {
            Stocks = new ObservableCollection<StockViewModel>();
            this.builder = builder;
            this.timer = timer;
        }

        public virtual ICollection<StockViewModel> Stocks { get; set; }
        public ObservableCommand AddSymbol { get; set; }
        public ObservableCommand Refresh { get; set; }

        public void present()
        {
            timer.start_notifying(this, new TimeSpan(0, 1, 0));
            AddSymbol = builder.build<AddSymbolCommand>(this);
            Refresh = builder.build<RefreshStockPricesCommand>(this);
        }

        public void notify()
        {
            Refresh.Execute(this);
        }

        public void notify(CurrentStockPrice message)
        {
            Stocks.Single(x => x.IsFor(message.Symbol)).ChangePriceTo(message.Price);
        }

        public void notify(StartWatchingSymbol message)
        {
            Stocks.Add(new StockViewModel(symbol: message.Symbol)); 
        }

        public class AddSymbolCommand : UICommand<StockWatchPresenter>
        {
            DialogLauncher launcher;

            public AddSymbolCommand(DialogLauncher launcher)
            {
                this.launcher = launcher;
            }

            public override void run(StockWatchPresenter presenter)
            {
                launcher.launch<AddNewStockSymbolPresenter, AddNewStockSymbolDialog>();
            }
        }

        public class RefreshStockPricesCommand : UICommand<StockWatchPresenter>
        {
            ServiceBus bus;

            public RefreshStockPricesCommand(ServiceBus bus)
            {
                this.bus = bus;
            }

            public override void run(StockWatchPresenter presenter)
            {
                presenter.Stocks.each(x => bus.publish(new StockPriceRequestQuery {Symbol = x.Symbol}));
            }
        }
    }
}