PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/V2/trunk/RI/Desktop/StockTraderRI.Modules.WatchList/WatchList/WatchListPresentationModel.cs

#
C# | 159 lines | 124 code | 19 blank | 16 comment | 9 complexity | cd03acdd6e564d0e093566ba0ddcd8c1 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Collections.ObjectModel;
 20using System.ComponentModel;
 21using System.Linq;
 22using System.Windows.Input;
 23using Microsoft.Practices.Composite.Events;
 24using Microsoft.Practices.Composite.Presentation.Commands;
 25using Microsoft.Practices.Composite.Presentation.Events;
 26using StockTraderRI.Infrastructure;
 27using StockTraderRI.Infrastructure.Interfaces;
 28using StockTraderRI.Modules.Watch.Properties;
 29using StockTraderRI.Modules.Watch.Services;
 30
 31namespace StockTraderRI.Modules.Watch.WatchList
 32{
 33    public class WatchListPresentationModel : IWatchListPresentationModel, INotifyPropertyChanged, IHeaderInfoProvider<string>
 34    {
 35        private readonly IMarketFeedService marketFeedService;
 36        private readonly IEventAggregator eventAggregator;
 37        private readonly ObservableCollection<string> watchList;
 38        private ICommand removeWatchCommand;
 39        private ObservableCollection<WatchItem> watchListItems;
 40        private WatchItem currentWatchItem;
 41
 42        public WatchListPresentationModel(IWatchListView view, IWatchListService watchListService, IMarketFeedService marketFeedService, IEventAggregator eventAggregator)
 43        {
 44            this.View = view;
 45            this.HeaderInfo = Resources.WatchListTitle;
 46            this.WatchListItems = new ObservableCollection<WatchItem>();
 47            this.View.SetModel(this);
 48
 49            this.marketFeedService = marketFeedService;
 50
 51            this.watchList = watchListService.RetrieveWatchList();
 52            this.watchList.CollectionChanged += delegate { this.PopulateWatchItemsList(this.watchList); };
 53            this.PopulateWatchItemsList(this.watchList);
 54
 55            this.eventAggregator = eventAggregator;
 56            this.eventAggregator.GetEvent<MarketPricesUpdatedEvent>().Subscribe(this.MarketPricesUpdated, ThreadOption.UIThread);
 57            this.RemoveWatchCommand = new DelegateCommand<string>(this.RemoveWatch);
 58        }
 59
 60        public event PropertyChangedEventHandler PropertyChanged = delegate { };
 61
 62        public ObservableCollection<WatchItem> WatchListItems
 63        {
 64            get
 65            {
 66                return this.watchListItems;
 67            }
 68
 69            set
 70            {
 71                if (this.watchListItems != value)
 72                {
 73                    this.watchListItems = value;
 74                    this.OnPropertyChanged("WatchListItems");
 75                }
 76            }
 77        }
 78
 79        public WatchItem CurrentWatchItem
 80        {
 81            get
 82            {
 83                return this.currentWatchItem;
 84            }
 85
 86            set
 87            {
 88                if (value != null && this.currentWatchItem != value)
 89                {
 90                    this.currentWatchItem = value;
 91                    this.OnPropertyChanged("CurrentWatchItem");
 92                    this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish(this.currentWatchItem.TickerSymbol);
 93                }
 94            }
 95        }
 96
 97        public string HeaderInfo { get; set; }
 98
 99        public IWatchListView View { get; private set; }
100
101        public ICommand RemoveWatchCommand
102        {
103            get
104            {
105                return this.removeWatchCommand;
106            }
107
108            private set
109            {
110                this.removeWatchCommand = value;
111                this.OnPropertyChanged("RemoveWatchCommand");
112            }
113        }
114
115        public void MarketPricesUpdated(IDictionary<string, decimal> updatedPrices)
116        {
117            foreach (WatchItem watchItem in this.WatchListItems)
118            {
119                if (updatedPrices.ContainsKey(watchItem.TickerSymbol))
120                {
121                    watchItem.CurrentPrice = updatedPrices[watchItem.TickerSymbol];
122                }
123            }
124        }
125
126        private void RemoveWatch(string tickerSymbol)
127        {
128            this.watchList.Remove(tickerSymbol);
129        }
130
131        private void PopulateWatchItemsList(IEnumerable<string> watchItemsList)
132        {
133            this.WatchListItems.Clear();
134            foreach (string tickerSymbol in watchItemsList)
135            {
136                decimal? currentPrice;
137                try
138                {
139                    currentPrice = this.marketFeedService.GetPrice(tickerSymbol);
140                }
141                catch (ArgumentException)
142                {
143                    currentPrice = null;
144                }
145
146                this.WatchListItems.Add(new WatchItem(tickerSymbol, currentPrice));
147            }
148        }
149
150        private void OnPropertyChanged(string propertyName)
151        {
152            PropertyChangedEventHandler handler = this.PropertyChanged;
153            if (handler != null)
154            {
155                handler(this, new PropertyChangedEventArgs(propertyName));
156            }
157        }
158    }
159}