annotate MetroWpf/Stocks.UI/StocksViewModel.cs @ 21:dfc81f8bb838

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