Mercurial > silverbladetech
annotate MetroWpf/Stocks.UI/ViewModels/StocksViewModel.cs @ 65:5d0c5cc83a7a
Click event handler added
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sun, 22 Apr 2012 14:21:33 +0100 |
parents | 4b8b38d17d24 |
children |
rev | line source |
---|---|
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
1 using System; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
2 using System.ComponentModel; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
3 using System.Linq; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
4 using System.Reactive; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
5 using System.Reactive.Linq; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
6 using System.Windows.Input; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
7 using GalaSoft.MvvmLight; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
8 using GalaSoft.MvvmLight.Command; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
9 using Stocks.Common; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
10 using Stocks.Common.Events; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
11 using Stocks.UI.Models; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
12 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
13 namespace Stocks.UI.ViewModels |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
14 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
15 public class StocksViewModel : ViewModelBase |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
16 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
17 private readonly IStocksService _service; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
18 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
19 public BindingList<DisplayStockPrice> DisplayStockPrices { get; set; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
20 public ICommand ServiceCommand { get; set; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
21 public ICommand SubscriptionCommand { get; set; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
22 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
23 public StocksViewModel(IStocksService service) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
24 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
25 _service = service; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
26 GetLatestPrices(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
27 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
28 SubscriptionCommand = new RelayCommand(SubscriptionCommandExecute); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
29 ServiceCommand = new RelayCommand(ServiceRunningCommandExecute); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
30 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
31 var priceUpdates = Observable.FromEventPattern<PriceChangedEventArgs>( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
32 _service, |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
33 "PriceChanged"); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
34 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
35 priceUpdates.Where(e => Subscribed) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
36 //.Throttle(TimeSpan.FromSeconds(1)) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
37 .Subscribe(PriceChanged); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
38 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
39 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
40 public void PriceChanged(EventPattern<PriceChangedEventArgs> e) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
41 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
42 var displayRate = DisplayStockPrices.First( |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
43 rate => rate.Symbol == e.EventArgs.Price.Symbol); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
44 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
45 if (displayRate != null) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
46 displayRate.Update(e.EventArgs.Price); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
47 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
48 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
49 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
50 private void GetLatestPrices() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
51 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
52 DisplayStockPrices = new BindingList<DisplayStockPrice>(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
53 var currentRates = _service.GetFullCurrentPrices(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
54 foreach (var latestRate in currentRates) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
55 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
56 var displayRate = DisplayStockPrice.Create(latestRate); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
57 DisplayStockPrices.Add(displayRate); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
58 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
59 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
60 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
61 private const string SubscribedPropertyName = "Subscribed"; |
39 | 62 private bool _subscribed; |
23
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
63 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
64 public bool Subscribed |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
65 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
66 get { return _subscribed; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
67 set |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
68 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
69 if (_subscribed == value) return; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
70 _subscribed = value; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
71 RaisePropertyChanged(SubscribedPropertyName); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
72 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
73 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
74 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
75 private const string ServiceRunningPropertyName = "ServiceRunning"; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
76 private bool _serviceRunning; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
77 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
78 public bool ServiceRunning |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
79 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
80 get { return _serviceRunning; } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
81 set |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
82 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
83 if (_serviceRunning == value) return; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
84 _serviceRunning = value; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
85 RaisePropertyChanged(ServiceRunningPropertyName); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
86 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
87 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
88 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
89 private void ServiceRunningCommandExecute() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
90 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
91 if (_service.IsRunning) |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
92 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
93 _service.Stop(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
94 ServiceRunning = false; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
95 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
96 else |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
97 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
98 _service.Start(); |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
99 ServiceRunning = true; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
100 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
101 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
102 |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
103 private void SubscriptionCommandExecute() |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
104 { |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
105 //toggle subscribed |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
106 Subscribed = !Subscribed; |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
107 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
108 } |
399398841fd0
Working version for Stocks (including loosely coupled components
adminsh@apollo
parents:
diff
changeset
|
109 } |