Преглед изворни кода

refresh all registered managed objects on update notification

Tobias Conradi пре 10 година
родитељ
комит
dfdb942dab

+ 19 - 0
VLC for iOS WatchKit Extension/NSManagedObjectContext+refreshAll.h

@@ -0,0 +1,19 @@
+/*****************************************************************************
+ * NSManagedObjectContext+refreshAll.h
+ * VLC for iOS
+ *****************************************************************************
+ * Copyright (c) 2015 VideoLAN. All rights reserved.
+ * $Id$
+ *
+ * Authors: Tobias Conradi <videolan # tobias-conradi.de>
+ *
+ * Refer to the COPYING file of the official project for license.
+ *****************************************************************************/
+
+
+#import <CoreData/CoreData.h>
+
+@interface NSManagedObjectContext (refreshAll)
+// mergeChanges has same sematics as - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag;
+- (void)vlc_refreshAllObjectsMerge:(BOOL)mergeChanges;
+@end

+ 22 - 0
VLC for iOS WatchKit Extension/NSManagedObjectContext+refreshAll.m

@@ -0,0 +1,22 @@
+/*****************************************************************************
+ * NSManagedObjectContext+refreshAll.m
+ * VLC for iOS
+ *****************************************************************************
+ * Copyright (c) 2015 VideoLAN. All rights reserved.
+ * $Id$
+ *
+ * Authors: Tobias Conradi <videolan # tobias-conradi.de>
+ *
+ * Refer to the COPYING file of the official project for license.
+ *****************************************************************************/
+
+#import "NSManagedObjectContext+refreshAll.h"
+
+@implementation NSManagedObjectContext (refreshAll)
+- (void)vlc_refreshAllObjectsMerge:(BOOL)mergeChanges
+{
+    for (NSManagedObject *object in self.registeredObjects) {
+        [self refreshObject:object mergeChanges:mergeChanges];
+    }
+}
+@end

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

@@ -18,4 +18,12 @@
 - (void)addNowPlayingMenu;
 - (void)showNowPlaying:(id)sender;
 
+
+// calls updataData if interface is currenlty active
+// otherwise it sets a flag so update data when the interface is activated
+- (void)setNeedsUpdateData;
+
+// actual update logic should be overwritten by subclasses that needs an update logic
+- (void)updateData;
+
 @end

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

@@ -12,13 +12,26 @@
 
 #import "VLCBaseInterfaceController.h"
 
+static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
+
+@interface VLCBaseInterfaceController()
+@property (nonatomic) BOOL needsUpdate;
+
+@end
+
 @implementation VLCBaseInterfaceController
 
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
 
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNeedsUpdateData) name:VLCDBUpdateNotification object:nil];
 }
 
+- (void) dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
+}
+
+
 - (void)addNowPlayingMenu {
     [self addMenuItemWithItemIcon:WKMenuItemIconMore title: NSLocalizedString(@"NOW_PLAYING", nil) action:@selector(showNowPlaying:)];
 }
@@ -31,10 +44,31 @@
 - (void)willActivate {
     [super willActivate];
     _activated = YES;
+
+    [self updateDataIfNeeded];
 }
 - (void)didDeactivate {
     [super didDeactivate];
     _activated = NO;
 }
 
+- (void)setNeedsUpdateData
+{
+    self.needsUpdate = YES;
+    [self updateDataIfNeeded];
+}
+- (void)updateDataIfNeeded
+{
+    // if not activated/visible we defer the update til activation
+    if (self.needsUpdate && self.activated) {
+        [self updateData];
+        self.needsUpdate = NO;
+    }
+}
+
+- (void)updateData
+{
+    self.needsUpdate = NO;
+}
+
 @end

+ 8 - 1
VLC for iOS WatchKit Extension/VLCDetailInterfaceController.m

@@ -49,6 +49,13 @@
     [super didDeactivate];
 }
 
+- (void)updateData {
+    [super updateData];
+    NSManagedObject *managedObject = self.managedObject;
+    [managedObject.managedObjectContext refreshObject:managedObject mergeChanges:NO];
+    [self configureWithFile:managedObject];
+}
+
 - (void)configureWithFile:(NSManagedObject *)managedObject {
     self.managedObject = managedObject;
 
@@ -78,7 +85,7 @@
 {
     UIImage *thumbnail = [VLCThumbnailsCache thumbnailForManagedObject:managedObject];
     if (thumbnail) {
-        [self.group setBackgroundImage:thumbnail];
+        [self.group performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:thumbnail waitUntilDone:NO];
     }
 }
 

+ 5 - 19
VLC for iOS WatchKit Extension/VLCPlaylistInterfaceController.m

@@ -21,6 +21,7 @@
 #import "VLCWatchTableController.h"
 #import "VLCThumbnailsCache.h"
 #import "WKInterfaceObject+VLCProgress.h"
+#import "NSManagedObjectContext+refreshAll.h"
 
 static NSString *const rowType = @"mediaRow";
 static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
@@ -41,7 +42,6 @@ typedef enum {
 
 @property (nonatomic, strong) VLCWatchTableController *tableController;
 @property (nonatomic) VLCLibraryMode libraryMode;
-@property (nonatomic) BOOL needsUpdate;
 @property (nonatomic) id groupObject;
 
 @end
@@ -75,7 +75,6 @@ typedef enum {
     }
 
     [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
 
     /* setup table view controller */
     VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
@@ -95,16 +94,9 @@ typedef enum {
     [self updateData];
 }
 
-- (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];
-    if (self.needsUpdate) {
-        [self updateData];
-    }
 }
 
 - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
@@ -155,13 +147,10 @@ typedef enum {
 #pragma mark - data handling
 
 - (void)updateData {
-    // 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;
-    }
+    [super updateData];
+    NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
+    [moc vlc_refreshAllObjectsMerge:NO];
+    self.tableController.objects = [self mediaArray];
 }
 
 - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
@@ -299,6 +288,3 @@ typedef enum {
 }
 
 @end
-
-
-

+ 6 - 0
VLC for iOS.xcodeproj/project.pbxproj

@@ -552,6 +552,7 @@
 		DD02C30E1ACAF4A50026EFEE /* VLCRowController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD02C30D1ACAF4A50026EFEE /* VLCRowController.m */; };
 		DD1542121ACFF76400AFD4EC /* VLCWatchTableController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD1542111ACFF76400AFD4EC /* VLCWatchTableController.m */; };
 		DD6FA7B01ACD641C006DEB2E /* VLCNowPlayingInterfaceController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD6FA7AF1ACD641C006DEB2E /* VLCNowPlayingInterfaceController.m */; };
