VLCWatchTableController.m 5.9 KB

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