FLEXBookmarksViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // FLEXBookmarksViewController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 2/6/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXBookmarksViewController.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXNavigationController.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXBookmarkManager.h"
  13. #import "UIBarButtonItem+FLEX.h"
  14. #import "FLEXColor.h"
  15. #import "FLEXUtility.h"
  16. #import "FLEXRuntimeUtility.h"
  17. #import "FLEXTableView.h"
  18. @interface FLEXBookmarksViewController ()
  19. @property (nonatomic, copy) NSArray *bookmarks;
  20. @property (nonatomic, readonly) FLEXExplorerViewController *corePresenter;
  21. @end
  22. @implementation FLEXBookmarksViewController
  23. #pragma mark - Initialization
  24. - (id)init {
  25. return [self initWithStyle:UITableViewStylePlain];
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. #if !TARGET_OS_TV
  30. self.navigationController.hidesBarsOnSwipe = NO;
  31. #endif
  32. self.tableView.allowsMultipleSelectionDuringEditing = YES;
  33. [self reloadData];
  34. }
  35. - (void)viewWillAppear:(BOOL)animated {
  36. [super viewWillAppear:animated];
  37. [self setupDefaultBarItems];
  38. }
  39. #pragma mark - Private
  40. - (void)reloadData {
  41. // We assume the bookmarks aren't going to change out from under us, since
  42. // presenting any other tool via keyboard shortcuts should dismiss us first
  43. self.bookmarks = FLEXBookmarkManager.bookmarks;
  44. self.title = [NSString stringWithFormat:@"Bookmarks (%@)", @(self.bookmarks.count)];
  45. }
  46. - (void)setupDefaultBarItems {
  47. self.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(Done, self, @selector(dismissAnimated));
  48. #if !TARGET_OS_TV
  49. self.toolbarItems = @[
  50. UIBarButtonItem.flex_flexibleSpace,
  51. FLEXBarButtonItemSystem(Edit, self, @selector(toggleEditing)),
  52. ];
  53. // Disable editing if no bookmarks available
  54. self.toolbarItems.lastObject.enabled = self.bookmarks.count > 0;
  55. #endif
  56. }
  57. - (void)setupEditingBarItems {
  58. self.navigationItem.rightBarButtonItem = nil;
  59. #if !TARGET_OS_TV
  60. self.toolbarItems = @[
  61. [UIBarButtonItem flex_itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed:)],
  62. UIBarButtonItem.flex_flexibleSpace,
  63. // We use a non-system done item because we change its title dynamically
  64. [UIBarButtonItem flex_doneStyleitemWithTitle:@"Done" target:self action:@selector(toggleEditing)]
  65. ];
  66. self.toolbarItems.firstObject.tintColor = FLEXColor.destructiveColor;
  67. #endif
  68. }
  69. - (FLEXExplorerViewController *)corePresenter {
  70. // We must be presented by a FLEXExplorerViewController, or presented
  71. // by another view controller that was presented by FLEXExplorerViewController
  72. FLEXExplorerViewController *presenter = (id)self.presentingViewController;
  73. presenter = (id)presenter.presentingViewController ?: presenter;
  74. presenter = (id)presenter.presentingViewController ?: presenter;
  75. NSAssert(
  76. [presenter isKindOfClass:[FLEXExplorerViewController class]],
  77. @"The bookmarks view controller expects to be presented by the explorer controller"
  78. );
  79. return presenter;
  80. }
  81. #pragma mark Button Actions
  82. - (void)dismissAnimated {
  83. [self dismissAnimated:nil];
  84. }
  85. - (void)dismissAnimated:(id)selectedObject {
  86. if (selectedObject) {
  87. UIViewController *explorer = [FLEXObjectExplorerFactory
  88. explorerViewControllerForObject:selectedObject
  89. ];
  90. if ([self.presentingViewController isKindOfClass:[FLEXNavigationController class]]) {
  91. // I am presented on an existing navigation stack, so
  92. // dismiss myself and push the bookmark there
  93. UINavigationController *presenter = (id)self.presentingViewController;
  94. [presenter dismissViewControllerAnimated:YES completion:^{
  95. [presenter pushViewController:explorer animated:YES];
  96. }];
  97. } else {
  98. // Dismiss myself and present explorer
  99. UIViewController *presenter = self.corePresenter;
  100. [presenter dismissViewControllerAnimated:YES completion:^{
  101. [presenter presentViewController:[FLEXNavigationController
  102. withRootViewController:explorer
  103. ] animated:YES completion:nil];
  104. }];
  105. }
  106. } else {
  107. // Just dismiss myself
  108. [self dismissViewControllerAnimated:YES completion:nil];
  109. }
  110. }
  111. - (void)toggleEditing {
  112. NSArray<NSIndexPath *> *selected = self.tableView.indexPathsForSelectedRows;
  113. self.editing = !self.editing;
  114. if (self.isEditing) {
  115. [self setupEditingBarItems];
  116. } else {
  117. [self setupDefaultBarItems];
  118. // Get index set of bookmarks to close
  119. NSMutableIndexSet *indexes = [NSMutableIndexSet new];
  120. for (NSIndexPath *ip in selected) {
  121. [indexes addIndex:ip.row];
  122. }
  123. if (selected.count) {
  124. // Close bookmarks and update data source
  125. [FLEXBookmarkManager.bookmarks removeObjectsAtIndexes:indexes];
  126. [self reloadData];
  127. // Remove deleted rows
  128. [self.tableView deleteRowsAtIndexPaths:selected withRowAnimation:UITableViewRowAnimationAutomatic];
  129. }
  130. }
  131. }
  132. - (void)closeAllButtonPressed:(UIBarButtonItem *)sender {
  133. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  134. NSInteger count = self.bookmarks.count;
  135. NSString *title = FLEXPluralFormatString(count, @"Remove %@ bookmarks", @"Remove %@ bookmark");
  136. make.button(title).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  137. [self closeAll];
  138. [self toggleEditing];
  139. });
  140. make.button(@"Cancel").cancelStyle();
  141. } showFrom:self source:sender];
  142. }
  143. - (void)closeAll {
  144. NSInteger rowCount = self.bookmarks.count;
  145. // Close bookmarks and update data source
  146. [FLEXBookmarkManager.bookmarks removeAllObjects];
  147. [self reloadData];
  148. // Delete rows from table view
  149. NSArray<NSIndexPath *> *allRows = [NSArray flex_forEachUpTo:rowCount map:^id(NSUInteger row) {
  150. return [NSIndexPath indexPathForRow:row inSection:0];
  151. }];
  152. [self.tableView deleteRowsAtIndexPaths:allRows withRowAnimation:UITableViewRowAnimationAutomatic];
  153. }
  154. #pragma mark - Table View Data Source
  155. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  156. return self.bookmarks.count;
  157. }
  158. - (UITableViewCell *)tableView:(FLEXTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXDetailCell forIndexPath:indexPath];
  160. id object = self.bookmarks[indexPath.row];
  161. cell.textLabel.text = [FLEXRuntimeUtility safeDescriptionForObject:object];
  162. cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ — %p", [object class], object];
  163. return cell;
  164. }
  165. #pragma mark - Table View Delegate
  166. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  167. if (self.editing) {
  168. // Case: editing with multi-select
  169. #if !TARGET_OS_TV
  170. self.toolbarItems.lastObject.title = @"Remove Selected";
  171. self.toolbarItems.lastObject.tintColor = FLEXColor.destructiveColor;
  172. #endif
  173. } else {
  174. // Case: selected a bookmark
  175. [self dismissAnimated:self.bookmarks[indexPath.row]];
  176. }
  177. }
  178. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  179. NSParameterAssert(self.editing);
  180. if (tableView.indexPathsForSelectedRows.count == 0) {
  181. #if !TARGET_OS_TV
  182. self.toolbarItems.lastObject.title = @"Done";
  183. self.toolbarItems.lastObject.tintColor = self.view.tintColor;
  184. #endif
  185. }
  186. }
  187. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  188. return YES;
  189. }
  190. - (void)tableView:(UITableView *)table
  191. commitEditingStyle:(UITableViewCellEditingStyle)edit
  192. forRowAtIndexPath:(NSIndexPath *)indexPath {
  193. NSParameterAssert(edit == UITableViewCellEditingStyleDelete);
  194. // Remove bookmark and update data source
  195. [FLEXBookmarkManager.bookmarks removeObjectAtIndex:indexPath.row];
  196. [self reloadData];
  197. // Delete row from table view
  198. [table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  199. }
  200. @end