public void SetCurrentActivity(ActivityId activity, DateTime now)
        {
            lock (this)
            {
                this.EndTime = now;

                if (this.CurrentActivityRegion == null || this.CurrentActivityRegion.ActivityId != activity)
                {
                    if (this.CurrentActivityRegion != null)
                    {
                        // Wrap up the previous activity
                        this.ActivitySummaries[this.CurrentActivityRegion.ActivityId] += now - this.lastUpdateTime;
                        this.CurrentActivityRegion.EndTime = now;
                    }
                    else if (this.ActivityRegions.Count > 0)
                    {
                        // We probably loaded from disk, fill the gap with a nodata region
                        var lastRegion = this.ActivityRegions[this.ActivityRegions.Count - 1];
                        var newRegion  = new ActivityRegion(lastRegion.EndTime, now, ActivityId.NoData);
                        this.ActivitySummaries[newRegion.ActivityId] += newRegion.Duration;
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            this.ActivityRegions.Add(newRegion);
                        });
                    }

                    // Start new activity
                    this.CurrentActivityRegion = new ActivityRegion(now, now, activity);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        this.ActivityRegions.Add(this.CurrentActivityRegion);
                    });
                }

                this.lastUpdateTime = now;
            }

            OnPropertyChanged("EndTime");
            OnPropertyChanged("ActivitySummaries");
            OnPropertyChanged("CurrentActivityRegion");
            OnPropertyChanged("ActivityRegions");
        }
Beispiel #2
0
        private void Update()
        {
            this.nMinusOneActivitySummaries = new Dictionary <ActivityId, TimeSpan>();
            List <ActivityRegion> aggregateRegions = new List <ActivityRegion>();

            for (int i = 0; i < this.dailyActivities.Count - 1; ++i)
            {
                var dailyActivity = this.dailyActivities[i];
                foreach (var entry in dailyActivity.ActivitySummaries)
                {
                    if (nMinusOneActivitySummaries.ContainsKey(entry.Key))
                    {
                        nMinusOneActivitySummaries[entry.Key] += entry.Value;
                    }
                    else
                    {
                        nMinusOneActivitySummaries.Add(entry.Key, entry.Value);
                    }
                }
            }

            ActivityRegion prevRegion = null;

            for (int i = 0; i < this.dailyActivities.Count; ++i)
            {
                var dailyActivity = this.dailyActivities[i];
                if (prevRegion != null)
                {
                    var nextFirstRegion = dailyActivity.ActivityRegions[0];
                    if (prevRegion.EndTime != nextFirstRegion.StartTime)
                    {
                        // If there is a gap between daily activity logs, fill it in
                        if (nextFirstRegion.StartTime < prevRegion.EndTime)
                        {
                            throw new Exception("DailyActivities have not been sorted correctly");
                        }

                        var newRegion = new ActivityRegion(prevRegion.EndTime, nextFirstRegion.StartTime, ActivityId.NoData);
                        aggregateRegions.Add(newRegion);

                        if (nMinusOneActivitySummaries.ContainsKey(ActivityId.NoData))
                        {
                            nMinusOneActivitySummaries[ActivityId.NoData] += newRegion.Duration;
                        }
                        else
                        {
                            nMinusOneActivitySummaries.Add(ActivityId.NoData, newRegion.Duration);
                        }
                    }
                }

                aggregateRegions.AddRange(dailyActivity.ActivityRegions.Where(x => this.isShowingOvernightAway || x.ActivityId != ActivityId.Away || (x.StartTime != x.StartTime.Date && x.EndTime != x.EndTime.Date)));
                if (dailyActivity.ActivityRegions.Count > 0)
                {
                    prevRegion = dailyActivity.ActivityRegions[dailyActivity.ActivityRegions.Count - 1];
                }
            }

            var lastDailyActivity = this.dailyActivities[this.dailyActivities.Count - 1];
            var aggregateSummary  = new Dictionary <ActivityId, TimeSpan>(nMinusOneActivitySummaries);

            foreach (var entry in lastDailyActivity.ActivitySummaries)
            {
                if (aggregateSummary.ContainsKey(entry.Key))
                {
                    aggregateSummary[entry.Key] += entry.Value;
                }
                else
                {
                    aggregateSummary.Add(entry.Key, entry.Value);
                }
            }

            this.CurrentActivityRegion = null;
            if (lastDailyActivity.StartTime.Date == DateTime.Now.Date)
            {
                this.CurrentActivityRegion = lastDailyActivity.CurrentActivityRegion;
                lastDailyActivity.ActivityRegions.CollectionChanged += ActivityRegions_CollectionChanged;
                lastDailyActivity.PropertyChanged += lastDailyActivity_PropertyChanged;
            }

            Application.Current.Dispatcher.InvokeAsync(() =>
            {
                this.ActivityRegions.Clear();
                foreach (var item in aggregateRegions)
                {
                    this.ActivityRegions.Add(item);
                }
            });
            this.ActivitySummaries = aggregateSummary;

            OnPropertyChanged("ActivitySummaries");
            OnPropertyChanged("CurrentActivityRegion");
        }