summaryrefslogtreecommitdiff
path: root/product/desktop.ui/presenters/AddNewStockSymbolPresenter.cs
blob: 098f49fac585106077904f11643d94ad55143ef8 (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
using System;
using gorilla.utility;
using solidware.financials.infrastructure;
using solidware.financials.messages;
using solidware.financials.windows.ui.presenters.validation;
using solidware.financials.windows.ui.views.controls;
using utility;

namespace solidware.financials.windows.ui.presenters
{
    public class AddNewStockSymbolPresenter : DialogPresenter
    {
        public AddNewStockSymbolPresenter(UICommandBuilder builder)
        {
            this.builder = builder;
            Symbol = new ObservableProperty<string>();
        }

        public ObservableCommand Add { get; set; }
        public ObservableCommand Cancel { get; set; }
        public virtual Observable<string> Symbol { get; set; }
        public virtual Action close { get; set; }

        public void present()
        {
            Add = builder.build<AddCommand, IsValid>(this);
            Cancel = builder.build<CancelCommand>(this);

            Symbol.Notify(Add);
            Symbol.Register<Error>(x => x.is_blank(), () => "Please specify a symbol.");
        }

        UICommandBuilder builder;

        public class AddCommand : UICommand<AddNewStockSymbolPresenter>
        {
            ServiceBus bus;

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

            public override void run(AddNewStockSymbolPresenter presenter)
            {
                bus.publish(new StartWatchingSymbol {Symbol = presenter.Symbol.Value.ToUpperInvariant()});
                presenter.close();
            }
        }

        public class IsValid : UISpecification<AddNewStockSymbolPresenter>
        {
            public override bool is_satisfied_by(AddNewStockSymbolPresenter presenter)
            {
                return presenter.Symbol.not(x => x.Value.is_blank());
            }
        }
    }
}