From 0147753ff67be957b9d5b36fd6878b491ab0cefc Mon Sep 17 00:00:00 2001 From: mdn <1stdragon@gmail.com> Date: Sun, 6 Dec 2015 21:28:38 +0100 Subject: [PATCH] Nochmal etwas umgestalltet und automatische Zeitaktualisierung eingebaut --- .../TimeScheduler/Domain/Impl/TimeItem.cs | 22 +++- TimeScheduler/TimeScheduler/MainWindow.xaml | 33 ++++-- .../TimeScheduler/ViewModel/MainViewModel.cs | 105 +++++++++++++++++- 3 files changed, 142 insertions(+), 18 deletions(-) diff --git a/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs b/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs index a162887..f3be84a 100644 --- a/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs +++ b/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs @@ -75,11 +75,29 @@ namespace TimeScheduler.Domain.Impl private DateTime from_; /// - public DateTime From { get { return from_; } set { SetField(ref from_, value); } } + public DateTime From + { + get { return from_; } + set + { + if (SetField(ref from_, value.Subtract(TimeSpan.FromMinutes(value.Minute % 15)))) + if (From > Till) + Till = From; + } + } private DateTime till_; /// - public DateTime Till { get { return till_; } set { SetField(ref till_, value); } } + public DateTime Till + { + get { return till_; } + set + { + if (SetField(ref till_, value.Subtract(TimeSpan.FromMinutes(value.Minute % 15)))) + if (Till < From) + From = Till; + } + } private int costUnit_; /// diff --git a/TimeScheduler/TimeScheduler/MainWindow.xaml b/TimeScheduler/TimeScheduler/MainWindow.xaml index 3795fc7..bd190fb 100644 --- a/TimeScheduler/TimeScheduler/MainWindow.xaml +++ b/TimeScheduler/TimeScheduler/MainWindow.xaml @@ -18,6 +18,7 @@ + @@ -25,7 +26,7 @@ - + @@ -63,12 +64,22 @@ + + + - - - - - + + + + + + + + + + @@ -109,8 +120,10 @@ + + - + @@ -145,13 +158,13 @@ - + - + - + aktuelles Ele + private ITimeItem lastTimeItem_; + /// Element, das noch undefinierten Zeitraum speichert + private ITimeItem undefinedItem_; + /// Zyklische Aktuallisierung + private System.Timers.Timer timer_; + /// Standard-Konstruktor public MainViewModel() : this(new Domain.Impl.FileDomain()) { Refresh(); } @@ -21,32 +28,105 @@ namespace TimeScheduler.ViewModel { Provider = provider; + // Elementliste aufbauen TimeItems = new ObservableCollection(); TimeItems.CollectionChanged += TimeItems_CollectionChanged; + var view = System.Windows.Data.CollectionViewSource.GetDefaultView(TimeItems); + view.SortDescriptions.Add(new System.ComponentModel.SortDescription("From", System.ComponentModel.ListSortDirection.Ascending)); + // Ereignis zum autmoatisierten speichern konsumieren + Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; + Microsoft.Win32.SystemEvents.SessionEnded += SystemEvents_SessionEnded; + Microsoft.Win32.SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; + + // Kommandos erzeugen CreateCommands(); + + // Zyklischer Timer aufbauen + timer_ = new System.Timers.Timer(10000); + timer_.Elapsed += Timer_Elapsed; + timer_.Start(); } + private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e) + { + try + { + switch (e.Mode) + { + case Microsoft.Win32.PowerModes.Resume: break; + case Microsoft.Win32.PowerModes.StatusChange: break; + case Microsoft.Win32.PowerModes.Suspend: + // Beim wechseln in Suspend, ein neues Element erzeugen, wenn das vorhergehende lang genug ist + Save(); + if (lastTimeItem_ != null) + if ((lastTimeItem_.Till - lastTimeItem_.From) > TimeSpan.FromMinutes(15)) + { + var newItem = Provider.NewItem(); + newItem.From = lastTimeItem_.Till; + TimeItems.Add(newItem); + } + break; + } + } + catch (Exception exc) { } + } + private void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e) { Save(); } + private void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) + { + try + { + switch (e.Reason) + { + case Microsoft.Win32.SessionSwitchReason.ConsoleConnect: break; + case Microsoft.Win32.SessionSwitchReason.ConsoleDisconnect: break; + case Microsoft.Win32.SessionSwitchReason.RemoteConnect: break; + case Microsoft.Win32.SessionSwitchReason.RemoteDisconnect: break; + case Microsoft.Win32.SessionSwitchReason.SessionLogon: break; + case Microsoft.Win32.SessionSwitchReason.SessionLogoff: break; + case Microsoft.Win32.SessionSwitchReason.SessionLock: + // Beim Sperren, eine neue Session beginnen + Save(); + if (lastTimeItem_ != null) + if ((lastTimeItem_.Till - lastTimeItem_.From) >= TimeSpan.FromMinutes(15)) + { + var newItem = Provider.NewItem(); + newItem.From = lastTimeItem_.Till; + TimeItems.Add(newItem); + } + break; + case Microsoft.Win32.SessionSwitchReason.SessionUnlock: break; + case Microsoft.Win32.SessionSwitchReason.SessionRemoteControl: break; + } + } + catch (Exception exc) { } + } + private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Save(); } + + /// Datenbeschaffer protected IDomain Provider { get; private set; } #endregion #region Eigenschaften - private DateTime currentDate_ = DateTime.Now.Date; - public DateTime CurrentDate + private DateTime selectedDate_ = DateTime.Now.Date; + /// Beinhaltet den selektierten Tag + public DateTime SelectedDate { - get { return currentDate_; } + get { return selectedDate_; } set { - if (SetField(ref currentDate_, value)) - RefreshForDate(currentDate_); + if (SetField(ref selectedDate_, value)) + RefreshForDate(selectedDate_); } } private Dictionary costUnits_; + /// Liste der vorhanden Kostenstellen public Dictionary CostUnits { get { return costUnits_; } private set { SetField(ref costUnits_, value); } } private Dictionary itemTypes_; + /// Liste der vorhanden Typen von TimeItem public Dictionary ItemTypes { get { return itemTypes_; } private set { SetField(ref itemTypes_, value); } } private ObservableCollection timeItems_; @@ -77,7 +157,18 @@ namespace TimeScheduler.ViewModel ItemTypes = it; // Andere Daten ermitteln - RefreshForDate(CurrentDate); + RefreshForDate(SelectedDate); + + // Letztes Element ermitteln, auf dem weiter gelaufen wird + var lastItem = TimeItems.OrderByDescending(x => x.From).FirstOrDefault(); + if (lastItem == null || lastItem.From.Date != SelectedDate.Date) + {// Wenn keines gefunden wurde oder keins für den heutigen Tag gefunden wurde, dann eins neu erzeugen + lastItem = Provider.NewItem(); + lastItem.From = DateTime.Now; + TimeItems.Add(lastItem); + } + // und abspeichern + lastTimeItem_ = lastItem; } private void RefreshForDate(DateTime date) { @@ -119,6 +210,8 @@ namespace TimeScheduler.ViewModel public ICommand DeleteCommand { get; private set; } private void Delete() { TimeItems.Remove(SelectedTimeItem); } public bool CanDelete() { return SelectedTimeItem != null; } + + private void Save() { if (lastTimeItem_ != null) { lastTimeItem_.Till = DateTime.Now; } } #endregion #region Hilfsfunktionen