VLCWatchTableController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. @end
  17. @implementation VLCWatchTableController
  18. - (void)setObjects:(NSArray *)objects {
  19. _objects = [objects copy];
  20. [self updateTable];
  21. }
  22. - (void)updateTable {
  23. NSUInteger pageSize = self.pageSize;
  24. NSUInteger currentPage = self.currentPage;
  25. NSUInteger startIndex = self.currentPage*pageSize;
  26. NSUInteger objectsCount = self.objects.count;
  27. /* calculate a valid start index and reset current page if needed */
  28. while (startIndex > objectsCount) {
  29. if (startIndex < pageSize) {
  30. startIndex = 0;
  31. currentPage = 0;
  32. } else {
  33. startIndex -= pageSize;
  34. currentPage--;
  35. }
  36. }
  37. /* calculate valid end index */
  38. NSUInteger endIndex = startIndex+pageSize;
  39. if (endIndex > objectsCount) {
  40. endIndex = objectsCount;
  41. }
  42. /* set starte for previous and next buttons */
  43. self.previousPageButton.hidden = currentPage > 0;
  44. self.nextPageButton.hidden = endIndex < objectsCount;
  45. /* get new dispayed objects */
  46. NSRange range = NSMakeRange(startIndex, endIndex-startIndex);
  47. NSArray *newObjects = [self.objects subarrayWithRange:range];
  48. NSMutableSet *newSet = [[NSMutableSet alloc] initWithArray:newObjects];
  49. NSArray *oldObjects = self.displayedObjects;
  50. NSSet *oldSet = [[NSSet alloc] initWithArray:oldObjects];
  51. WKInterfaceTable *table = self.table;
  52. [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  53. if (![newSet containsObject:obj]) {
  54. [table removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx]];
  55. }
  56. }];
  57. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  58. if (![oldSet containsObject:obj]) {
  59. NSString *rowType = [self rowTypeForObject:obj];
  60. [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
  61. }
  62. [self configureTableCellAtIndex:idx withObject:obj];
  63. }];
  64. self.displayedObjects = newObjects;
  65. self.displayedIndexes = [NSIndexSet indexSetWithIndexesInRange:range];
  66. }
  67. - (void)nextPageButtonPressed {
  68. NSUInteger nextPageStartIndex = self.pageSize * (self.currentPage+1);
  69. if (nextPageStartIndex > self.objects.count) {
  70. return;
  71. }
  72. self.currentPage = self.currentPage+1;
  73. [self updateTable];
  74. }
  75. - (void)previousPageButtonPressed {
  76. if (self.pageSize>0) {
  77. self.pageSize = self.pageSize-1;
  78. [self updateTable];
  79. }
  80. }
  81. #pragma mark - internal helper
  82. - (NSString *)rowTypeForObject:(id)object {
  83. if (self.rowTypeForObjectBlock) {
  84. return self.rowTypeForObjectBlock(object);
  85. }
  86. NSAssert(self.rowType, @"Either rowTypeForObjectBlock or rowType must be set");
  87. return self.rowType;
  88. }
  89. - (void)configureTableCellAtIndex:(NSUInteger)index withObject:(id)object {
  90. VLCWatchTableControllerConfigureRowControllerWithObjectBlock configureBlock = self.configureRowControllerWithObjectBlock;
  91. NSAssert(configureBlock, @"configureRowControllerWithObjectBlock must be set");
  92. if (configureBlock) {
  93. id rowController = [self.table rowControllerAtIndex:index];
  94. configureBlock(rowController, object);
  95. }
  96. }
  97. @end