From 1cfcd084e7375ce69bafade380e6f9d48c40478d Mon Sep 17 00:00:00 2001 From: Moedrian Date: Wed, 18 Dec 2024 20:59:22 +0800 Subject: [PATCH] add off time reminder and restore interval get/set sentences --- src/Tomato/MainWindow.xaml.cs | 75 +++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/src/Tomato/MainWindow.xaml.cs b/src/Tomato/MainWindow.xaml.cs index 3564ec4..210863b 100644 --- a/src/Tomato/MainWindow.xaml.cs +++ b/src/Tomato/MainWindow.xaml.cs @@ -23,6 +23,8 @@ public partial class MainWindow : Window private TimeSpan _ctrDwnInterval = TimeSpan.Zero; private Timer? _timerCtrDwn; + private Timer? _offReminderTimer; + private const string DismissButtonArgKey = "action"; private const string DismissButtonArgValue = "start"; @@ -48,6 +50,8 @@ public MainWindow() AddEvents(); Start(); + + StartOffTimer(); } private void InitializeIcon() @@ -177,27 +181,19 @@ private void Reset() { var cfg = TomatoConfig.Deserialize(); - //IntervalBox.SelectedIndex = cfg.Interval / 5 - 1; + IntervalBox.SelectedIndex = cfg.Interval / 5 - 1; HourBox.SelectedIndex = cfg.OffTimeHour; MinuteBox.SelectedIndex = cfg.OffTimeMinute; _ctrDwnInterval = TimeSpan.FromMinutes(cfg.Interval); } - public static ToastContentBuilder GetToastContentBuilder() + public static ToastContentBuilder GetToastContentBuilder(string title, string text) { - const string title = "Tomato Clock Is Here."; - const string text = "Get up to drink some water!"; - var msg = text + Environment.NewLine + CalculateOff(); - var builder = new ToastContentBuilder() .AddText(title) - .AddText(msg) - .SetToastScenario(ToastScenario.Reminder) - .AddButton(new ToastButton() - .SetContent("Start Another Tomato") - .AddArgument(DismissButtonArgKey, DismissButtonArgValue) - .SetBackgroundActivation()); + .AddText(text) + .SetToastScenario(ToastScenario.Reminder); if (File.Exists(TomatoConfig.GetTomatoPicture())) builder.AddAppLogoOverride(new Uri("file:///" + TomatoConfig.GetTomatoPicture()), @@ -208,12 +204,52 @@ public static ToastContentBuilder GetToastContentBuilder() private void DisplayTomatoNotification() { + const string title = "Tomato Clock Is Here."; + const string text = "Get up to drink some water!"; + var msg = text + Environment.NewLine + FormatOff(); + if (!_toastOn) - GetToastContentBuilder().Show(); + GetToastContentBuilder(title, msg) + .AddButton(new ToastButton() + .SetBackgroundActivation() + .SetContent("Start Another Tomato") + .AddArgument(DismissButtonArgKey, DismissButtonArgValue)) + .Show(); _toastOn = true; } + private bool _offReminderRaised; + private void StartOffTimer() + { + var remindTs = TimeSpan.FromMinutes(5); + _offReminderTimer = new Timer(TimeSpan.FromSeconds(30)); + _offReminderTimer.Elapsed += delegate + { + var ts = CalculateOff(); + + if (ts < TimeSpan.Zero) + { + _offReminderRaised = false; + return; + } + + if (!_offReminderRaised) + { + if (ts < remindTs && ts > TimeSpan.Zero) + { + GetToastContentBuilder("Time to prepare off!", "Go Go Go") + .AddButton(new ToastButtonDismiss("OKK")) + .Show(); + + _offReminderRaised = true; + } + } + }; + + _offReminderTimer.Start(); + } + private void Start() { var ts = TimeSpan.FromSeconds(1); @@ -248,13 +284,13 @@ private void StoreUserConfig() { try { - //var interval = int.Parse(IntervalBox.SelectedItem as string ?? throw new Exception("Interval parsing error.")); + var interval = int.Parse(IntervalBox.SelectedItem as string ?? throw new Exception("Interval parsing error.")); var hour = int.Parse(HourBox.SelectedItem as string ?? throw new Exception("Hour parsing error.")); var minute = int.Parse(MinuteBox.SelectedItem as string ?? throw new Exception("Minute parsing error.")); var cfg = new TomatoConfig { - //Interval = interval, + Interval = interval, OffTimeHour = hour, OffTimeMinute = minute }; @@ -272,7 +308,7 @@ private string GetCtrDwnString() return $"{_ctrDwnInterval.Hours:00}:{_ctrDwnInterval.Minutes:00}:{_ctrDwnInterval.Seconds:00}"; } - private static string CalculateOff() + private static TimeSpan CalculateOff() { var cfg = TomatoConfig.Deserialize(); var now = DateTime.Now; @@ -280,6 +316,13 @@ private static string CalculateOff() var timeSpan = off - now; + return timeSpan; + } + + private static string FormatOff() + { + var timeSpan = CalculateOff(); + if (timeSpan <= TimeSpan.Zero) return "OFF NOW."; var totalHours = timeSpan.TotalHours;