RATreeView+Private.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2014 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RATreeView+Private.h"
  20. #import "RATreeView+Enums.h"
  21. #import "RATreeView+RATreeNodeCollectionControllerDataSource.h"
  22. #import "RATreeView_ClassExtension.h"
  23. #import "RABatchChanges.h"
  24. #import "RATreeNode.h"
  25. #import "RATreeNodeController.h"
  26. #import "RATreeNodeCollectionController.h"
  27. @implementation RATreeView (Private)
  28. - (void)setupTreeStructure
  29. {
  30. self.treeNodeCollectionController = [[RATreeNodeCollectionController alloc] init];
  31. self.treeNodeCollectionController.dataSource = self;
  32. self.batchChanges = [[RABatchChanges alloc] init];
  33. }
  34. - (NSArray *)childrenForItem:(id)item
  35. {
  36. NSParameterAssert(item);
  37. NSMutableArray *children = [NSMutableArray array];
  38. NSInteger numberOfChildren = [self.dataSource treeView:self numberOfChildrenOfItem:item];
  39. for (int i = 0; i < numberOfChildren; i++) {
  40. [children addObject:[self.dataSource treeView:self child:i ofItem:item]];
  41. }
  42. return [NSArray arrayWithArray:children];
  43. }
  44. - (RATreeNode *)treeNodeForIndexPath:(NSIndexPath *)indexPath
  45. {
  46. NSParameterAssert(indexPath.section == 0);
  47. return [self.treeNodeCollectionController treeNodeForIndex:indexPath.row];
  48. }
  49. - (NSIndexPath *)indexPathForItem:(id)item
  50. {
  51. return [NSIndexPath indexPathForRow:[self.treeNodeCollectionController indexForItem:item] inSection:0];
  52. }
  53. #pragma mark Collapsing and Expanding Rows
  54. - (void)collapseCellForTreeNode:(RATreeNode *)treeNode
  55. {
  56. [self collapseCellForTreeNode:treeNode collapseChildren:self.collapsesChildRowsWhenRowCollapses withRowAnimation:self.rowsCollapsingAnimation];
  57. }
  58. - (void)collapseCellForTreeNode:(RATreeNode *)treeNode collapseChildren:(BOOL)collapseChildren withRowAnimation:(RATreeViewRowAnimation)rowAnimation
  59. {
  60. [self.tableView beginUpdates];
  61. [self.batchChanges beginUpdates];
  62. NSInteger index = [self.treeNodeCollectionController lastVisibleDescendantIndexForItem:treeNode.item];
  63. __weak __typeof(self) weakSelf = self;
  64. [self.batchChanges collapseItemWithBlock:^{
  65. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:rowAnimation];
  66. [weakSelf.treeNodeCollectionController collapseRowForItem:treeNode.item collapseChildren:collapseChildren updates:^(NSIndexSet *deletions) {
  67. [weakSelf.tableView deleteRowsAtIndexPaths:IndexesToIndexPaths(deletions) withRowAnimation:tableViewRowAnimation];
  68. }];
  69. } lastIndex:index];
  70. [self.batchChanges endUpdates];
  71. [self.tableView endUpdates];
  72. }
  73. - (void)expandCellForTreeNode:(RATreeNode *)treeNode
  74. {
  75. [self expandCellForTreeNode:treeNode expandChildren:self.expandsChildRowsWhenRowExpands withRowAnimation:self.rowsExpandingAnimation];
  76. }
  77. - (void)expandCellForTreeNode:(RATreeNode *)treeNode expandChildren:(BOOL)expandChildren withRowAnimation:(RATreeViewRowAnimation)rowAnimation
  78. {
  79. [self.tableView beginUpdates];
  80. [self.batchChanges beginUpdates];
  81. NSInteger index = [self.treeNodeCollectionController indexForItem:treeNode.item];
  82. __weak __typeof(self) weakSelf = self;
  83. [self.batchChanges expandItemWithBlock:^{
  84. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:rowAnimation];
  85. [weakSelf.treeNodeCollectionController expandRowForItem:treeNode.item expandChildren:expandChildren updates:^(NSIndexSet *insertions) {
  86. [weakSelf.tableView insertRowsAtIndexPaths:IndexesToIndexPaths(insertions) withRowAnimation:tableViewRowAnimation];
  87. }];
  88. } atIndex:index];
  89. [self.batchChanges endUpdates];
  90. [self.tableView endUpdates];
  91. }
  92. - (void)insertItemAtIndex:(NSInteger)index inParent:(id)parent withAnimation:(RATreeViewRowAnimation)animation
  93. {
  94. NSInteger idx = [self.treeNodeCollectionController indexForItem:parent];
  95. if (idx == NSNotFound) {
  96. return;
  97. }
  98. idx += index + 1;
  99. __weak __typeof(self) weakSelf = self;
  100. [self.batchChanges insertItemWithBlock:^{
  101. [weakSelf.treeNodeCollectionController insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:parent];
  102. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
  103. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:animation];
  104. [weakSelf.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:tableViewRowAnimation];
  105. } atIndex:idx];
  106. }
  107. #pragma clang diagnostic push
  108. #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
  109. - (void)moveItemAtIndex:(NSInteger)index inParent:(id)parent toIndex:(NSInteger)newIndex inParent:(id)newParent
  110. #pragma clang diagnostic pop
  111. {
  112. NSInteger idx = [self.treeNodeCollectionController indexForItem:parent];
  113. if (idx == NSNotFound) {
  114. return;
  115. }
  116. idx += index + 1;
  117. __weak __typeof(self) weakSelf = self;
  118. [self.batchChanges insertItemWithBlock:^{
  119. [weakSelf.treeNodeCollectionController moveItemAtIndex:index inParent:parent toIndex:newIndex inParent:newParent updates:^(NSIndexSet *deletions, NSIndexSet *additions) {
  120. NSArray *deletionsArray = IndexesToIndexPaths(deletions);
  121. NSArray *additionsArray = IndexesToIndexPaths(additions);
  122. NSInteger i = 0;
  123. for (NSIndexPath *deletedIndexPath in deletionsArray) {
  124. [weakSelf.tableView moveRowAtIndexPath:deletedIndexPath toIndexPath:additionsArray[i]];
  125. i++;
  126. }
  127. }];
  128. } atIndex:idx];
  129. }
  130. - (void)removeItemAtIndex:(NSInteger)index inParent:(id)parent withAnimation:(RATreeViewRowAnimation)animation
  131. {
  132. id child = [self.treeNodeCollectionController childInParent:parent atIndex:index];
  133. NSInteger idx = [self.treeNodeCollectionController lastVisibleDescendantIndexForItem:child];
  134. if (idx == NSNotFound) {
  135. return;
  136. }
  137. __weak __typeof(self) weakSelf = self;
  138. [self.batchChanges insertItemWithBlock:^{
  139. [weakSelf.treeNodeCollectionController removeItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:parent updates:^(NSIndexSet *removedIndexes) {
  140. UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:animation];
  141. [weakSelf.tableView deleteRowsAtIndexPaths:IndexesToIndexPaths(removedIndexes) withRowAnimation:tableViewRowAnimation];
  142. }];
  143. } atIndex:idx];
  144. }
  145. #pragma mark -
  146. static NSArray * IndexesToIndexPaths(NSIndexSet *indexes)
  147. {
  148. NSMutableArray *indexPaths = [NSMutableArray array];
  149. [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  150. [indexPaths addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
  151. }];
  152. return [indexPaths copy];
  153. }
  154. @end