VLCWatchTableController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. - (NSString *)identifierKey {
  20. if (!_identifierKeyPath) {
  21. _identifierKeyPath = @"self";
  22. }
  23. return _identifierKeyPath;
  24. }
  25. - (void)setObjects:(NSArray *)objects {
  26. _objects = [objects copy];
  27. [self updateTable];
  28. }
  29. - (void)updateTable {
  30. NSUInteger pageSize = self.pageSize;
  31. NSUInteger currentPage = self.currentPage;
  32. NSUInteger startIndex = self.currentPage*pageSize;
  33. NSUInteger objectsCount = self.objects.count;
  34. /* calculate a valid start index and reset current page if needed */
  35. while (startIndex > objectsCount) {
  36. if (startIndex < pageSize) {
  37. startIndex = 0;
  38. currentPage = 0;
  39. } else {
  40. startIndex -= pageSize;
  41. currentPage--;
  42. }
  43. }
  44. /* calculate valid end index */
  45. NSUInteger endIndex = startIndex+pageSize;
  46. if (endIndex > objectsCount) {
  47. endIndex = objectsCount;
  48. }
  49. /* get new dispayed objects */
  50. NSRange range = NSMakeRange(startIndex, endIndex-startIndex);
  51. NSArray *newObjects = [self.objects subarrayWithRange:range];
  52. NSSet *newSet = [[NSSet alloc] initWithArray:[newObjects valueForKeyPath:self.identifierKeyPath]];
  53. NSArray *oldObjects = self.displayedObjects;
  54. NSSet *oldSet = [[NSSet alloc] initWithArray:[oldObjects valueForKeyPath:self.identifierKeyPath]];
  55. WKInterfaceTable *table = self.table;
  56. NSMutableSet *addedSet = [NSMutableSet setWithSet:newSet];
  57. [addedSet minusSet:oldSet];
  58. NSMutableSet *removedSet = [NSMutableSet setWithSet:oldSet];
  59. [removedSet minusSet:newSet];
  60. BOOL differentRowTypes = self.rowTypeForObjectBlock != nil;
  61. BOOL pageChange = startIndex != self.displayedIndexes.firstIndex;
  62. NSMutableArray *rowTypes = differentRowTypes ? [NSMutableArray arrayWithCapacity:pageSize] : nil;
  63. // we changed the page
  64. if (pageChange) {
  65. if (differentRowTypes) {
  66. // TODO add support different rowtypes
  67. NSAssert(NO,@"TODO add support different rowtypes");
  68. } else {
  69. NSUInteger oldCount = oldObjects.count;
  70. NSUInteger newCount = newObjects.count;
  71. // remove rows if now on smaller page
  72. if (oldCount > newCount) {
  73. NSRange range = NSMakeRange(newCount, oldCount-newCount);
  74. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
  75. [table removeRowsAtIndexes:indexSet];
  76. }
  77. // add rows if now on bigger page
  78. else if (oldCount < newCount) {
  79. NSRange range = NSMakeRange(oldCount, newCount-oldCount);
  80. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
  81. [table insertRowsAtIndexes:indexSet withRowType:self.rowType];
  82. }
  83. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  84. [self _configureTableCellAtIndex:idx withObject:obj];
  85. }];
  86. }
  87. }
  88. // update on the same page
  89. else {
  90. NSMutableIndexSet *removeRowIndexes = [NSMutableIndexSet new];
  91. [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  92. if ([removedSet containsObject:[obj valueForKeyPath:self.identifierKeyPath]]) {
  93. [removeRowIndexes addIndex:idx];
  94. }
  95. }];
  96. [table removeRowsAtIndexes:removeRowIndexes];
  97. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  98. if ([addedSet containsObject:[obj valueForKeyPath:self.identifierKeyPath]]) {
  99. NSString *rowType = [self _rowTypeForObject:obj];
  100. [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
  101. }
  102. [self _configureTableCellAtIndex:idx withObject:obj];
  103. }];
  104. }
  105. self.rowTypes = rowTypes;
  106. self.displayedObjects = newObjects;
  107. self.displayedIndexes = [NSIndexSet indexSetWithIndexesInRange:range];
  108. /* set state for previous and next buttons */
  109. self.previousPageButton.hidden = currentPage == 0;
  110. self.nextPageButton.hidden = endIndex >= objectsCount;
  111. self.emptyLibraryInterfaceObjects.hidden = newObjects.count != 0;
  112. }
  113. - (void)nextPageButtonPressed {
  114. NSUInteger nextPageStartIndex = self.pageSize * (self.currentPage+1);
  115. if (nextPageStartIndex > self.objects.count) {
  116. return;
  117. }
  118. self.currentPage = self.currentPage+1;
  119. [self updateTable];
  120. [self.table scrollToRowAtIndex:0];
  121. }
  122. - (void)previousPageButtonPressed {
  123. if (self.currentPage>0) {
  124. self.currentPage = self.currentPage-1;
  125. [self updateTable];
  126. }
  127. NSUInteger displayedCount = self.displayedObjects.count;
  128. if (displayedCount) {
  129. [self.table scrollToRowAtIndex:displayedCount-1];
  130. }
  131. }
  132. #pragma mark - internal helper
  133. - (NSString *)_rowTypeForObject:(id)object {
  134. if (self.rowTypeForObjectBlock) {
  135. return self.rowTypeForObjectBlock(object);
  136. }
  137. NSAssert(self.rowType, @"Either rowTypeForObjectBlock or rowType must be set");
  138. return self.rowType;
  139. }
  140. - (void)_configureTableCellAtIndex:(NSUInteger)index withObject:(id)object {
  141. VLCWatchTableControllerConfigureRowControllerWithObjectBlock configureBlock = self.configureRowControllerWithObjectBlock;
  142. NSAssert(configureBlock, @"configureRowControllerWithObjectBlock must be set");
  143. if (configureBlock) {
  144. id rowController = [self.table rowControllerAtIndex:index];
  145. configureBlock(rowController, object);
  146. }
  147. }
  148. @end