浏览代码

reload data in watch app on saves in iPhone app

Tobias Conradi 10 年之前
父节点
当前提交
6a6a9586ae

+ 13 - 0
Sources/VLCAppDelegate.m

@@ -15,6 +15,7 @@
  * Refer to the COPYING file of the official project for license.
  *****************************************************************************/
 
+#import <notify.h>
 #import "VLCAppDelegate.h"
 #import "VLCMediaFileDiscoverer.h"
 #import "NSString+SupportedMedia.h"
@@ -142,6 +143,9 @@
         }
         setupBlock();
     }
+
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiyWatchForDBUpdate) name:NSManagedObjectContextDidSaveNotification object:nil];
+
     return YES;
 }
 
@@ -558,4 +562,13 @@
     [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
 }
 
+#pragma mark - watch struff
+- (void)notifiyWatchForDBUpdate
+{
+    notify_post("org.videolan.ios-app.dbupdate");
+}
+- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
+
+}
+
 @end

+ 21 - 8
VLC for iOS WatchKit Extension/InterfaceController.m

@@ -10,6 +10,7 @@
 #import <MediaLibraryKit/MediaLibraryKit.h>
 #import "VLCRowController.h"
 #import <MobileVLCKit/VLCTime.h>
+#import "VLCDBChangeNotifier.h"
 
 static NSString *const rowType = @"mediaRow";
 
@@ -28,28 +29,40 @@ static NSString *const rowType = @"mediaRow";
 
     MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
     mediaLibrary.libraryBasePath = groupURL.path;
+    mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
 
     self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
-
-
-    self.mediaObjects = [self mediaArray];
-
-    [self.table setNumberOfRows:self.mediaObjects.count withRowType:rowType];
-    [self configureTable:self.table withObjects:self.mediaObjects];
-
+    [[VLCDBChangeNotifier sharedNotifier] addObserver:self block:^{
+        [self updateData];
+    }];
+    [self updateData];
 }
 
 - (void)willActivate {
     // This method is called when watch view controller is about to be visible to user
     [super willActivate];
     NSLog(@"%s",__PRETTY_FUNCTION__);
-
+    [[VLCDBChangeNotifier sharedNotifier] addObserver:self block:^{
+        [self updateData];
+    }];
 }
 
 - (void)didDeactivate {
     // This method is called when watch view controller is no longer visible
     [super didDeactivate];
     NSLog(@"%s",__PRETTY_FUNCTION__);
+
+    [[VLCDBChangeNotifier sharedNotifier] removeObserver:self];
+}
+
+- (void)updateData {
+    self.mediaObjects = [self mediaArray];
+    NSUInteger newCount = self.mediaObjects.count;
+    if (newCount < self.table.numberOfRows) {
+        [self.table removeRowsAtIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(newCount, self.table.numberOfRows-newCount)]];
+    }
+    [self.table setNumberOfRows:self.mediaObjects.count withRowType:rowType];
+    [self configureTable:self.table withObjects:self.mediaObjects];
 }
 
 - (void)configureTable:(WKInterfaceTable *)table withObjects:(NSArray *)objects {

+ 15 - 0
VLC for iOS WatchKit Extension/VLCDBChangeNotifier.h

@@ -0,0 +1,15 @@
+//
+//  VLCDBChangeNotifier.h
+//  VLC for iOS
+//
+//  Created by Tobias Conradi on 01.04.15.
+//  Copyright (c) 2015 VideoLAN. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface VLCDBChangeNotifier : NSObject
++ (instancetype)sharedNotifier;
+- (void)addObserver:(id)observer block:(void (^)(void))onUpdate;
+- (void)removeObserver:(id)observer;
+@end

+ 67 - 0
VLC for iOS WatchKit Extension/VLCDBChangeNotifier.m

@@ -0,0 +1,67 @@
+//
+//  VLCDBChangeNotifier.m
+//  VLC for iOS
+//
+//  Created by Tobias Conradi on 01.04.15.
+//  Copyright (c) 2015 VideoLAN. All rights reserved.
+//
+
+#import "VLCDBChangeNotifier.h"
+#import <notify.h>
+
+
+@interface VLCDBChangeNotifier ()
+@property (nonatomic, strong) NSMapTable *observers;
+@property (nonatomic, assign) int notification_token;
+@end
+@implementation VLCDBChangeNotifier
+
+- (id)init
+{
+    self = [super init];
+    if (self) {
+        self.observers = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPointerPersonality valueOptions:NSPointerFunctionsCopyIn];
+    }
+    [self startListening];
+    return self;
+}
+
+- (void)dealloc {
+    notify_cancel(_notification_token);
+}
+
++ (instancetype)sharedNotifier
+{
+    static dispatch_once_t onceToken;
+    static VLCDBChangeNotifier *instance;
+    dispatch_once(&onceToken, ^{
+        instance = [VLCDBChangeNotifier new];
+    });
+    return instance;
+}
+
+- (void)addObserver:(id)observer block:(void (^)(void))onUpdate {
+    [self.observers setObject:onUpdate forKey:observer];
+}
+
+- (void)removeObserver:(id)observer {
+    [self.observers removeObjectForKey:observer];
+}
+
+- (void)dbDidChange {
+    for (void(^onUpdate)() in self.observers.objectEnumerator) {
+        onUpdate();
+    }
+}
+- (void)startListening {
+    const char *notification_name = "org.videolan.ios-app.dbupdate";
+    dispatch_queue_t queue = dispatch_get_main_queue();
+    int registration_token;
+    __weak typeof(self) weakSelf = self;
+    notify_register_dispatch(notification_name, &registration_token, queue, ^ (int token) {
+        [weakSelf dbDidChange];
+    });
+    self.notification_token = registration_token;
+}
+
+@end

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

