From ec674fa64bfff5b567afbb3a9d4bb5e255073197 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Mon, 7 Sep 2020 21:49:22 +0300 Subject: [PATCH] move all activation handling into MainPage --- PomoTime/App.xaml.cs | 32 ++++--------------------- PomoTime/MainPage.xaml.cs | 50 ++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/PomoTime/App.xaml.cs b/PomoTime/App.xaml.cs index b326378..540d723 100644 --- a/PomoTime/App.xaml.cs +++ b/PomoTime/App.xaml.cs @@ -24,7 +24,6 @@ namespace PomoTime /// sealed partial class App : Application { - public RunningState RunningState = new RunningState(); /// /// Initializes the singleton application object. This is the first line of authored code @@ -44,16 +43,6 @@ namespace PomoTime { Frame rootFrame = Window.Current.Content as Frame; - ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; - if (localSettings.Values["MinutesLeft"] != null) - { - RunningState.MinutesLeft = (int)localSettings.Values["MinutesLeft"]; - RunningState.SecondsLeft = (int)localSettings.Values["SecondsLeft"]; - RunningState.IsRunning = (bool)localSettings.Values["IsRunning"]; - RunningState.PreviousShortBreaks = (int)localSettings.Values["PreviousShortBreaks"]; - RunningState.CurrentPeriod = (Period)localSettings.Values["CurrentPeriod"]; - } - // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) @@ -78,7 +67,7 @@ namespace PomoTime // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter - rootFrame.Navigate(typeof(MainPage), RunningState); + rootFrame.Navigate(typeof(MainPage), null); } // Ensure the current window is active Window.Current.Activate(); @@ -90,23 +79,12 @@ namespace PomoTime QueryString args = QueryString.Parse(toastActivationArgs.Argument); if (args.Contains("action")) { - localSettings.Values["SuspendTime"] = (DateTime.Now + new TimeSpan(1, 0, 0)).Ticks; - switch (args["action"]) - { - case "continue": - RunningState.IsRunning = true; - RunningState.MinutesLeft = 0; - RunningState.SecondsLeft = 0; - break; - case "5minutes": - RunningState.MinutesLeft = 5; - RunningState.SecondsLeft = 0; - RunningState.IsRunning = true; - break; - } + rootFrame.Navigate(typeof(MainPage), args["action"]); + } else + { + rootFrame.Navigate(typeof(MainPage), null); } - rootFrame.Navigate(typeof(MainPage), RunningState); Window.Current.Activate(); } } diff --git a/PomoTime/MainPage.xaml.cs b/PomoTime/MainPage.xaml.cs index aa3d331..aec02e6 100644 --- a/PomoTime/MainPage.xaml.cs +++ b/PomoTime/MainPage.xaml.cs @@ -260,7 +260,7 @@ namespace PomoTime if (MainViewRunningState.MinutesLeft != 0 || MainViewRunningState.SecondsLeft != 0) { - SchedulePeriodOverNotification(); + RescheduleNotification(); } } @@ -269,7 +269,7 @@ namespace PomoTime AppBarButton b = sender as AppBarButton; MainViewRunningState.IsRunning = false; - ClearScheduledNotifications(); + RescheduleNotification(); } private void Reset() @@ -280,7 +280,7 @@ namespace PomoTime MainViewRunningState.MinutesLeft = WorkMinutes; MainViewRunningState.SecondsLeft = 0; - ClearScheduledNotifications(); + RescheduleNotification(); } private void ResetButton_Click(object sender, RoutedEventArgs e) @@ -421,23 +421,61 @@ namespace PomoTime StopTimer(); } + private void LoadRunningState() + { + ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; + if (localSettings.Values["MinutesLeft"] != null) + { + MainViewRunningState.MinutesLeft = (int)localSettings.Values["MinutesLeft"]; + MainViewRunningState.SecondsLeft = (int)localSettings.Values["SecondsLeft"]; + MainViewRunningState.IsRunning = (bool)localSettings.Values["IsRunning"]; + MainViewRunningState.PreviousShortBreaks = (int)localSettings.Values["PreviousShortBreaks"]; + MainViewRunningState.CurrentPeriod = (Period)localSettings.Values["CurrentPeriod"]; + } + } + protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); - - MainViewRunningState = (RunningState)e.Parameter; + string action = (string)e.Parameter; + StopTimer(); + ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; if (localSettings.Values["SuspendTime"] != null) { SuspendTime = new DateTime((long)localSettings.Values["SuspendTime"]); - FastForwardTime(SuspendTime); + LoadRunningState(); + + if(action != null) + { + switch (action) + { + case "5minutes": + MainViewRunningState.MinutesLeft = 5; + MainViewRunningState.SecondsLeft = 0; + MainViewRunningState.IsRunning = true; + break; + case "continue": + MainViewRunningState.MinutesLeft = 0; + MainViewRunningState.SecondsLeft = 0; + // Onto the next period + PlusSecond(); + MainViewRunningState.IsRunning = true; + break; + + } + } else + { + FastForwardTime(SuspendTime); + } } else { Reset(); SaveLocalState(); } + StartTimer(); RescheduleNotification(); }