Browse Source

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 years ago
parent
commit
602692743e

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

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

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

@@ -34,6 +34,7 @@ typedef enum {
 @interface InterfaceController()
 @interface InterfaceController()
 @property (nonatomic, strong) VLCWatchTableController *tableController;
 @property (nonatomic, strong) VLCWatchTableController *tableController;
 @property (nonatomic) VLCLibraryMode libraryMode;
 @property (nonatomic) VLCLibraryMode libraryMode;
+@property (nonatomic) BOOL needsUpdate;
 @end
 @end
 
 
 @implementation InterfaceController
 @implementation InterfaceController
@@ -69,20 +70,17 @@ typedef enum {
 
 
 }
 }
 
 
+- (void) dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
+}
+
 - (void)willActivate {
 - (void)willActivate {
     // This method is called when watch view controller is about to be visible to user
     // This method is called when watch view controller is about to be visible to user
     [super willActivate];
     [super willActivate];
     NSLog(@"%s",__PRETTY_FUNCTION__);
     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 {
 - (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:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
     [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
     [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
     [self addMenuItemWithImageNamed:@"TVShowsIcon" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
     [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{
 - (void)switchToAllFiles{
@@ -126,13 +124,16 @@ typedef enum {
     [self updateData];
     [self updateData];
 }
 }
 
 
-- (void)showNowPlaying:(id)sender {
-    [self presentControllerWithName:@"nowPlaying" context:nil];
-}
 #pragma mark - data handling
 #pragma mark - data handling
 
 
 - (void)updateData {
 - (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 {
 - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {

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

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

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

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

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

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