From 702785868e473602ec2c8cf0b6ce02f7ec8eee70 Mon Sep 17 00:00:00 2001 From: Nikolai Voronin Date: Fri, 3 Mar 2023 00:13:00 +0300 Subject: [PATCH] Unify updating of notify icon --- Xm4Battery/Program.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Xm4Battery/Program.cs b/Xm4Battery/Program.cs index 5be5d7d..01d97ce 100644 --- a/Xm4Battery/Program.cs +++ b/Xm4Battery/Program.cs @@ -121,15 +121,9 @@ private static Icon CreateLevelIcon( int level = -1 ) private static void Xm4state_BatteryLevelChanged( object? sender, int level ) { var xm4 = sender as Xm4Entity; - - var lastConnectedTime = string.Empty; var connected = xm4?.IsConnected ?? false; - if ( connected ) - _notifyIcon.Icon = CreateLevelIcon( level ); - else - lastConnectedTime = $"\n{xm4!.LastConnectedTime.Value:F}"; - _notifyIcon.Text = $"{AppName} {level}%{lastConnectedTime}"; + UpdateNotifyIcon( xm4!, connected, level ); } private static void Xm4state_ConnectionChanged( object? sender, bool connected ) @@ -137,15 +131,23 @@ private static void Xm4state_ConnectionChanged( object? sender, bool connected ) var xm4 = sender as Xm4Entity; var level = xm4?.BatteryLevel ?? 0; + UpdateNotifyIcon( xm4!, connected, level ); + } + + private static void UpdateNotifyIcon( + Xm4Entity xm4, + bool connected, + int level ) + { _notifyIcon.Icon = connected ? CreateLevelIcon( level ) : _disconnectIcon; - var lastConnectedTime = + var at = connected ? string.Empty : $"\n{xm4!.LastConnectedTime.Value:F}"; - _notifyIcon.Text = $"{AppName} {level}%{lastConnectedTime}"; + _notifyIcon.Text = $"{AppName} {level}%{at}"; } const string AppName = "XM4 Battery Level";