Jelajahi Sumber

Improve table controller objects comparison so rows for objects which were updated are not removed and added, but updated.
Rows no longer slide out and in when the represented object was updated.

Tobias Conradi 9 tahun lalu
induk
melakukan
efe9aad036

+ 1 - 0
VLC WatchKit Native Extension/Classes/VLCPlaylistInterfaceController.m

@@ -57,6 +57,7 @@ static NSString *const rowType = @"mediaRow";
     tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
     tableController.pageSize = 20;
     tableController.rowType = rowType;
+    tableController.identifierKeyPath = @"objectID.URIRepresentation";
 
     tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
         if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {

+ 8 - 0
VLC WatchKit Native Extension/Classes/VLCWatchTableController.h

@@ -52,6 +52,14 @@ typedef void(^VLCWatchTableControllerConfigureRowControllerWithObjectBlock)(id r
  */
 @property (nonatomic, copy) NSArray *objects;
 
+/*
+ * Set the identifierKeyPath to the key path of a unique identifier of the objects.
+ * The identifier at the keyPath is used to determine if a object was added or removed.
+ * Default is @"self".
+ */
+@property (nonatomic, copy) NSString *identifierKeyPath;
+
+
 /* updates the table with the current configuration (pagesize, page, objects) */
 - (void)updateTable;
 

+ 10 - 4
VLC WatchKit Native Extension/Classes/VLCWatchTableController.m

@@ -23,6 +23,12 @@
 
 @implementation VLCWatchTableController
 
+- (NSString *)identifierKey {
+    if (!_identifierKeyPath) {
+        _identifierKeyPath = @"self";
+    }
+    return _identifierKeyPath;
+}
 
 - (void)setObjects:(NSArray *)objects {
     _objects = [objects copy];
@@ -56,10 +62,10 @@
     /* get new dispayed objects */
     NSRange range = NSMakeRange(startIndex, endIndex-startIndex);
     NSArray *newObjects = [self.objects subarrayWithRange:range];
-    NSSet *newSet = [[NSSet alloc] initWithArray:newObjects];
+    NSSet *newSet = [[NSSet alloc] initWithArray:[newObjects valueForKeyPath:self.identifierKeyPath]];
 
     NSArray *oldObjects = self.displayedObjects;
-    NSSet *oldSet = [[NSSet alloc] initWithArray:oldObjects];
+    NSSet *oldSet = [[NSSet alloc] initWithArray:[oldObjects valueForKeyPath:self.identifierKeyPath]];
 
     WKInterfaceTable *table = self.table;
 
@@ -102,14 +108,14 @@
     else {
         NSMutableIndexSet *removeRowIndexes = [NSMutableIndexSet new];
         [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-            if ([removedSet containsObject:obj]) {
+            if ([removedSet containsObject:[obj valueForKeyPath:self.identifierKeyPath]]) {
                 [removeRowIndexes addIndex:idx];
             }
         }];
         [table removeRowsAtIndexes:removeRowIndexes];
 
         [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-            if ([addedSet containsObject:obj]) {
+            if ([addedSet containsObject:[obj valueForKeyPath:self.identifierKeyPath]]) {
                 NSString *rowType = [self _rowTypeForObject:obj];
                 [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
             }