FLEXTabsViewController.m 12 KB

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