FLEXTabsViewController.m 12 KB

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