Permalink
Browse files

Poll for power source changes and display red window

  • Loading branch information...
1 parent 3b3fa0a commit 9e16e5d81cc742eb73c0abc95fe986902840026d @MSch committed Dec 11, 2011
Showing with 28 additions and 7 deletions.
  1. +20 −7 Sources/BDAppDelegate.m
  2. +1 −0 Sources/BDWindow.h
  3. +7 −0 Sources/BDWindow.m
View
@@ -1,16 +1,29 @@
#import "BDAppDelegate.h"
#import "BDPowerInfo.h"
-@implementation BDAppDelegate
+static NSInteger const BDChargeThreshold = 5;
+static NSInteger const BDPollingInterval = 5;
+
+@interface BDAppDelegate ()
+- (void)checkForLowCharge;
+@end
+
+@implementation BDAppDelegate {
+ NSTimer *timer;
+}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- [BDWindow show];
- [BDWindow performSelector:@selector(hide) withObject:nil afterDelay:2];
-
+ timer = [NSTimer scheduledTimerWithTimeInterval:BDPollingInterval
+ target:self
+ selector:@selector(checkForLowCharge)
+ userInfo:nil
+ repeats:YES];
+ [self checkForLowCharge];
+}
+
+- (void)checkForLowCharge {
BDPowerInfo *powerInfo = [BDPowerInfo sharedPowerInfo];
- NSLog(@"%ld", powerInfo.remainingCharge);
- NSLog(@"%d", powerInfo.chargingBattery);
- NSLog(@"%d", powerInfo.runningOnBattery);
+ [BDWindow setVisible:(powerInfo.runningOnBattery && !powerInfo.chargingBattery && (powerInfo.remainingCharge < BDChargeThreshold))];
}
@end
View
@@ -5,6 +5,7 @@
@property (nonatomic, strong) BDView *contentView;
++ (void)setVisible:(BOOL)visible;
+ (void)show;
+ (void)hide;
View
@@ -29,6 +29,13 @@ - (id)init {
#pragma mark -
++ (void)setVisible:(BOOL)visible {
+ if (visible)
+ [self show];
+ else
+ [self hide];
+}
+
+ (void)show {
if (sharedWindow)
return;

0 comments on commit 9e16e5d

Please sign in to comment.