From bae0b2f5ff1b9adc3c86c5149da79e9830fbcf6b Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 22:39:45 -0400 Subject: [PATCH 1/8] Implement low battery alert dialog --- Battery Time Remaining/AppDelegate.m | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index e4b87d9..2f89517 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -378,6 +378,10 @@ - (void)updateStatusItem { [self notify:NSLocalizedString(@"Battery Time Remaining", "Battery Time Remaining notification") message:[NSString stringWithFormat:NSLocalizedString(@"%1$ld:%2$02ld left (%3$ld%%)", @"Time remaining left notification"), hour, minute, self.currentPercent]]; } + // YATHARTH + if (self.currentPercent == 10) { + [self showLowBatteryDialog]; + } self.previousPercent = self.currentPercent; } } @@ -388,6 +392,17 @@ - (void)updateStatusItem CFRelease(psBlob); } +- (void)showLowBatteryDialog +{ + // TODO: Add setting + // TODO: Internationalize + NSAlert *alert = [[NSAlert alloc] init]; + [alert setAlertStyle:NSWarningAlertStyle]; + [alert setMessageText:@"Low Battery"]; + [alert setInformativeText:@"Please connect your computer to a charger"]; + [alert runModal]; +} + - (void)updateStatusItemMenu { [self updateStatusItem]; From 779264348a8a3bafe03a43631af0918767abac81 Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 22:43:06 -0400 Subject: [PATCH 2/8] Add easy way to summon dialog for testing --- Battery Time Remaining/AppDelegate.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index 2f89517..c0de188 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -295,6 +295,8 @@ - (void)updateStatusItem unpluggedTimer = nil; } previousState = psState; + + [self showLowBatteryDialog]; } // Still calculating the estimated time remaining... From bec203b9839bf696c930126a0b5f36d31cf271ec Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 23:10:52 -0400 Subject: [PATCH 3/8] Add setting for low battery dialog --- Battery Time Remaining/AppDelegate.h | 1 + Battery Time Remaining/AppDelegate.m | 42 ++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Battery Time Remaining/AppDelegate.h b/Battery Time Remaining/AppDelegate.h index ac0cfae..04522d2 100644 --- a/Battery Time Remaining/AppDelegate.h +++ b/Battery Time Remaining/AppDelegate.h @@ -27,6 +27,7 @@ #define kBTRMenuEnergySaverSetting 14 #define kBTRMenuUpdater 15 #define kBTRMenuQuitKey 16 +#define kBTRMenuLowBatteryDialog 17 #endif diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index c0de188..837bc0b 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -48,6 +48,7 @@ @interface AppDelegate () BOOL showPercentage; BOOL hideIcon; BOOL hideTime; + BOOL showLowBatteryDialog; } - (void)cacheBatteryIcon; @@ -167,6 +168,14 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification hideTime = [[NSUserDefaults standardUserDefaults] boolForKey:@"hideTime"]; hideTimeSubmenuItem.state = (hideTime) ? NSOnState : NSOffState; + // Low battery dialog item + NSMenuItem *showLowBatteryAlertSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Show low battery dialog", @"Show low battery dialog setting") action:@selector(toggleShowLowBatteryDialog:) keyEquivalent:@""]; + [showLowBatteryAlertSubmenuItem setTag:kBTRMenuLowBatteryDialog]; + showLowBatteryAlertSubmenuItem.target = self; + showLowBatteryDialog = [[NSUserDefaults standardUserDefaults] boolForKey:@"lowbatterydialog"]; + showLowBatteryAlertSubmenuItem.state = (showLowBatteryDialog) ? NSOnState : NSOffState; + + // Build the setting submenu NSMenu *settingSubmenu = [[NSMenu alloc] initWithTitle:@"Setting Menu"]; [settingSubmenu addItem:advancedSubmenuItem]; @@ -175,6 +184,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification [settingSubmenu addItem:percentageSubmenuItem]; [settingSubmenu addItem:hideIconSubmenuItem]; [settingSubmenu addItem:hideTimeSubmenuItem]; + [settingSubmenu addItem:showLowBatteryAlertSubmenuItem]; + // Settings menu item NSMenuItem *settingMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Settings", @"Settings menuitem") action:nil keyEquivalent:@""]; @@ -380,7 +391,7 @@ - (void)updateStatusItem { [self notify:NSLocalizedString(@"Battery Time Remaining", "Battery Time Remaining notification") message:[NSString stringWithFormat:NSLocalizedString(@"%1$ld:%2$02ld left (%3$ld%%)", @"Time remaining left notification"), hour, minute, self.currentPercent]]; } - // YATHARTH + if (self.currentPercent == 10) { [self showLowBatteryDialog]; } @@ -394,10 +405,35 @@ - (void)updateStatusItem CFRelease(psBlob); } +- (void)toggleShowLowBatteryDialog:(id)sender +{ + NSMenuItem *item = sender; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + if ([defaults boolForKey:@"lowbatterydialog"]) + { + item.state = NSOffState; + showLowBatteryDialog = NO; + [defaults setBool:NO forKey:@"lowbatterydialog"]; + } + else + { + item.state = NSOnState; + showLowBatteryDialog = YES; + [defaults setBool:YES forKey:@"lowbatterydialog"]; + } + [defaults synchronize]; + + [self updateStatusItem]; +} + +// TODO: Rename related code from 'low battery dialog' to 'critical battery alert' +// TODO: Reorder code and GUI - (void)showLowBatteryDialog { - // TODO: Add setting - // TODO: Internationalize + if (!showLowBatteryDialog) { + return; + } NSAlert *alert = [[NSAlert alloc] init]; [alert setAlertStyle:NSWarningAlertStyle]; [alert setMessageText:@"Low Battery"]; From 2aff36bb3f5187220f0d12f886107bd24d13a085 Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 23:36:50 -0400 Subject: [PATCH 4/8] Rename and reorganize code --- Battery Time Remaining/AppDelegate.h | 34 +++++----- Battery Time Remaining/AppDelegate.m | 93 ++++++++++++++-------------- 2 files changed, 63 insertions(+), 64 deletions(-) diff --git a/Battery Time Remaining/AppDelegate.h b/Battery Time Remaining/AppDelegate.h index 04522d2..bdbce59 100644 --- a/Battery Time Remaining/AppDelegate.h +++ b/Battery Time Remaining/AppDelegate.h @@ -11,23 +11,23 @@ #ifndef _BTR_MENU #define _BTR_MENU -#define kBTRMenuPowerSourcePercent 1 -#define kBTRMenuPowerSourceState 2 -#define kBTRMenuPowerSourceAdvanced 3 -#define kBTRMenuStartAtLogin 4 -#define kBTRMenuNotification 5 -#define kBTRMenuSetting 6 -#define kBTRMenuAdvanced 7 -#define kBTRMenuParenthesis 8 -#define kBTRMenuFahrenheit 9 -#define kBTRMenuPercentage 10 -#define kBTRMenuWhiteText 11 -#define kBTRMenuHideIcon 12 -#define kBTRMenuHideTime 13 -#define kBTRMenuEnergySaverSetting 14 -#define kBTRMenuUpdater 15 -#define kBTRMenuQuitKey 16 -#define kBTRMenuLowBatteryDialog 17 +#define kBTRMenuPowerSourcePercent 1 +#define kBTRMenuPowerSourceState 2 +#define kBTRMenuPowerSourceAdvanced 3 +#define kBTRMenuStartAtLogin 4 +#define kBTRMenuNotification 5 +#define kBTRMenuSetting 6 +#define kBTRMenuAdvanced 7 +#define kBTRMenuParenthesis 8 +#define kBTRMenuFahrenheit 9 +#define kBTRMenuPercentage 10 +#define kBTRMenuWhiteText 11 +#define kBTRMenuHideIcon 12 +#define kBTRMenuHideTime 13 +#define kBTRMenuEnergySaverSetting 14 +#define kBTRMenuUpdater 15 +#define kBTRMenuQuitKey 16 +#define kBTRMenuCriticalBatteryAlert 17 #endif diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index 837bc0b..fe36c3d 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -48,7 +48,7 @@ @interface AppDelegate () BOOL showPercentage; BOOL hideIcon; BOOL hideTime; - BOOL showLowBatteryDialog; + BOOL enableCriticalBatteryAlert; } - (void)cacheBatteryIcon; @@ -132,6 +132,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification advancedSubmenuItem.target = self; advancedSubmenuItem.state = ([[NSUserDefaults standardUserDefaults] boolForKey:@"advanced"]) ? NSOnState : NSOffState; [advancedSubmenuItem setHidden:!self.advancedSupported]; + + // Critical battery alert item + NSMenuItem *enableCriticalBatteryAlertSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Enable critical battery alert", @"Enable critical battery alert/dialog setting") action:@selector(toggleEnableCriticalBatteryAlert:) keyEquivalent:@""]; + [enableCriticalBatteryAlertSubmenuItem setTag:kBTRMenuCriticalBatteryAlert]; + enableCriticalBatteryAlertSubmenuItem.target = self; + enableCriticalBatteryAlert = [[NSUserDefaults standardUserDefaults] boolForKey:@"criticalBatteryAlert"]; + enableCriticalBatteryAlertSubmenuItem.state = (enableCriticalBatteryAlert) ? NSOnState : NSOffState; // Time display control menu item NSMenuItem *parenthesisSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Display time with parentheses", @"Display time with parentheses setting") action:@selector(toggleParenthesis:) keyEquivalent:@""]; @@ -168,23 +175,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification hideTime = [[NSUserDefaults standardUserDefaults] boolForKey:@"hideTime"]; hideTimeSubmenuItem.state = (hideTime) ? NSOnState : NSOffState; - // Low battery dialog item - NSMenuItem *showLowBatteryAlertSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Show low battery dialog", @"Show low battery dialog setting") action:@selector(toggleShowLowBatteryDialog:) keyEquivalent:@""]; - [showLowBatteryAlertSubmenuItem setTag:kBTRMenuLowBatteryDialog]; - showLowBatteryAlertSubmenuItem.target = self; - showLowBatteryDialog = [[NSUserDefaults standardUserDefaults] boolForKey:@"lowbatterydialog"]; - showLowBatteryAlertSubmenuItem.state = (showLowBatteryDialog) ? NSOnState : NSOffState; - // Build the setting submenu NSMenu *settingSubmenu = [[NSMenu alloc] initWithTitle:@"Setting Menu"]; [settingSubmenu addItem:advancedSubmenuItem]; + [settingSubmenu addItem:enableCriticalBatteryAlertSubmenuItem]; [settingSubmenu addItem:parenthesisSubmenuItem]; [settingSubmenu addItem:fahrenheitSubmenuItem]; [settingSubmenu addItem:percentageSubmenuItem]; [settingSubmenu addItem:hideIconSubmenuItem]; [settingSubmenu addItem:hideTimeSubmenuItem]; - [settingSubmenu addItem:showLowBatteryAlertSubmenuItem]; // Settings menu item @@ -307,7 +307,7 @@ - (void)updateStatusItem } previousState = psState; - [self showLowBatteryDialog]; + [self showCriticalBatteryAlert]; } // Still calculating the estimated time remaining... @@ -393,7 +393,7 @@ - (void)updateStatusItem } if (self.currentPercent == 10) { - [self showLowBatteryDialog]; + [self showCriticalBatteryAlert]; } self.previousPercent = self.currentPercent; } @@ -405,42 +405,6 @@ - (void)updateStatusItem CFRelease(psBlob); } -- (void)toggleShowLowBatteryDialog:(id)sender -{ - NSMenuItem *item = sender; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - - if ([defaults boolForKey:@"lowbatterydialog"]) - { - item.state = NSOffState; - showLowBatteryDialog = NO; - [defaults setBool:NO forKey:@"lowbatterydialog"]; - } - else - { - item.state = NSOnState; - showLowBatteryDialog = YES; - [defaults setBool:YES forKey:@"lowbatterydialog"]; - } - [defaults synchronize]; - - [self updateStatusItem]; -} - -// TODO: Rename related code from 'low battery dialog' to 'critical battery alert' -// TODO: Reorder code and GUI -- (void)showLowBatteryDialog -{ - if (!showLowBatteryDialog) { - return; - } - NSAlert *alert = [[NSAlert alloc] init]; - [alert setAlertStyle:NSWarningAlertStyle]; - [alert setMessageText:@"Low Battery"]; - [alert setInformativeText:@"Please connect your computer to a charger"]; - [alert runModal]; -} - - (void)updateStatusItemMenu { [self updateStatusItem]; @@ -759,6 +723,28 @@ - (void)toggleAdvanced:(id)sender [self updateStatusItem]; } +- (void)toggleEnableCriticalBatteryAlert:(id)sender +{ + NSMenuItem *item = sender; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + if ([defaults boolForKey:@"criticalBatteryAlert"]) + { + item.state = NSOffState; + enableCriticalBatteryAlert = NO; + [defaults setBool:NO forKey:@"criticalBatteryAlert"]; + } + else + { + item.state = NSOnState; + enableCriticalBatteryAlert = YES; + [defaults setBool:YES forKey:@"criticalBatteryAlert"]; + } + [defaults synchronize]; + + [self updateStatusItem]; +} + - (void)toggleParenthesis:(id)sender { NSMenuItem *item = sender; @@ -884,6 +870,19 @@ - (void)notify:(NSString *)title message:(NSString *)message [center scheduleNotification:notification]; } +- (void)showCriticalBatteryAlert +{ + if (!enableCriticalBatteryAlert) { + return; + } + + NSAlert *alert = [[NSAlert alloc] init]; + [alert setAlertStyle:NSWarningAlertStyle]; + [alert setMessageText:@"Critical Battery"]; + [alert setInformativeText:@"Please connect your computer to a charger."]; + [alert runModal]; +} + - (void)loadNotificationSetting { // Fetch user settings for notifications From 9c272200f642d5cb2bcad5ad0a8c361060d26023 Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 23:39:33 -0400 Subject: [PATCH 5/8] Extract magic number --- Battery Time Remaining/AppDelegate.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index fe36c3d..8786790 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -23,6 +23,9 @@ // exact same look. #define EXTRA_TOP_OFFSET 2.0f +// This value is used by the critical battery alert. +#define CRITICAL_BATTERY 10 + // IOPS notification callback on power source change static void PowerSourceChanged(void * context) { @@ -392,7 +395,7 @@ - (void)updateStatusItem [self notify:NSLocalizedString(@"Battery Time Remaining", "Battery Time Remaining notification") message:[NSString stringWithFormat:NSLocalizedString(@"%1$ld:%2$02ld left (%3$ld%%)", @"Time remaining left notification"), hour, minute, self.currentPercent]]; } - if (self.currentPercent == 10) { + if (self.currentPercent == CRITICAL_BATTERY) { [self showCriticalBatteryAlert]; } self.previousPercent = self.currentPercent; From c88bd335e803bc345a1f85ed005651e05b08f3a4 Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 23:40:07 -0400 Subject: [PATCH 6/8] Remove testing code --- Battery Time Remaining/AppDelegate.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index 8786790..e1ea3b7 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -309,8 +309,6 @@ - (void)updateStatusItem unpluggedTimer = nil; } previousState = psState; - - [self showCriticalBatteryAlert]; } // Still calculating the estimated time remaining... From 6c977f4c09fb1046a79f0fbd3a136ad6df4fcb3b Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Sun, 10 Jul 2016 23:42:03 -0400 Subject: [PATCH 7/8] Add/delete empty lines --- Battery Time Remaining/AppDelegate.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index e1ea3b7..756730e 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -178,7 +178,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification hideTime = [[NSUserDefaults standardUserDefaults] boolForKey:@"hideTime"]; hideTimeSubmenuItem.state = (hideTime) ? NSOnState : NSOffState; - // Build the setting submenu NSMenu *settingSubmenu = [[NSMenu alloc] initWithTitle:@"Setting Menu"]; [settingSubmenu addItem:advancedSubmenuItem]; @@ -189,7 +188,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification [settingSubmenu addItem:hideIconSubmenuItem]; [settingSubmenu addItem:hideTimeSubmenuItem]; - // Settings menu item NSMenuItem *settingMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Settings", @"Settings menuitem") action:nil keyEquivalent:@""]; [settingMenu setTag:kBTRMenuSetting]; @@ -396,6 +394,7 @@ - (void)updateStatusItem if (self.currentPercent == CRITICAL_BATTERY) { [self showCriticalBatteryAlert]; } + self.previousPercent = self.currentPercent; } } From 8a9ed04f5eb6a7d8e1989c41aa37292937a0865e Mon Sep 17 00:00:00 2001 From: Yatharth Agarwal Date: Mon, 11 Jul 2016 16:25:41 -0400 Subject: [PATCH 8/8] Wrap strings with NSLocalizedString --- Battery Time Remaining/AppDelegate.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index 756730e..c786681 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -878,8 +878,8 @@ - (void)showCriticalBatteryAlert NSAlert *alert = [[NSAlert alloc] init]; [alert setAlertStyle:NSWarningAlertStyle]; - [alert setMessageText:@"Critical Battery"]; - [alert setInformativeText:@"Please connect your computer to a charger."]; + [alert setMessageText:NSLocalizedString(@"Critical Battery",@"Title of alert")]; + [alert setInformativeText:NSLocalizedString(@"Please connect your computer to a charger.","Body of critical battery alert")]; [alert runModal]; }