FLEXTabsViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // FLEXTabsViewController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 2/4/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXTabsViewController.h"
  9. #import "FLEXNavigationController.h"
  10. #import "FLEXTabList.h"
  11. #import "FLEXTableView.h"
  12. #import "FLEXUtility.h"
  13. #import "FLEXColor.h"
  14. #import "UIBarButtonItem+FLEX.h"
  15. #import "FLEXExplorerViewController.h"
  16. #import "FLEXGlobalsViewController.h"
  17. @interface FLEXTabsViewController ()
  18. @property (nonatomic, copy) NSArray<UINavigationController *> *openTabs;
  19. @property (nonatomic, copy) NSArray<UIImage *> *tabSnapshots;
  20. @property (nonatomic) NSInteger activeIndex;
  21. @property (nonatomic) BOOL presentNewActiveTabOnDismiss;
  22. @property (nonatomic, readonly) FLEXExplorerViewController *corePresenter;
  23. @end
  24. @implementation FLEXTabsViewController
  25. #pragma mark - Initialization
  26. - (id)init {
  27. return [self initWithStyle:UITableViewStylePlain];
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.title = @"Open Tabs";
  32. self.navigationController.hidesBarsOnSwipe = NO;
  33. self.tableView.allowsMultipleSelectionDuringEditing = YES;
  34. [FLEXTabList.sharedList updateSnapshotForActiveTab];
  35. [self reloadData:NO];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. [self setupDefaultBarItems];
  40. }
  41. #pragma mark - Private
  42. /// @param trackActiveTabDelta whether to check if the active
  43. /// tab changed and needs to be presented upon "Done" dismissal.
  44. /// @return whether the active tab changed or not (if there are any tabs left)
  45. - (BOOL)reloadData:(BOOL)trackActiveTabDelta {
  46. BOOL activeTabDidChange = NO;
  47. FLEXTabList *list = FLEXTabList.sharedList;
  48. // Flag to enable check to determine whether
  49. if (trackActiveTabDelta) {
  50. NSInteger oldActiveIndex = self.activeIndex;
  51. if (oldActiveIndex != list.activeTabIndex && list.activeTabIndex != NSNotFound) {
  52. self.presentNewActiveTabOnDismiss = YES;
  53. activeTabDidChange = YES;
  54. } else if (self.presentNewActiveTabOnDismiss) {
  55. // If we had something to present before, now we don't
  56. // (i.e. activeTabIndex == NSNotFound)
  57. self.presentNewActiveTabOnDismiss = NO;
  58. }
  59. }
  60. // We assume the tabs aren't going to change out from under us, since
  61. // presenting any other tool via keyboard shortcuts should dismiss us first
  62. self.openTabs = list.openTabs;
  63. self.tabSnapshots = list.openTabSnapshots;
  64. self.activeIndex = list.activeTabIndex;
  65. return activeTabDidChange;
  66. }
  67. - (void)reloadActiveTabRowIfChanged:(BOOL)activeTabChanged {
  68. // Refresh the newly active tab row if needed
  69. if (activeTabChanged) {
  70. NSIndexPath *active = [NSIndexPath
  71. indexPathForRow:self.activeIndex inSection:0
  72. ];
  73. [self.tableView reloadRowsAtIndexPaths:@[active] withRowAnimation:UITableViewRowAnimationNone];
  74. }
  75. }
  76. - (void)setupDefaultBarItems {
  77. self.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(Done, self, @selector(dismissAnimated));
  78. self.toolbarItems = @[
  79. UIBarButtonItem.flex_fixedSpace,
  80. UIBarButtonItem.flex_flexibleSpace,
  81. FLEXBarButtonItemSystem(Add, self, @selector(addTabButtonPressed)),
  82. UIBarButtonItem.flex_flexibleSpace,
  83. FLEXBarButtonItemSystem(Edit, self, @selector(toggleEditing)),
  84. ];
  85. // Disable editing if no tabs available
  86. self.toolbarItems.lastObject.enabled = self.openTabs.count > 0;
  87. }
  88. - (void)setupEditingBarItems {
  89. self.navigationItem.rightBarButtonItem = nil;
  90. self.toolbarItems = @[
  91. [UIBarButtonItem itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed)],
  92. UIBarButtonItem.flex_flexibleSpace,
  93. [UIBarButtonItem disabledSystemItem:UIBarButtonSystemItemAdd],
  94. UIBarButtonItem.flex_flexibleSpace,
  95. // We use a non-system done item because we change its title dynamically
  96. [UIBarButtonItem doneStyleitemWithTitle:@"Done" target:self action:@selector(toggleEditing)]
  97. ];
  98. self.toolbarItems.firstObject.tintColor = FLEXColor.destructiveColor;
  99. }
  100. - (FLEXExplorerViewController *)corePresenter {
  101. // We must be presented by a FLEXExplorerViewController, or presented
  102. // by another view controller that was presented by FLEXExplorerViewController
  103. FLEXExplorerViewController *presenter = (id)self.presentingViewController;
  104. presenter = (id)presenter.presentingViewController ?: presenter;
  105. NSAssert(
  106. [presenter isKindOfClass:[FLEXExplorerViewController class]],
  107. @"The tabs view controller expects to be presented by the explorer controller"
  108. );
  109. return presenter;
  110. }
  111. #pragma mark Button Actions
  112. - (void)dismissAnimated {
  113. if (self.presentNewActiveTabOnDismiss) {
  114. // The active tab was closed so we need to present the new one
  115. UIViewController *activeTab = FLEXTabList.sharedList.activeTab;
  116. FLEXExplorerViewController *presenter = self.corePresenter;
  117. [presenter dismissViewControllerAnimated:YES completion:^{
  118. [presenter presentViewController:activeTab animated:YES completion:nil];
  119. }];
  120. } else if (self.activeIndex == NSNotFound) {
  121. // The only tab was closed
  122. [self.corePresenter dismissViewControllerAnimated:YES completion:nil];
  123. } else {
  124. // Simple dismiss with the same active tab
  125. [self dismissViewControllerAnimated:YES completion:nil];
  126. }
  127. }
  128. - (void)toggleEditing {
  129. NSArray<NSIndexPath *> *selected = self.tableView.indexPathsForSelectedRows;
  130. self.editing = !self.editing;
  131. if (self.isEditing) {
  132. [self setupEditingBarItems];
  133. } else {
  134. [self setupDefaultBarItems];
  135. // Get index set of tabs to close
  136. NSMutableIndexSet *indexes = [NSMutableIndexSet new];
  137. for (NSIndexPath *ip in selected) {
  138. [indexes addIndex:ip.row];
  139. }
  140. if (selected.count) {
  141. // Close tabs and update data source
  142. [FLEXTabList.sharedList closeTabsAtIndexes:indexes];
  143. BOOL activeTabChanged = [self reloadData:YES];
  144. // Remove deleted rows
  145. [self.tableView deleteRowsAtIndexPaths:selected withRowAnimation:UITableViewRowAnimationAutomatic];
  146. // Refresh the newly active tab row if needed
  147. [self reloadActiveTabRowIfChanged:activeTabChanged];
  148. }
  149. }
  150. }
  151. - (void)addTabButtonPressed {
  152. FLEXExplorerViewController *presenter = self.corePresenter;
  153. [presenter dismissViewControllerAnimated:YES completion:^{
  154. [presenter presentViewController:[FLEXNavigationController
  155. withRootViewController:[FLEXGlobalsViewController new]
  156. ] animated:YES completion:nil];
  157. }];
  158. }
  159. - (void)closeAllButtonPressed {
  160. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  161. NSInteger count = self.openTabs.count;
  162. NSString *title = FLEXPluralFormatString(count, @"Close %@ tabs", @"Close %@ tab");
  163. make.button(title).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  164. [self closeAllTabs];
  165. [self toggleEditing];
  166. });
  167. make.button(@"Cancel").cancelStyle();
  168. } showFrom:self];
  169. }
  170. - (void)closeAllTabs {
  171. NSInteger rowCount = self.openTabs.count;
  172. // Close tabs and update data source
  173. [FLEXTabList.sharedList closeAllTabs];
  174. [self reloadData:YES];
  175. // Delete rows from table view
  176. NSArray<NSIndexPath *> *allRows = [NSArray flex_forEachUpTo:rowCount map:^id(NSUInteger row) {
  177. return [NSIndexPath indexPathForRow:row inSection:0];
  178. }];
  179. [self.tableView deleteRowsAtIndexPaths:allRows withRowAnimation:UITableViewRowAnimationAutomatic];
  180. }
  181. #pragma mark - Table View Data Source
  182. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  183. return self.openTabs.count;
  184. }
  185. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  186. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXDetailCell forIndexPath:indexPath];
  187. UINavigationController *tab = self.openTabs[indexPath.row];
  188. cell.imageView.image = self.tabSnapshots[indexPath.row];
  189. cell.textLabel.text = tab.topViewController.title;
  190. cell.detailTextLabel.text = FLEXPluralString(tab.viewControllers.count, @"pages", @"page");
  191. if (!cell.tag) {
  192. cell.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
  193. cell.detailTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
  194. cell.tag = 1;
  195. }
  196. if (indexPath.row == self.activeIndex) {
  197. cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
  198. } else {
  199. cell.backgroundColor = FLEXColor.primaryBackgroundColor;
  200. }
  201. return cell;
  202. }
  203. #pragma mark - Table View Delegate
  204. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  205. if (self.editing) {
  206. // Case: editing with multi-select
  207. self.toolbarItems.lastObject.title = @"Close Selected";
  208. self.toolbarItems.lastObject.tintColor = FLEXColor.destructiveColor;
  209. } else {
  210. if (self.activeIndex == indexPath.row && self.corePresenter != self.presentingViewController) {
  211. // Case: selected the already active tab
  212. [self dismissAnimated];
  213. } else {
  214. // Case: selected a different tab,
  215. // or selected a tab when presented from the FLEX toolbar
  216. FLEXTabList.sharedList.activeTabIndex = indexPath.row;
  217. self.presentNewActiveTabOnDismiss = YES;
  218. [self dismissAnimated];
  219. }
  220. }
  221. }
  222. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  223. NSParameterAssert(self.editing);
  224. if (tableView.indexPathsForSelectedRows.count == 0) {
  225. self.toolbarItems.lastObject.title = @"Done";
  226. self.toolbarItems.lastObject.tintColor = self.view.tintColor;
  227. }
  228. }
  229. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  230. return YES;
  231. }
  232. - (void)tableView:(UITableView *)table
  233. commitEditingStyle:(UITableViewCellEditingStyle)edit
  234. forRowAtIndexPath:(NSIndexPath *)indexPath {
  235. NSParameterAssert(edit == UITableViewCellEditingStyleDelete);
  236. // Close tab and update data source
  237. [FLEXTabList.sharedList closeTab:self.openTabs[indexPath.row]];
  238. BOOL activeTabChanged = [self reloadData:YES];
  239. // Delete row from table view
  240. [table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  241. // Refresh the newly active tab row if needed
  242. [self reloadActiveTabRowIfChanged:activeTabChanged];
  243. }
  244. @end