123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*****************************************************************************
- * VLCWatchTableController.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Author: Tobias Conradi <videolan # tobias-conradi.de>
- *
- * 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
|