using System; using System.Windows.Input; using System.ComponentModel; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Windows; using System.Linq; using System.Text; using System.Threading.Tasks; using MarketData; using MarketData.Utils; using MarketData.DataAccess; using MarketData.MarketDataModel; using TradeBlotter.DataAccess; using TradeBlotter.Command; using TradeBlotter.Model; using TradeBlotter.Cache; namespace TradeBlotter.ViewModels { public class DividendHistoryViewModel : WorkspaceViewModel { private List watchLists; private List symbols; private String selectedWatchList; private String symbol; private String selectedCompanyName; private RelayCommand refreshCommand; private RelayCommand getLatestDivExDatesCommand; private bool busyIndicator = false; public DividendHistoryViewModel() { base.DisplayName = "DividendHistory"; watchLists = WatchListDA.GetWatchLists(); watchLists.Insert(0, Constants.CONST_ALL); selectedWatchList = watchLists.Find(x => x.Equals("Valuations")); symbols = WatchListDA.GetWatchList(selectedWatchList); PropertyChanged += OnDividendHistoryViewModelPropertyChanged; } public override SaveParameters GetSaveParameters() { SaveParameters saveParams = new SaveParameters(); if (null == symbol) return null; saveParams.Add(new KeyValuePair("Type", GetType().Namespace + "." + GetType().Name)); saveParams.Add(new KeyValuePair("SelectedSymbol", symbol)); saveParams.Add(new KeyValuePair("SelectedWatchList", selectedWatchList)); return saveParams; } public override void SetSaveParameters(SaveParameters saveParameters) { try { symbol = (from KeyValuePair item in saveParameters where item.Key.Equals("SelectedSymbol") select item).FirstOrDefault().Value; selectedWatchList = (from KeyValuePair item in saveParameters where item.Key.Equals("SelectedWatchList") select item).FirstOrDefault().Value; Referer=saveParameters.Referer; base.OnPropertyChanged("SelectedWatchList"); base.OnPropertyChanged("SelectedSymbol"); } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString())); } } public override bool CanPersist() { return true; } public ObservableCollection AllDividendHistory { get; private set; } public bool BusyIndicator { get { return busyIndicator; } set { busyIndicator = value; base.OnPropertyChanged("BusyIndicator"); } } private void OnDividendHistoryViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs) { if (eventArgs.PropertyName.Equals("SelectedSymbol") && null != symbol) { BusyIndicator = true; Task workerTask = Task.Factory.StartNew(() => { base.DisplayName = "Dividend History (" + symbol + ")"; base.OnPropertyChanged("DisplayName"); String companyName = PricingDA.GetNameForSymbol(symbol); selectedCompanyName = PricingDA.GetNameForSymbol(symbol); List priceModels = new List(); List priceModelList = priceModels.ToList(); DividendHistory dividendHistory=DividendHistoryDA.GetDividendHistory(symbol); if(null!=dividendHistory && 0!=dividendHistory.Count) this.AllDividendHistory = new ObservableCollection(dividendHistory); else this.AllDividendHistory = new ObservableCollection(); }); workerTask.ContinueWith((continuation) => { BusyIndicator = false; base.OnPropertyChanged("AllDividendHistory"); base.OnPropertyChanged("Title"); }); } else if (eventArgs.PropertyName.Equals("SelectedWatchList")) { // if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = PricingDA.GetSymbols(); if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = SymbolCache.GetInstance().GetSymbols(); else symbols = WatchListDA.GetWatchList(selectedWatchList); base.OnPropertyChanged("Symbols"); } } public override String Title { get { if (null != selectedCompanyName && null!=symbol) return selectedCompanyName+" ("+symbol+")"; return null; } } public List WatchListNames { get { return watchLists; } set { ;} } public String SelectedWatchList { get { return selectedWatchList; } set { selectedWatchList = value; base.OnPropertyChanged("SelectedWatchList"); } } public List Symbols { get { return symbols; } } public String SelectedSymbol { get { return symbol; } set { if (value == symbol || String.IsNullOrEmpty(value)) return; symbol = value; base.OnPropertyChanged("SelectedSymbol"); } } private void Refresh() { base.OnPropertyChanged("SelectedSymbol"); } public ICommand RefreshCommand { get { if (refreshCommand == null) { refreshCommand = new RelayCommand(param => this.Refresh(), param => { return true; }); } return refreshCommand; } } public void GetLatestDivExDates() { BusyIndicator = true; Task workerTask = Task.Factory.StartNew(() => { List divExDates = DividendHistoryDA.GetLatestDivExDates(); DividendHistory dividendHistory = DividendHistoryDA.GetDividendHistory(divExDates); if (null != dividendHistory && 0 != dividendHistory.Count) this.AllDividendHistory = new ObservableCollection(dividendHistory); }); workerTask.ContinueWith((continuation) => { BusyIndicator = false; base.OnPropertyChanged("AllDividendHistory"); }); } public ICommand GetLatestDivExDatesCommand { get { if (getLatestDivExDatesCommand == null) { getLatestDivExDatesCommand = new RelayCommand(param => this.GetLatestDivExDates(), param => { return true; }); } return getLatestDivExDatesCommand; } } } }