private void RefreshModel() { var workingDays = ModelHelpers.Gallifrey.Settings.AppSettings.ExportDays.ToList(); var workingDate = DateTime.Now.AddDays((ModelHelpers.Gallifrey.Settings.AppSettings.KeepTimersForDays - 1) * -1).Date; var validTimerDates = new List <DateTime>(); var jiraCache = new List <Issue>(); while (workingDate.Date <= DateTime.Now.Date) { if (workingDays.Contains(workingDate.DayOfWeek)) { validTimerDates.Add(workingDate.Date); } workingDate = workingDate.AddDays(1); } foreach (var timerDate in ModelHelpers.Gallifrey.JiraTimerCollection.GetValidTimerDates()) { if (!validTimerDates.Contains(timerDate)) { validTimerDates.Add(timerDate.Date); } } foreach (var timerDate in validTimerDates.OrderBy(x => x.Date)) { var dateModel = TimerDates.FirstOrDefault(x => x.TimerDate.Date == timerDate.Date); if (dateModel == null) { dateModel = new TimerDateModel(timerDate, ModelHelpers.Gallifrey.JiraTimerCollection); TimerDates.Add(dateModel); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TimerDates))); } var dateTimers = ModelHelpers.Gallifrey.JiraTimerCollection.GetTimersForADate(timerDate.Date).ToList(); foreach (var timer in dateTimers) { if (dateModel.Timers.All(x => x.JiraTimer.UniqueId != timer.UniqueId)) { timer.PropertyChanged += (sender, args) => TimerChanged(); dateModel.AddTimerModel(new TimerModel(timer)); } } foreach (var removeTimer in dateModel.Timers.Where(timerModel => dateTimers.All(x => x.UniqueId != timerModel.JiraTimer.UniqueId)).ToList()) { dateModel.RemoveTimerModel(removeTimer); } if (dateModel.TimerDate.Date <= DateTime.Now.Date) { var defaultTimers = (ModelHelpers.Gallifrey.Settings.AppSettings.DefaultTimers ?? new List <string>()).Select(x => x.ToUpper().Trim()).Distinct(); foreach (var defaultJira in defaultTimers) { if (!dateModel.Timers.Any(x => string.Equals(x.JiraTimer.JiraReference, defaultJira, StringComparison.InvariantCultureIgnoreCase) && x.JiraTimer.DateStarted.Date == dateModel.TimerDate.Date)) { try { var jira = jiraCache.FirstOrDefault(x => string.Equals(x.key, defaultJira, StringComparison.InvariantCultureIgnoreCase)); if (jira == null && ModelHelpers.Gallifrey.JiraConnection.IsConnected) { if (ModelHelpers.Gallifrey.JiraConnection.DoesJiraExist(defaultJira)) { jira = ModelHelpers.Gallifrey.JiraConnection.GetJiraIssue(defaultJira); jiraCache.Add(jira); } } if (jira != null && string.Equals(jira.key, defaultJira, StringComparison.InvariantCultureIgnoreCase)) { var timerId = ModelHelpers.Gallifrey.JiraTimerCollection.AddTimer(jira, dateModel.TimerDate.Date, TimeSpan.Zero, false); var timer = ModelHelpers.Gallifrey.JiraTimerCollection.GetTimer(timerId); if (timer != null) { timer.PropertyChanged += (sender, args) => TimerChanged(); dateModel.AddTimerModel(new TimerModel(timer)); } } } catch (Exception ex) { ExceptionlessClient.Default.CreateEvent().SetException(ex).AddTags("Hidden").Submit(); } } } } } //see if the order would be different now, and if so, recreate the TimerDates var orderedCollection = TimerDates.Where(x => validTimerDates.Contains(x.TimerDate)).OrderByDescending(x => x.TimerDate).ToList(); if (orderedCollection.Count != TimerDates.Count) { TimerDates = new ObservableCollection <TimerDateModel>(orderedCollection); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TimerDates))); } else { for (var i = 0; i < TimerDates.Count; i++) { var main = TimerDates[i]; var ordered = orderedCollection[i]; if (main.TimerDate != ordered.TimerDate) { TimerDates = new ObservableCollection <TimerDateModel>(orderedCollection); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TimerDates))); break; } } } SetTrackingOnlyInModel(); targetBarValues.Update(); }
private void RefreshModel() { var workingDays = ModelHelpers.Gallifrey.Settings.AppSettings.ExportDays.ToList(); var workingDate = DateTime.Now.AddDays((ModelHelpers.Gallifrey.Settings.AppSettings.KeepTimersForDays - 1) * -1).Date; var validTimerDates = new List <DateTime>(); var jiraCache = new List <Issue>(); while (workingDate.Date <= DateTime.Now.Date) { if (workingDays.Contains(workingDate.DayOfWeek)) { validTimerDates.Add(workingDate.Date); } workingDate = workingDate.AddDays(1); } foreach (var timerDate in ModelHelpers.Gallifrey.JiraTimerCollection.GetValidTimerDates()) { if (!validTimerDates.Contains(timerDate)) { validTimerDates.Add(timerDate.Date); } } foreach (var timerDate in validTimerDates.OrderBy(x => x.Date)) { var dateModel = TimerDates.FirstOrDefault(x => x.TimerDate.Date == timerDate.Date); if (dateModel == null) { dateModel = new TimerDateModel(timerDate, ModelHelpers.Gallifrey.JiraTimerCollection); TimerDates.Add(dateModel); } var dateTimers = ModelHelpers.Gallifrey.JiraTimerCollection.GetTimersForADate(timerDate).ToList(); foreach (var timer in dateTimers) { if (!dateModel.Timers.Any(x => x.JiraTimer.UniqueId == timer.UniqueId)) { timer.PropertyChanged += (sender, args) => TimerChanged(); dateModel.AddTimerModel(new TimerModel(timer)); } } foreach (var removeTimer in dateModel.Timers.Where(timerModel => !dateTimers.Any(x => x.UniqueId == timerModel.JiraTimer.UniqueId)).ToList()) { dateModel.RemoveTimerModel(removeTimer); } foreach (var defaultJira in ModelHelpers.Gallifrey.Settings.AppSettings.DefaultTimers ?? new List <string>()) { if (!dateModel.Timers.Any(x => x.JiraTimer.JiraReference == defaultJira) && dateModel.TimerDate.Date <= DateTime.Now.Date) { try { var jira = jiraCache.FirstOrDefault(x => x.key == defaultJira); if (jira == null && ModelHelpers.Gallifrey.JiraConnection.IsConnected) { jira = ModelHelpers.Gallifrey.JiraConnection.GetJiraIssue(defaultJira); jiraCache.Add(jira); } if (jira != null) { var timerId = ModelHelpers.Gallifrey.JiraTimerCollection.AddTimer(jira, timerDate.Date, TimeSpan.Zero, false); var timer = ModelHelpers.Gallifrey.JiraTimerCollection.GetTimer(timerId); timer.PropertyChanged += (sender, args) => TimerChanged(); dateModel.AddTimerModel(new TimerModel(timer)); } } catch (Exception) { // ignored } } } } //see if the order would be different now, and if so, recreate the TimerDates var orderedCollection = TimerDates.Where(x => validTimerDates.Contains(x.TimerDate)).OrderByDescending(x => x.TimerDate).ToList(); if (orderedCollection.Count != TimerDates.Count) { TimerDates = new ObservableCollection <TimerDateModel>(orderedCollection); } else { for (var i = 0; i < TimerDates.Count; i++) { var main = TimerDates[i]; var ordered = orderedCollection[i]; if (main.TimerDate != ordered.TimerDate) { TimerDates = new ObservableCollection <TimerDateModel>(orderedCollection); break; } } } PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TimerDates")); SetTrackingOnlyInModel(); targetBarValues.Update(); }