فهرست منبع

fix VLCWatchTableController for all same row type. TODO add support for different row types (currently crashing assert)

Tobias Conradi 10 سال پیش
والد
کامیت
8726764e37
2فایلهای تغییر یافته به همراه74 افزوده شده و 28 حذف شده
  1. 14 12
      VLC for iOS WatchKit Extension/InterfaceController.m
  2. 60 16
      VLC for iOS WatchKit Extension/VLCWatchTableController.m

+ 14 - 12
VLC for iOS WatchKit Extension/InterfaceController.m

@@ -43,17 +43,21 @@ static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.db
     self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
     [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
-    _tableController = [[VLCWatchTableController alloc] init];
-    _tableController.table = self.table;
-    _tableController.previousPageButton = self.previousButton;
-    _tableController.nextPageButton = self.nextButton;
-    _tableController.pageSize = 5;
-    _tableController.rowTypeForObjectBlock = ^(id object) {
-        return rowType;
-    };
-    _tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
-        [self configureTableRowController:context withObject:object];
+
+    VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
+    tableController.table = self.table;
+    tableController.previousPageButton = self.previousButton;
+    tableController.nextPageButton = self.nextButton;
+    tableController.pageSize = 5;
+    tableController.rowType = rowType;
+
+    __weak typeof(self) weakSelf = self;
+    tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
+        [weakSelf configureTableRowController:controller withObject:object];
     };
+    self.tableController = tableController;
+    [self updateData];
+
 }
 
 - (void)willActivate {
@@ -61,8 +65,6 @@ static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.db
     [super willActivate];
     NSLog(@"%s",__PRETTY_FUNCTION__);
 
-    [self updateData];
-
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
 }
 

+ 60 - 16
VLC for iOS WatchKit Extension/VLCWatchTableController.m

@@ -17,6 +17,7 @@
 
 @property (nonatomic, copy, readwrite) NSArray *displayedObjects;
 @property (nonatomic, copy, readwrite) NSIndexSet *displayedIndexes;
+@property (nonatomic, copy, readwrite) NSArray *rowTypes;
 
 @end
 
@@ -54,33 +55,71 @@
     }
 
     /* set starte for previous and next buttons */
-    self.previousPageButton.hidden = currentPage > 0;
-    self.nextPageButton.hidden = endIndex < objectsCount;
+    self.previousPageButton.hidden = currentPage == 0;
+    self.nextPageButton.hidden = endIndex >= objectsCount;
 
     /* get new dispayed objects */
     NSRange range = NSMakeRange(startIndex, endIndex-startIndex);
     NSArray *newObjects = [self.objects subarrayWithRange:range];
-    NSMutableSet *newSet = [[NSMutableSet alloc] initWithArray:newObjects];
+    NSSet *newSet = [[NSSet alloc] initWithArray:newObjects];
 
     NSArray *oldObjects = self.displayedObjects;
     NSSet *oldSet = [[NSSet alloc] initWithArray:oldObjects];
 
     WKInterfaceTable *table = self.table;
 
-    [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-        if (![newSet containsObject:obj]) {
-            [table removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx]];
-        }
-    }];
+    NSMutableSet *addedSet = [NSMutableSet setWithSet:newSet];
+    [addedSet minusSet:oldSet];
+    NSMutableSet *removedSet = [NSMutableSet setWithSet:oldSet];
+    [removedSet minusSet:newSet];
+
+    BOOL differentRowTypes = self.rowTypeForObjectBlock != nil;
+    BOOL pageChange = startIndex != self.displayedIndexes.firstIndex;
 
-    [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-        if (![oldSet containsObject:obj]) {
-            NSString *rowType = [self rowTypeForObject:obj];
-            [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
+    NSMutableArray *rowTypes = differentRowTypes ? [NSMutableArray arrayWithCapacity:pageSize] : nil;
+
+    // we changed the page
+    if (pageChange) {
+        if (differentRowTypes) {
+            // TODO add support different rowtypes
+            NSAssert(NO,@"TODO add support different rowtypes");
+        } else {
+            NSUInteger oldCount = oldObjects.count;
+            NSUInteger newCount = newObjects.count;
+            // remove rows if now on smaller page
+            if (oldCount > newCount) {
+                NSRange range = NSMakeRange(newCount, oldCount-newCount);
+                NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
+                [table removeRowsAtIndexes:indexSet];
+            }
+            // add rows if now on bigger page
+            else if (oldCount < newCount) {
+                NSRange range = NSMakeRange(oldCount, newCount-oldCount);
+                NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
+                [table insertRowsAtIndexes:indexSet withRowType:self.rowType];
+            }
+            [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+                [self configureTableCellAtIndex:idx withObject:obj];
+            }];
         }
-        [self configureTableCellAtIndex:idx withObject:obj];
-    }];
+    }
+    // update on the same page
+    else {
+        [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+            if ([removedSet containsObject:obj]) {
+                [table removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx]];
+            }
+        }];
+        [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+            if ([addedSet containsObject:obj]) {
+                NSString *rowType = [self rowTypeForObject:obj];
+                [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
+            }
+            [self configureTableCellAtIndex:idx withObject:obj];
+        }];
+    }
 
+    self.rowTypes = rowTypes;
     self.displayedObjects = newObjects;
     self.displayedIndexes = [NSIndexSet indexSetWithIndexesInRange:range];
 }
@@ -93,13 +132,18 @@
     }
     self.currentPage = self.currentPage+1;
     [self updateTable];
+    [self.table scrollToRowAtIndex:0];
 }
 
 - (void)previousPageButtonPressed {
-    if (self.pageSize>0) {
-        self.pageSize = self.pageSize-1;
+    if (self.currentPage>0) {
+        self.currentPage = self.currentPage-1;
         [self updateTable];
     }
+    NSUInteger displayedCount = self.displayedObjects.count;
+    if (displayedCount) {
+        [self.table scrollToRowAtIndex:displayedCount-1];
+    }
 }
 
 #pragma mark - internal helper