VLCWatchTableController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*****************************************************************************
  2. * VLCWatchTableController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Author: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCWatchTableController.h"
  13. @interface VLCWatchTableController()
  14. @property (nonatomic, copy, readwrite) NSArray *displayedObjects;
  15. @property (nonatomic, copy, readwrite) NSIndexSet *displayedIndexes;
  16. @property (nonatomic, copy, readwrite) NSArray *rowTypes;
  17. @end
  18. @implementation VLCWatchTableController
  19. - (void)setObjects:(NSArray *)objects {
  20. _objects = [objects copy];
  21. [self updateTable];
  22. }
  23. - (void)updateTable {
  24. NSUInteger pageSize = self.pageSize;
  25. NSUInteger currentPage = self.currentPage;
  26. NSUInteger startIndex = self.currentPage*pageSize;
  27. NSUInteger objectsCount = self.objects.count;
  28. /* calculate a valid start index and reset current page if needed */
  29. while (startIndex > objectsCount) {
  30. if (startIndex < pageSize) {
  31. startIndex = 0;
  32. currentPage = 0;
  33. } else {
  34. startIndex -= pageSize;
  35. currentPage--;
  36. }
  37. }
  38. /* calculate valid end index */
  39. NSUInteger endIndex = startIndex+pageSize;
  40. if (endIndex > objectsCount) {
  41. endIndex = objectsCount;
  42. }
  43. /* set starte for previous and next buttons */
  44. self.previousPageButton.hidden = currentPage == 0;
  45. self.nextPageButton.hidden = endIndex >= objectsCount;
  46. /* get new dispayed objects */
  47. NSRange range = NSMakeRange(startIndex, endIndex-startIndex);
  48. NSArray *newObjects = [self.objects subarrayWithRange:range];
  49. NSSet *newSet = [[NSSet alloc] initWithArray:newObjects];
  50. NSArray *oldObjects = self.displayedObjects;
  51. NSSet *oldSet = [[NSSet alloc] initWithArray:oldObjects];
  52. WKInterfaceTable *table = self.table;
  53. NSMutableSet *addedSet = [NSMutableSet setWithSet:newSet];
  54. [addedSet minusSet:oldSet];
  55. NSMutableSet *removedSet = [NSMutableSet setWithSet:oldSet];
  56. [removedSet minusSet:newSet];
  57. BOOL differentRowTypes = self.rowTypeForObjectBlock != nil;
  58. BOOL pageChange = startIndex != self.displayedIndexes.firstIndex;
  59. NSMutableArray *rowTypes = differentRowTypes ? [NSMutableArray arrayWithCapacity:pageSize] : nil;
  60. // we changed the page
  61. if (pageChange) {
  62. if (differentRowTypes) {
  63. // TODO add support different rowtypes
  64. NSAssert(NO,@"TODO add support different rowtypes");
  65. } else {
  66. NSUInteger oldCount = oldObjects.count;
  67. NSUInteger newCount = newObjects.count;
  68. // remove rows if now on smaller page
  69. if (oldCount > newCount) {
  70. NSRange range = NSMakeRange(newCount, oldCount-newCount);
  71. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
  72. [table removeRowsAtIndexes:indexSet];
  73. }
  74. // add rows if now on bigger page
  75. else if (oldCount < newCount) {
  76. NSRange range = NSMakeRange(oldCount, newCount-oldCount);
  77. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
  78. [table insertRowsAtIndexes:indexSet withRowType:self.rowType];
  79. }
  80. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  81. [self configureTableCellAtIndex:idx withObject:obj];
  82. }];
  83. }
  84. }
  85. // update on the same page
  86. else {
  87. NSMutableIndexSet *removeRowIndexes = [NSMutableIndexSet new];
  88. [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  89. if ([removedSet containsObject:obj]) {
  90. [removeRowIndexes addIndex:idx];
  91. }
  92. }];
  93. [table removeRowsAtIndexes:removeRowIndexes];
  94. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  95. if ([addedSet containsObject:obj]) {
  96. NSString *rowType = [self rowTypeForObject:obj];
  97. [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
  98. }
  99. [self configureTableCellAtIndex:idx withObject:obj];
  100. }];
  101. }
  102. self.rowTypes = rowTypes;
  103. self.displayedObjects = newObjects;
  104. self.displayedIndexes = [NSIndexSet indexSetWithIndexesInRange:range];
  105. }
  106. - (void)nextPageButtonPressed {
  107. NSUInteger nextPageStartIndex = self.pageSize * (self.currentPage+1);
  108. if (nextPageStartIndex > self.objects.count) {
  109. return;
  110. }
  111. self.currentPage = self.currentPage+1;
  112. [self updateTable];
  113. [self.table scrollToRowAtIndex:0];
  114. }
  115. - (void)previousPageButtonPressed {
  116. if (self.currentPage>0) {
  117. self.currentPage = self.currentPage-1;
  118. [self updateTable];
  119. }
  120. NSUInteger displayedCount = self.displayedObjects.count;
  121. if (displayedCount) {
  122. [self.table scrollToRowAtIndex:displayedCount-1];
  123. }
  124. }
  125. #pragma mark - internal helper
  126. - (NSString *)rowTypeForObject:(id)object {
  127. if (self.rowTypeForObjectBlock) {
  128. return self.rowTypeForObjectBlock(object);
  129. }
  130. NSAssert(self.rowType, @"Either rowTypeForObjectBlock or rowType must be set");
  131. return self.rowType;
  132. }
  133. - (void)configureTableCellAtIndex:(NSUInteger)index withObject:(id)object {
  134. VLCWatchTableControllerConfigureRowControllerWithObjectBlock configureBlock = self.configureRowControllerWithObjectBlock;
  135. NSAssert(configureBlock, @"configureRowControllerWithObjectBlock must be set");
  136. if (configureBlock) {
  137. id rowController = [self.table rowControllerAtIndex:index];
  138. configureBlock(rowController, object);
  139. }
  140. }
  141. @end