Ejemplo n.º 1
0
    public CharterSnapshotControl() {
      InitializeComponent();

      this.SizeChanged += (s, e) => FitToView();

      MessageBus.Current.Listen<HedgeHog.NewsCaster.NewsEvent>("Snapshot")
        .Throttle(1.FromSeconds(),DispatcherScheduler.Current)
        .Where(ne => VisualParent != null)
        .Subscribe(ne => {
          try {
            var monthOffset = 3;
            var dateStart = ne.Time.AddMonths(-monthOffset*2);
            var dateEnd = ne.Time.AddDays(-1);
            ReadNewsEventsHistoryFromDB(ne.Country, ne.Name, dateStart, dateEnd);
            NewsHistoryCurrent = NewsEventHistory.First();
            NewsEventCurrent = NewsEvents.FirstOrDefault(n => n.Country == ne.Country && n.Name == ne.Name);
            DateStart = NewsEventHistory.First().Time.DateTime;
            Show();
          } catch (Exception exc) { LogMessage.Send(exc); }
        });

      Pairs.AddRange(ForexStorage.UseForexContext(c => c.v_Pair.ToArray(), (c, ex) => LogMessage.Send(ex)));
      otherVLines.Changing.ObserveOnDispatcher()
        .Subscribe(ev => {
          switch (ev.Action) {
            case NotifyCollectionChangedAction.Reset:
            case NotifyCollectionChangedAction.Remove:
              try {
                ev.OldItems.Cast<IPlotterElement>().ToList().ForEach(vl => plotter.Children.Remove(vl));
              } catch (Exception exc) {
                LogMessage.Send(exc);
              }
              return;
            case NotifyCollectionChangedAction.Add:
              ev.NewItems.Cast<VerticalLine>().ForEach(vl => plotter.Children.Add(vl));
              return;
          }
        });
      DispatcherScheduler.Current.Schedule(1.FromSeconds(), () => {
        Event__News newsEventPrev = null;
        this.ObservableForProperty(me => me.NewsEventCurrent, ne => ne).Subscribe(ne => {
          try {
            if (ne == null || CompareNewsEvents(ne, newsEventPrev)) return;
            newsEventPrev = ne;
            DateStart = ne.Time.DateTime;
            Show();
            var monthOffset = 3;
            var dateStart = ne.Time.AddMonths(-monthOffset);
            var dateEnd = ne.Time.AddMonths(monthOffset);
            ReadNewsEventsHistoryFromDB(ne.Country, ne.Name, dateStart, dateEnd);
            NewsHistoryCurrent = NewsEventHistory.FirstOrDefault(n =>
              n.Country == NewsEventCurrent.Country && n.Name == NewsHistoryCurrent.Name && n.Time == NewsEventCurrent.Time);
          } catch (Exception ex) { LogMessage.Send(ex); }
        }, ex => LogMessage.Send(ex), () => LogMessage.Send("Done with NewsEventCurrent."));

        this.ObservableForProperty(me => me.NewsHistoryCurrent, nh => nh).Subscribe(nh => {
          try {
            if (nh == null || CompareNewsEvents(NewsEventCurrent, nh)) return;
            DateStart = nh.Time.DateTime;
            Show();
            var newsEvent = NewsEvents.SingleOrDefault(ne => ne.Country == nh.Country && ne.Name == nh.Name);
            if (newsEvent != null)
              NewsEventCurrent = newsEventPrev = newsEvent;
          } catch (Exception ex) { LogMessage.Send(ex); }
        });

        this.ObservableForProperty(me => me.PairCurrent).Subscribe(oc => Show());
      });
    }