private Task restoreStateFromDisk() { return(Task.Run(async() => { try { StorageFolder folder = ApplicationData.Current.LocalCacheFolder; StorageFile saveFile = await folder.GetFileAsync(_saveFileName); SaveStateModel savedState = null; using (Stream inputStream = await saveFile.OpenStreamForReadAsync()) using (StreamReader input = new StreamReader(inputStream)) { string json = input.ReadToEnd(); savedState = JsonConvert.DeserializeObject <SaveStateModel>(json); } TimeManager.Instance.Initialize(savedState); } catch (FileNotFoundException) { // Ignore because it just means that there was no save file present } catch (Exception e) { HockeyClient.Current.TrackException(e); } // Refresh the notification queue just to make sure everything is in sync. TimeManager.Instance.ScheduleNotifications(); })); }
private Task saveStateToDisk() { return(Task.Run(async() => { // Save the current mode information to disk SaveStateModel saveInfo = TimeManager.Instance.GetCurrentModeInfo(); // Not backed up to the cloud for now. Haven't thought through the details of that. StorageFolder folder = ApplicationData.Current.LocalCacheFolder; StorageFile saveFile = await folder.CreateFileAsync(_saveFileName, CreationCollisionOption.ReplaceExisting); using (Stream outputStream = await saveFile.OpenStreamForWriteAsync()) using (StreamWriter output = new StreamWriter(outputStream)) { output.Write(JsonConvert.SerializeObject(saveInfo)); } }).ContinueWith(completedTask => { // Do some error logging if the save task fails if (completedTask.IsFaulted) { HockeyClient.Current.TrackException(completedTask.Exception); } })); }
public void Initialize(SaveStateModel savedState) { Modes = savedState?.Modes ?? new List <ModeModel>(); if (!HasMultipleModes) { return; } DateTime now = DateTime.Now; State = savedState.TimerState; CurrentMode = savedState.CurrentMode; _timeRemainingInCurrentMode = savedState.TimeRemainingInCurrentMode; if (State == TimerState.Running) { // Use the saved state as a starting point and then figure out what mode we should currently be in based on the // elapsed time. ModeModel mode = savedState.CurrentMode; DateTime modeStartTime = savedState.CurrentModeStartTime; DateTime modeEndTime = modeStartTime + mode.TimeInMode; bool modeChangedSinceLastRun = false; while (modeEndTime < now) { modeStartTime = modeEndTime; mode = getNextMode(mode); modeEndTime = modeStartTime + mode.TimeInMode; modeChangedSinceLastRun = true; } _currentModeStart = modeStartTime; CurrentMode = mode; if (modeChangedSinceLastRun && now - savedState.LastRunDebugInfo.LastRunTime > TimeSpan.FromMinutes(30)) { // If we have not run in the past 30 min, then the user's computer was off. // In this case figure out if the mode has changed at all since the last notification and if so, // we will force a notification immediately to notify the user which mode they are in. XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01); IXmlNode textNode = toastXml.GetElementsByTagName("text")[0]; textNode.AppendChild(toastXml.CreateTextNode($"Time to {CurrentMode}! ({modeStartTime.ToString("t")})")); ToastNotification notification = new ToastNotification(toastXml); _toastNotifier.Show(notification); } } // This will finish initializing the _currentModeStart and the _timeRemainingInCurrentMode variables. GetTimeRemainingInCurrentMode(); }