浏览代码

let interface controller listen to db update notifications until it gets deallocated but defer updates while not active until it becomes active again

Tobias Conradi 10 年之前
父节点
当前提交
602692743e

+ 1 - 1
VLC for iOS WatchKit Extension/InterfaceController.h

@@ -16,7 +16,7 @@
 
 #import "VLCBaseInterfaceController.h"
 
-@interface InterfaceController : WKInterfaceController
+@interface InterfaceController : VLCBaseInterfaceController
 @property (weak, nonatomic) IBOutlet WKInterfaceButton *previousButton;
 @property (nonatomic, weak) IBOutlet WKInterfaceTable *table;
 @property (weak, nonatomic) IBOutlet WKInterfaceButton *nextButton;

+ 16 - 15
VLC for iOS WatchKit Extension/InterfaceController.m

@@ -34,6 +34,7 @@ typedef enum {
 @interface InterfaceController()
 @property (nonatomic, strong) VLCWatchTableController *tableController;
 @property (nonatomic) VLCLibraryMode libraryMode;
+@property (nonatomic) BOOL needsUpdate;
 @end
 
 @implementation InterfaceController
@@ -69,20 +70,17 @@ typedef enum {
 
 }
 
+- (void) dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
+}
+
 - (void)willActivate {
     // This method is called when watch view controller is about to be visible to user
     [super willActivate];
     NSLog(@"%s",__PRETTY_FUNCTION__);
-
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
-}
-
-- (void)didDeactivate {
-    // This method is called when watch view controller is no longer visible
-    [super didDeactivate];
-    NSLog(@"%s",__PRETTY_FUNCTION__);
-
-    [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
+    if (self.needsUpdate) {
+        [self updateData];
+    }
 }
 
 - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
@@ -105,7 +103,7 @@ typedef enum {
     [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
     [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
     [self addMenuItemWithImageNamed:@"TVShowsIcon" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
-    [self addMenuItemWithItemIcon:WKMenuItemIconMore title: NSLocalizedString(@"NOW_PLAYING", nil) action:@selector(showNowPlaying:)];
+    [self addNowPlayingMenu];
 }
 
 - (void)switchToAllFiles{
@@ -126,13 +124,16 @@ typedef enum {
     [self updateData];
 }
 
-- (void)showNowPlaying:(id)sender {
-    [self presentControllerWithName:@"nowPlaying" context:nil];
-}
 #pragma mark - data handling
 
 - (void)updateData {
-    self.tableController.objects = [self mediaArray];
+    // if not activated/visible we defer the update til activation
+    if (self.activated) {
+        self.tableController.objects = [self mediaArray];
+        self.needsUpdate = NO;
+    } else {
+        self.needsUpdate = YES;
+    }
 }
 
 - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {

+ 2 - 0
VLC for iOS WatchKit Extension/VLCBaseInterfaceController.h

@@ -13,7 +13,9 @@
 #import <WatchKit/WatchKit.h>
 
 @interface VLCBaseInterfaceController : WKInterfaceController
+@property (nonatomic, assign, readonly, getter=isActivated) BOOL activated;
 
+- (void)addNowPlayingMenu;
 - (void)showNowPlaying:(id)sender;
 
 @end

+ 13 - 0
VLC for iOS WatchKit Extension/VLCBaseInterfaceController.m

@@ -17,6 +17,9 @@
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
 
+}
+
+- (void)addNowPlayingMenu {
     [self addMenuItemWithItemIcon:WKMenuItemIconMore title: NSLocalizedString(@"NOW_PLAYING", nil) action:@selector(showNowPlaying:)];
 }
 
@@ -24,4 +27,14 @@
     [self presentControllerWithName:@"nowPlaying" context:nil];
 }
 
+
+- (void)willActivate {
+    [super willActivate];
+    _activated = YES;
+}
+- (void)didDeactivate {
+    [super didDeactivate];
+    _activated = NO;
+}
+
 @end

+ 2 - 0
VLC for iOS WatchKit Extension/VLCDetailInterfaceController.m

@@ -34,6 +34,8 @@
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
 
+    [self addNowPlayingMenu];
+    
     if ([context isKindOfClass:[MLFile class]]) {
         [self configureWithFile:context];
     }