PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.ObjectModel;
  20. using System.ComponentModel;
  21. using System.Linq;
  22. using System.Windows.Input;
  23. using Microsoft.Practices.Composite.Events;
  24. using Microsoft.Practices.Composite.Presentation.Commands;
  25. using Microsoft.Practices.Composite.Presentation.Events;
  26. using StockTraderRI.Infrastructure;
  27. using StockTraderRI.Infrastructure.Interfaces;
  28. using StockTraderRI.Modules.Watch.Properties;
  29. using StockTraderRI.Modules.Watch.Services;
  30. namespace StockTraderRI.Modules.Watch.WatchList
  31. {
  32. public class WatchListPresentationModel : IWatchListPresentationModel, INotifyPropertyChanged, IHeaderInfoProvider<string>
  33. {
  34. private readonly IMarketFeedService marketFeedService;
  35. private readonly IEventAggregator eventAggregator;
  36. private readonly ObservableCollection<string> watchList;
  37. private ICommand removeWatchCommand;
  38. private ObservableCollection<WatchItem> watchListItems;
  39. private WatchItem currentWatchItem;
  40. public WatchListPresentationModel(IWatchListView view, IWatchListService watchListService, IMarketFeedService marketFeedService, IEventAggregator eventAggregator)
  41. {
  42. this.View = view;
  43. this.HeaderInfo = Resources.WatchListTitle;
  44. this.WatchListItems = new ObservableCollection<WatchItem>();
  45. this.View.SetModel(this);
  46. this.marketFeedService = marketFeedService;
  47. this.watchList = watchListService.RetrieveWatchList();
  48. this.watchList.CollectionChanged += delegate { this.PopulateWatchItemsList(this.watchList); };
  49. this.PopulateWatchItemsList(this.watchList);
  50. this.eventAggregator = eventAggregator;
  51. this.eventAggregator.GetEvent<MarketPricesUpdatedEvent>().Subscribe(this.MarketPricesUpdated, ThreadOption.UIThread);
  52. this.RemoveWatchCommand = new DelegateCommand<string>(this.RemoveWatch);
  53. }
  54. public event PropertyChangedEventHandler PropertyChanged = delegate { };
  55. public ObservableCollection<WatchItem> WatchListItems
  56. {
  57. get
  58. {
  59. return this.watchListItems;
  60. }
  61. set
  62. {
  63. if (this.watchListItems != value)
  64. {
  65. this.watchListItems = value;
  66. this.OnPropertyChanged("WatchListItems");
  67. }
  68. }
  69. }
  70. public WatchItem CurrentWatchItem
  71. {
  72. get
  73. {
  74. return this.currentWatchItem;
  75. }
  76. set
  77. {
  78. if (value != null && this.currentWatchItem != value)
  79. {
  80. this.currentWatchItem = value;
  81. this.OnPropertyChanged("CurrentWatchItem");
  82. this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish(this.currentWatchItem.TickerSymbol);
  83. }
  84. }
  85. }
  86. public string HeaderInfo { get; set; }
  87. public IWatchListView View { get; private set; }
  88. public ICommand RemoveWatchCommand
  89. {
  90. get
  91. {
  92. return this.removeWatchCommand;
  93. }
  94. private set
  95. {
  96. this.removeWatchCommand = value;
  97. this.OnPropertyChanged("RemoveWatchCommand");
  98. }
  99. }
  100. public void MarketPricesUpdated(IDictionary<string, decimal> updatedPrices)
  101. {
  102. foreach (WatchItem watchItem in this.WatchListItems)
  103. {
  104. if (updatedPrices.ContainsKey(watchItem.TickerSymbol))
  105. {
  106. watchItem.CurrentPrice = updatedPrices[watchItem.TickerSymbol];
  107. }
  108. }
  109. }
  110. private void RemoveWatch(string tickerSymbol)
  111. {
  112. this.watchList.Remove(tickerSymbol);
  113. }
  114. private void PopulateWatchItemsList(IEnumerable<string> watchItemsList)
  115. {
  116. this.WatchListItems.Clear();
  117. foreach (string tickerSymbol in watchItemsList)
  118. {
  119. decimal? currentPrice;
  120. try
  121. {
  122. currentPrice = this.marketFeedService.GetPrice(tickerSymbol);
  123. }
  124. catch (ArgumentException)
  125. {
  126. currentPrice = null;
  127. }
  128. this.WatchListItems.Add(new WatchItem(tickerSymbol, currentPrice));
  129. }
  130. }
  131. private void OnPropertyChanged(string propertyName)
  132. {
  133. PropertyChangedEventHandler handler = this.PropertyChanged;
  134. if (handler != null)
  135. {
  136. handler(this, new PropertyChangedEventArgs(propertyName));
  137. }
  138. }
  139. }
  140. }