@@ -540,6 +540,7 @@
 		DD02C3071ACAEC690026EFEE /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CC1BBC4D170493A300A20CBF /* libbz2.dylib */; };
 		DD02C30B1ACAF0370026EFEE /* libstdc++.6.0.9.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DD02C30A1ACAF0370026EFEE /* libstdc++.6.0.9.dylib */; };
 		DD02C30E1ACAF4A50026EFEE /* VLCRowController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD02C30D1ACAF4A50026EFEE /* VLCRowController.m */; };
+		DDCA94791ACC12630044449A /* VLCDBChangeNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = DDCA94781ACC12630044449A /* VLCDBChangeNotifier.m */; };
 		DDF157B11ACB162700AAFBC6 /* MediaLibrary.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 7D594CBC1A969E33004BFB17 /* MediaLibrary.xcdatamodeld */; };
 		DDF157B21ACB169600AAFBC6 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CC1BBC57170493E100A20CBF /* CoreData.framework */; };
 		DDF157B41ACB169B00AAFBC6 /* WatchKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DDF157B31ACB169B00AAFBC6 /* WatchKit.framework */; };
@@ -1572,6 +1573,8 @@
 		DD02C30A1ACAF0370026EFEE /* libstdc++.6.0.9.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libstdc++.6.0.9.dylib"; path = "usr/lib/libstdc++.6.0.9.dylib"; sourceTree = SDKROOT; };
 		DD02C30C1ACAF4A50026EFEE /* VLCRowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCRowController.h; sourceTree = "<group>"; };
 		DD02C30D1ACAF4A50026EFEE /* VLCRowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCRowController.m; sourceTree = "<group>"; };
+		DDCA94771ACC12630044449A /* VLCDBChangeNotifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCDBChangeNotifier.h; sourceTree = "<group>"; };
+		DDCA94781ACC12630044449A /* VLCDBChangeNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCDBChangeNotifier.m; sourceTree = "<group>"; };
 		DDF157B31ACB169B00AAFBC6 /* WatchKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WatchKit.framework; path = System/Library/Frameworks/WatchKit.framework; sourceTree = SDKROOT; };
 		E09EACF57CDD22ABAE66CDD0 /* Pods-vlc-ios.distribution.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-vlc-ios.distribution.xcconfig"; path = "Pods/Target Support Files/Pods-vlc-ios/Pods-vlc-ios.distribution.xcconfig"; sourceTree = "<group>"; };
 		E0C04F931A25B4410080331A /* VLCDocumentPickerController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VLCDocumentPickerController.h; path = Sources/VLCDocumentPickerController.h; sourceTree = SOURCE_ROOT; };
@@ -1783,6 +1786,8 @@
 			isa = PBXGroup;
 			children = (
 				DD02C2FC1ACACF400026EFEE /* VLC for iOS WatchKit Extension.entitlements */,
+				DDCA94771ACC12630044449A /* VLCDBChangeNotifier.h */,
+				DDCA94781ACC12630044449A /* VLCDBChangeNotifier.m */,
 				4173AEA41ABF1B850004101D /* InterfaceController.h */,
 				4173AEA51ABF1B850004101D /* InterfaceController.m */,
 				DD02C30C1ACAF4A50026EFEE /* VLCRowController.h */,
@@ -3543,6 +3548,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				DDF157B11ACB162700AAFBC6 /* MediaLibrary.xcdatamodeld in Sources */,
+				DDCA94791ACC12630044449A /* VLCDBChangeNotifier.m in Sources */,
 				DD02C30E1ACAF4A50026EFEE /* VLCRowController.m in Sources */,
 				4173AEA61ABF1B850004101D /* InterfaceController.m in Sources */,
 			);