Explorar o código

Factorize idle timer handling

Felix Paul Kühne %!s(int64=12) %!d(string=hai) anos
pai
achega
086b4fef0e

+ 2 - 0
AspenProject/VLCAppDelegate.h

@@ -16,6 +16,8 @@
 @interface VLCAppDelegate : UIResponder <UIApplicationDelegate>
 
 - (void)updateMediaList;
+- (void)disableIdleTimer;
+- (void)activateIdleTimer;
 
 @property (nonatomic, readonly) VLCPlaylistViewController *playlistViewController;
 @property (nonatomic, readonly) VLCDropboxTableViewController *dropboxTableViewController;

+ 16 - 0
AspenProject/VLCAppDelegate.m

@@ -23,6 +23,7 @@
 @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
     PAPasscodeViewController *_passcodeLockController;
     VLCDropboxTableViewController *_dropboxTableViewController;
+    int _idleCounter;
 }
 
 @property (nonatomic) BOOL passcodeValidated;
@@ -249,4 +250,19 @@
     // TODO handle error attempts
 }
 
+#pragma mark - idle timer preventer
+- (void)disableIdleTimer
+{
+    _idleCounter++;
+    if ([UIApplication sharedApplication].idleTimerDisabled == NO)
+        [UIApplication sharedApplication].idleTimerDisabled = YES;
+}
+
+- (void)activateIdleTimer
+{
+    _idleCounter--;
+    if (_idleCounter < 1)
+        [UIApplication sharedApplication].idleTimerDisabled = NO;
+}
+
 @end

+ 2 - 2
AspenProject/VLCDropboxController.m

@@ -172,7 +172,7 @@
     _outstandingNetworkRequests++;
     if (_outstandingNetworkRequests == 1) {
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
-        [UIApplication sharedApplication].idleTimerDisabled = YES;
+        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
     }
 }
 
@@ -181,7 +181,7 @@
     _outstandingNetworkRequests--;
     if (_outstandingNetworkRequests == 0) {
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
-        [UIApplication sharedApplication].idleTimerDisabled = NO;
+        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
     }
 }
 

+ 2 - 2
AspenProject/VLCHTTPFileDownloader.m

@@ -44,7 +44,7 @@
     } else {
         _downloadInProgress = YES;
         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
-        [UIApplication sharedApplication].idleTimerDisabled = YES;
+        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
     }
 }
 
@@ -129,7 +129,7 @@
 {
     _downloadInProgress = NO;
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
-    [UIApplication sharedApplication].idleTimerDisabled = NO;
+    [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
 
     [self.delegate downloadEnded];
 }

+ 2 - 2
AspenProject/VLCHTTPUploaderController.m

@@ -284,7 +284,7 @@ static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE
         storeFile = [NSFileHandle fileHandleForWritingAtPath:filePath];
         [uploadedFiles addObject: [NSString stringWithFormat:@"/upload/%@", filename]];
         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
-        [UIApplication sharedApplication].idleTimerDisabled = YES;
+        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
     }
 }
 
@@ -302,7 +302,7 @@ static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE
     [storeFile closeFile];
     storeFile = nil;
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
-    [UIApplication sharedApplication].idleTimerDisabled = NO;
+    [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
 
     /* update media library when file upload was completed */
     VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;