PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.ObjectModel;
  20. using System.ComponentModel;
  21. using System.Windows.Input;
  22. using Microsoft.Practices.Composite.Events;
  23. using Microsoft.Practices.Composite.Wpf.Commands;
  24. using Microsoft.Practices.Composite.Wpf.Events;
  25. using StockTraderRI.Infrastructure;
  26. using StockTraderRI.Infrastructure.Interfaces;
  27. using StockTraderRI.Modules.Watch.Properties;
  28. using StockTraderRI.Modules.Watch.Services;
  29. namespace StockTraderRI.Modules.Watch.WatchList
  30. {
  31. public class WatchListPresentationModel : IWatchListPresentationModel, INotifyPropertyChanged, IHeaderInfoProvider<string>
  32. {
  33. private ObservableCollection<WatchItem> _watchListItems;
  34. public WatchListPresentationModel(IWatchListView view, IWatchListService watchListService, IMarketFeedService marketFeedService, IEventAggregator eventAggregator)
  35. {
  36. View = view;
  37. this.HeaderInfo = Resources.WatchListTitle;
  38. this.WatchListItems = new ObservableCollection<WatchItem>();
  39. View.Model = this;
  40. this.marketFeedService = marketFeedService;
  41. this.watchList = watchListService.RetrieveWatchList();
  42. watchList.CollectionChanged += delegate { PopulateWatchItemsList(watchList); };
  43. PopulateWatchItemsList(watchList);
  44. eventAggregator.GetEvent<MarketPricesUpdatedEvent>().Subscribe(MarketPricesUpdated, ThreadOption.UIThread);
  45. RemoveWatchCommand = new DelegateCommand<string>(RemoveWatch);
  46. }
  47. public ObservableCollection<WatchItem> WatchListItems
  48. {
  49. get { return _watchListItems; }
  50. set
  51. {
  52. if (_watchListItems != value)
  53. {
  54. _watchListItems = value;
  55. PropertyChanged(this, new PropertyChangedEventArgs("WatchListItems"));
  56. }
  57. }
  58. }
  59. public string HeaderInfo { get; set; }
  60. #region INotifyPropertyChanged Members
  61. public event PropertyChangedEventHandler PropertyChanged = delegate { };
  62. #endregion
  63. private void MarketPricesUpdated(IDictionary<string, decimal> updatedPriceList)
  64. {
  65. foreach (WatchItem watchItem in this.WatchListItems)
  66. {
  67. if (updatedPriceList.ContainsKey(watchItem.TickerSymbol))
  68. watchItem.CurrentPrice = updatedPriceList[watchItem.TickerSymbol];
  69. }
  70. }
  71. private void RemoveWatch(string tickerSymbol)
  72. {
  73. watchList.Remove(tickerSymbol);
  74. }
  75. private void PopulateWatchItemsList(ObservableCollection<string> watchItemsList)
  76. {
  77. this.WatchListItems.Clear();
  78. foreach (string tickerSymbol in watchItemsList)
  79. {
  80. decimal? currentPrice;
  81. try
  82. {
  83. currentPrice = marketFeedService.GetPrice(tickerSymbol);
  84. }
  85. catch (ArgumentException)
  86. {
  87. currentPrice = null;
  88. }
  89. this.WatchListItems.Add(new WatchItem(tickerSymbol, currentPrice));
  90. }
  91. }
  92. public IWatchListView View { get; private set; }
  93. public ICommand RemoveWatchCommand { get; private set; }
  94. private readonly IMarketFeedService marketFeedService;
  95. private readonly ObservableCollection<string> watchList;
  96. }
  97. }