PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/StockTraderRI/StockTraderRI.Modules.WatchList/WatchList/WatchListPresentationModel.cs

#
C# | 113 lines | 81 code | 16 blank | 16 comment | 3 complexity | 1deddb4ac0de44d17823ea154cdfb224 MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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//===============================================================================
 17
 18using System;
 19using System.Collections.Generic;
 20using System.Collections.ObjectModel;
 21using System.ComponentModel;
 22using System.Windows.Input;
 23using Microsoft.Practices.Composite.Events;
 24using Microsoft.Practices.Composite.Wpf.Commands;
 25using Microsoft.Practices.Composite.Wpf.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 ObservableCollection<WatchItem> _watchListItems;
 36
 37        public WatchListPresentationModel(IWatchListView view, IWatchListService watchListService, IMarketFeedService marketFeedService, IEventAggregator eventAggregator)
 38        {
 39            View = view;
 40            this.HeaderInfo = Resources.WatchListTitle;
 41            this.WatchListItems = new ObservableCollection<WatchItem>();
 42            View.Model = this;
 43
 44            this.marketFeedService = marketFeedService;
 45
 46            this.watchList = watchListService.RetrieveWatchList();
 47            watchList.CollectionChanged += delegate { PopulateWatchItemsList(watchList); };
 48            PopulateWatchItemsList(watchList);
 49
 50            eventAggregator.GetEvent<MarketPricesUpdatedEvent>().Subscribe(MarketPricesUpdated, ThreadOption.UIThread);
 51            RemoveWatchCommand = new DelegateCommand<string>(RemoveWatch);
 52        }
 53
 54
 55        public ObservableCollection<WatchItem> WatchListItems
 56        {
 57            get { return _watchListItems; }
 58            set
 59            {
 60                if (_watchListItems != value)
 61                {
 62                    _watchListItems = value;
 63                    PropertyChanged(this, new PropertyChangedEventArgs("WatchListItems"));
 64                }
 65            }
 66        }
 67
 68        public string HeaderInfo { get; set; }
 69
 70        #region INotifyPropertyChanged Members
 71
 72        public event PropertyChangedEventHandler PropertyChanged = delegate { };
 73
 74        #endregion
 75
 76        private void MarketPricesUpdated(IDictionary<string, decimal> updatedPriceList)
 77        {
 78            foreach (WatchItem watchItem in this.WatchListItems)
 79            {
 80                if (updatedPriceList.ContainsKey(watchItem.TickerSymbol))
 81                    watchItem.CurrentPrice = updatedPriceList[watchItem.TickerSymbol];
 82            }
 83        }
 84
 85        private void RemoveWatch(string tickerSymbol)
 86        {
 87            watchList.Remove(tickerSymbol);
 88        }
 89
 90        private void PopulateWatchItemsList(ObservableCollection<string> watchItemsList)
 91        {
 92            this.WatchListItems.Clear();
 93            foreach (string tickerSymbol in watchItemsList)
 94            {
 95                decimal? currentPrice;
 96                try
 97                {
 98                    currentPrice = marketFeedService.GetPrice(tickerSymbol);
 99                }
100                catch (ArgumentException)
101                {
102                    currentPrice = null;
103                }
104                this.WatchListItems.Add(new WatchItem(tickerSymbol, currentPrice));
105            }
106        }
107
108        public IWatchListView View { get; private set; }
109        public ICommand RemoveWatchCommand { get; private set; }
110        private readonly IMarketFeedService marketFeedService;
111        private readonly ObservableCollection<string> watchList;
112    }
113}