/***************************************************************************** * VLCWatchTableController.m * VLC for iOS ***************************************************************************** * Copyright (c) 2015 VideoLAN. All rights reserved. * $Id$ * * Author: Tobias Conradi * * Refer to the COPYING file of the official project for license. *****************************************************************************/ #import "VLCWatchTableController.h" @interface VLCWatchTableController() @property (nonatomic, copy, readwrite) NSArray *displayedObjects; @property (nonatomic, copy, readwrite) NSIndexSet *displayedIndexes; @end @implementation VLCWatchTableController - (void)setObjects:(NSArray *)objects { _objects = [objects copy]; [self updateTable]; } - (void)updateTable { NSUInteger pageSize = self.pageSize; NSUInteger currentPage = self.currentPage; NSUInteger startIndex = self.currentPage*pageSize; NSUInteger objectsCount = self.objects.count; /* calculate a valid start index and reset current page if needed */ while (startIndex > objectsCount) { if (startIndex < pageSize) { startIndex = 0; currentPage = 0; } else { startIndex -= pageSize; currentPage--; } } /* calculate valid end index */ NSUInteger endIndex = startIndex+pageSize; if (endIndex > objectsCount) { endIndex = objectsCount; } /* set starte for previous and next buttons */ 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]; 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]]; } }]; [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (![oldSet containsObject:obj]) { NSString *rowType = [self rowTypeForObject:obj]; [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType]; } [self configureTableCellAtIndex:idx withObject:obj]; }]; self.displayedObjects = newObjects; self.displayedIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; } - (void)nextPageButtonPressed { NSUInteger nextPageStartIndex = self.pageSize * (self.currentPage+1); if (nextPageStartIndex > self.objects.count) { return; } self.currentPage = self.currentPage+1; [self updateTable]; } - (void)previousPageButtonPressed { if (self.pageSize>0) { self.pageSize = self.pageSize-1; [self updateTable]; } } #pragma mark - internal helper - (NSString *)rowTypeForObject:(id)object { if (self.rowTypeForObjectBlock) { return self.rowTypeForObjectBlock(object); } NSAssert(self.rowType, @"Either rowTypeForObjectBlock or rowType must be set"); return self.rowType; } - (void)configureTableCellAtIndex:(NSUInteger)index withObject:(id)object { VLCWatchTableControllerConfigureRowControllerWithObjectBlock configureBlock = self.configureRowControllerWithObjectBlock; NSAssert(configureBlock, @"configureRowControllerWithObjectBlock must be set"); if (configureBlock) { id rowController = [self.table rowControllerAtIndex:index]; configureBlock(rowController, object); } } @end