+		DD7635D61AF262D100240CB8 /* NSManagedObjectContext+refreshAll.m in Sources */ = {isa = PBXBuildFile; fileRef = DD7635D51AF262D100240CB8 /* NSManagedObjectContext+refreshAll.m */; };
 		DDACEB561ADAD11300735484 /* WKInterfaceObject+VLCProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = DDACEB551ADAD11300735484 /* WKInterfaceObject+VLCProgress.m */; };
 		DDC10BE41AEE8EA700890DC3 /* VLCTimeNavigationTitleView.m in Sources */ = {isa = PBXBuildFile; fileRef = DDC10BE31AEE8EA700890DC3 /* VLCTimeNavigationTitleView.m */; };
 		DDE4906C1ACDB63F00B1B5E3 /* VLCNotificationRelay.m in Sources */ = {isa = PBXBuildFile; fileRef = DDE4906B1ACDB63F00B1B5E3 /* VLCNotificationRelay.m */; };
@@ -1617,6 +1618,8 @@
 		DD1542111ACFF76400AFD4EC /* VLCWatchTableController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCWatchTableController.m; sourceTree = "<group>"; };
 		DD6FA7AE1ACD641C006DEB2E /* VLCNowPlayingInterfaceController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCNowPlayingInterfaceController.h; sourceTree = "<group>"; };
 		DD6FA7AF1ACD641C006DEB2E /* VLCNowPlayingInterfaceController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCNowPlayingInterfaceController.m; sourceTree = "<group>"; };
+		DD7635D41AF262D100240CB8 /* NSManagedObjectContext+refreshAll.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectContext+refreshAll.h"; sourceTree = "<group>"; };
+		DD7635D51AF262D100240CB8 /* NSManagedObjectContext+refreshAll.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectContext+refreshAll.m"; sourceTree = "<group>"; };
 		DDACEB541ADAD11300735484 /* WKInterfaceObject+VLCProgress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WKInterfaceObject+VLCProgress.h"; sourceTree = "<group>"; };
 		DDACEB551ADAD11300735484 /* WKInterfaceObject+VLCProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "WKInterfaceObject+VLCProgress.m"; sourceTree = "<group>"; };
 		DDC10BE21AEE8EA700890DC3 /* VLCTimeNavigationTitleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VLCTimeNavigationTitleView.h; path = Sources/VLCTimeNavigationTitleView.h; sourceTree = SOURCE_ROOT; };
@@ -1841,6 +1844,8 @@
 			isa = PBXGroup;
 			children = (
 				DD02C2FC1ACACF400026EFEE /* VLC for iOS WatchKit Extension.entitlements */,
+				DD7635D41AF262D100240CB8 /* NSManagedObjectContext+refreshAll.h */,
+				DD7635D51AF262D100240CB8 /* NSManagedObjectContext+refreshAll.m */,
 				DDACEB541ADAD11300735484 /* WKInterfaceObject+VLCProgress.h */,
 				DDACEB551ADAD11300735484 /* WKInterfaceObject+VLCProgress.m */,
 				DDE4906A1ACDB63F00B1B5E3 /* VLCNotificationRelay.h */,
@@ -3635,6 +3640,7 @@
 				DDE4906C1ACDB63F00B1B5E3 /* VLCNotificationRelay.m in Sources */,
 				DD1542121ACFF76400AFD4EC /* VLCWatchTableController.m in Sources */,
 				DD02C30E1ACAF4A50026EFEE /* VLCRowController.m in Sources */,
+				DD7635D61AF262D100240CB8 /* NSManagedObjectContext+refreshAll.m in Sources */,
 				DDE490701ACE8BBC00B1B5E3 /* VLCDetailInterfaceController.m in Sources */,
 				4173AEA61ABF1B850004101D /* VLCPlaylistInterfaceController.m in Sources */,
 				DDACEB561ADAD11300735484 /* WKInterfaceObject+VLCProgress.m in Sources */,