comparison MetroWpf/FxRates.UI/ViewModels/FxRatesViewModel.cs @ 24:a8b50a087544

Stocks and FxRates working, new menu introduced. Working nicely so far
author adminsh@apollo
date Tue, 20 Mar 2012 20:18:35 +0000
parents
children
comparison
equal deleted inserted replaced
23:399398841fd0 24:a8b50a087544
1 using System;
2 using System.ComponentModel;
3 using System.Linq;
4 using System.Reactive;
5 using System.Reactive.Linq;
6 using System.Windows.Input;
7 using FxRates.Common;
8 using FxRates.UI.Models;
9 using GalaSoft.MvvmLight;
10 using GalaSoft.MvvmLight.Command;
11
12 namespace FxRates.UI.ViewModels
13 {
14 public class FxRatesViewModel : ViewModelBase
15 {
16 private IPricingService pricingService;
17
18 public BindingList<DisplayFxRate> DisplayFxRates { get; set; }
19 public ICommand ServiceRunningCommand { get; set; }
20 public ICommand SubscriptionCommand { get; set; }
21
22 public FxRatesViewModel(IPricingService service)
23 {
24 pricingService = service;
25 GetLatestPrices();
26
27 SubscriptionCommand = new RelayCommand(SubscriptionCommand_Execute);
28 ServiceRunningCommand = new RelayCommand(ServiceRunningCommand_Execute);
29
30 var priceUpdates = Observable.FromEventPattern<PriceUpdateEventArgs>(
31 pricingService, "PriceUpdate");
32
33 priceUpdates.Where(e => (Subscribed)
34 // && (e.EventArgs.LatestPrice.Ccy == Ccy.EURtoGBP) example of filter
35 )
36 //.Throttle(TimeSpan.FromSeconds(1)) example of throttling
37 .Subscribe(PriceUpdate);
38 }
39
40 public void PriceUpdate(EventPattern<PriceUpdateEventArgs> e)
41 {
42 var displayRate = DisplayFxRates.First(rate => rate.Ccy == e.EventArgs.LatestPrice.Ccy);
43
44 if (displayRate != null)
45 displayRate.Update(e.EventArgs.LatestPrice);
46 }
47
48
49 private void GetLatestPrices()
50 {
51 DisplayFxRates = new BindingList<DisplayFxRate>();
52 var currentRates = pricingService.GetFullCurrentPrices();
53 foreach (var latestRate in currentRates)
54 {
55 var displayRate = DisplayFxRate.Create(latestRate);
56 DisplayFxRates.Add(displayRate);
57 }
58 }
59
60 private const string SubscribedPropertyName = "Subscribed";
61 private bool _subscribed = false;
62 public bool Subscribed
63 {
64 get { return _subscribed; }
65 set
66 {
67 if (_subscribed == value) return;
68 _subscribed = value;
69 RaisePropertyChanged(SubscribedPropertyName);
70 }
71 }
72
73 private const string ServiceRunningPropertyName = "ServiceRunning";
74 private bool _serviceRunning;
75 public bool ServiceRunning
76 {
77 get
78 {
79 return _serviceRunning;
80 }
81 set
82 {
83 if (_serviceRunning == value) return;
84 _serviceRunning = value;
85 RaisePropertyChanged(ServiceRunningPropertyName);
86 }
87 }
88
89 void ServiceRunningCommand_Execute()
90 {
91 if (pricingService.IsRunning)
92 {
93 pricingService.Stop();
94 ServiceRunning = false;
95 }
96 else
97 {
98 pricingService.Start();
99 ServiceRunning = true;
100 }
101 }
102
103 void SubscriptionCommand_Execute()
104 {
105 //toggle subscribed
106 Subscribed = !Subscribed;
107 }
108 }
109 }