Skip to content

Commit

Permalink
hotfix: Updater version parsing bug, Updater delay, Better baloontip …
Browse files Browse the repository at this point in the history
…click handling
  • Loading branch information
christiankyle-ching committed Feb 3, 2022
1 parent e978484 commit 3be50e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
22 changes: 13 additions & 9 deletions DigitalWellbeingWPF/Helpers/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static class Notifier
private static int NOTIFICATION_TIMOUT_SECONDS = 10;
private static int CHECK_INTERVAL = 130;

private static EventHandler defaultNotificationHandler;

static Notifier()
{
trayIcon = new System.Windows.Forms.NotifyIcon();
Expand Down Expand Up @@ -47,28 +49,30 @@ static Notifier()
mWindow.Close();
});

// Always visible for notifications
//trayIcon.Visible = true;
// Always visible for notifications to work
trayIcon.Visible = true;
}

public static void ShowNotification(string title, string message, System.Windows.Forms.ToolTipIcon icon = System.Windows.Forms.ToolTipIcon.None)
public static void ShowNotification(string title, string message, EventHandler clickHandler = null, System.Windows.Forms.ToolTipIcon icon = System.Windows.Forms.ToolTipIcon.None)
{
trayIcon.BalloonTipTitle = title;
trayIcon.BalloonTipText = message;
trayIcon.BalloonTipIcon = icon;

trayIcon.BalloonTipClicked += clickHandler ?? defaultNotificationHandler;

trayIcon.ShowBalloonTip(NOTIFICATION_TIMOUT_SECONDS * 1000);
}

public static void ShowTrayIcon(EventHandler handler)
public static void SetDoubleClickHandler(EventHandler doubleClickHandler)
{
trayIcon.DoubleClick += handler;
trayIcon.BalloonTipClicked += handler;
trayIcon.Visible = true;
trayIcon.DoubleClick += doubleClickHandler;
}

public static void HideTrayIcon()
public static void SetDefaultNotificationHandler(EventHandler baloonTipHandlerClick)
{
trayIcon.Visible = false;
defaultNotificationHandler = baloonTipHandlerClick;
trayIcon.BalloonTipClicked += baloonTipHandlerClick;
}

#region App Time Limit Checker
Expand Down
2 changes: 1 addition & 1 deletion DigitalWellbeingWPF/Helpers/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static int ParseVersion(string strVersion)

if (strVersion == string.Empty) return version;

strVersion = Regex.Replace(strVersion, "[^0-9.]", ""); // Remove all non-numbers and period
strVersion = Regex.Replace(strVersion, "[^0-9]", ""); // Remove all non-numbers

if (int.TryParse(strVersion, out version))
{
Expand Down
5 changes: 3 additions & 2 deletions DigitalWellbeingWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public MainWindow()

// Init Notifier
Notifier.InitNotifierTimer();
// Set Default Click Handler for any Notification
Notifier.SetDefaultNotificationHandler((s, e) => RestoreWindow());

// Check Autorun File
InitAutoRun();
Expand Down Expand Up @@ -101,14 +103,13 @@ private void Window_StateChanged(object sender, EventArgs e)
public void MinimizeToTray()
{
this.Hide();
Notifier.ShowTrayIcon((s, e) => RestoreWindow());
Notifier.SetDoubleClickHandler((s, e) => RestoreWindow());
}

public void RestoreWindow()
{
this.Show();
this.WindowState = WindowState.Normal;
Notifier.HideTrayIcon();

// Trigger refresh
usagePage.OnNavigate();
Expand Down
12 changes: 11 additions & 1 deletion DigitalWellbeingWPF/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace DigitalWellbeingWPF.Views
public partial class SettingsPage : Page
{
private readonly ApplicationTheme? systemTheme;
private int UPDATE_CHECK_DELAY = 20;

public SettingsPage()
{
Expand Down Expand Up @@ -215,13 +216,22 @@ private void LoadAboutApp()

private async void CheckForUpdates()
{
await Task.Delay(UPDATE_CHECK_DELAY * 1000);

strLatestVersion = await Updater.GetLatestVersion();
latestVersion = Updater.ParseVersion(strLatestVersion);

if (Updater.IsUpdateAvailable(currentVersion, latestVersion))
{
TxtLatestVersion.Text = $" (Update Available {strLatestVersion})";
Notifier.ShowNotification(App.APPNAME, $"Update Available: {strLatestVersion}");
Notifier.ShowNotification(
App.APPNAME,
$"Update Available: {strLatestVersion}",
(s, e) =>
{
(Application.Current.MainWindow as MainWindow).GoToSettings();
}
);
}
}

Expand Down

0 comments on commit 3be50e9

Please sign in to comment.