FLEXTabList.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // FLEXTabList.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 2/1/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXTabList.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXTabList () {
  11. NSMutableArray *_openTabs;
  12. NSMutableArray *_openTabSnapshots;
  13. }
  14. @end
  15. #pragma mark -
  16. @implementation FLEXTabList
  17. #pragma mark Initialization
  18. + (FLEXTabList *)sharedList {
  19. static FLEXTabList *sharedList = nil;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. sharedList = [self new];
  23. });
  24. return sharedList;
  25. }
  26. - (id)init {
  27. self = [super init];
  28. if (self) {
  29. _openTabs = [NSMutableArray new];
  30. _openTabSnapshots = [NSMutableArray new];
  31. _activeTabIndex = NSNotFound;
  32. }
  33. return self;
  34. }
  35. #pragma mark Private
  36. - (void)chooseNewActiveTab {
  37. if (self.openTabs.count) {
  38. self.activeTabIndex = self.openTabs.count - 1;
  39. } else {
  40. self.activeTabIndex = NSNotFound;
  41. }
  42. }
  43. #pragma mark Public
  44. - (void)setActiveTabIndex:(NSInteger)idx {
  45. NSParameterAssert(idx < self.openTabs.count || idx == NSNotFound);
  46. if (_activeTabIndex == idx) return;
  47. _activeTabIndex = idx;
  48. _activeTab = (idx == NSNotFound) ? nil : self.openTabs[idx];
  49. }
  50. - (void)addTab:(UINavigationController *)newTab {
  51. NSParameterAssert(newTab);
  52. // Update snapshot of the last active tab
  53. if (self.activeTab) {
  54. [self updateSnapshotForActiveTab];
  55. }
  56. // Add new tab and snapshot,
  57. // update active tab and index
  58. [_openTabs addObject:newTab];
  59. [_openTabSnapshots addObject:[FLEXUtility previewImageForView:newTab.view]];
  60. _activeTab = newTab;
  61. _activeTabIndex = self.openTabs.count - 1;
  62. }
  63. - (void)closeTab:(UINavigationController *)tab {
  64. NSParameterAssert(tab);
  65. NSParameterAssert([self.openTabs containsObject:tab]);
  66. NSInteger idx = [self.openTabs indexOfObject:tab];
  67. [self closeTabAtIndex:idx];
  68. }
  69. - (void)closeTabAtIndex:(NSInteger)idx {
  70. NSParameterAssert(idx < self.openTabs.count);
  71. // Remove old tab and snapshot
  72. [_openTabs removeObjectAtIndex:idx];
  73. [_openTabSnapshots removeObjectAtIndex:idx];
  74. // Update active tab and index if needed
  75. if (self.activeTabIndex == idx) {
  76. [self chooseNewActiveTab];
  77. }
  78. }
  79. - (void)closeTabsAtIndexes:(NSIndexSet *)indexes {
  80. // Remove old tabs and snapshot
  81. [_openTabs removeObjectsAtIndexes:indexes];
  82. [_openTabSnapshots removeObjectsAtIndexes:indexes];
  83. // Update active tab and index if needed
  84. if ([indexes containsIndex:self.activeTabIndex]) {
  85. [self chooseNewActiveTab];
  86. }
  87. }
  88. - (void)closeActiveTab {
  89. [self closeTab:self.activeTab];
  90. }
  91. - (void)closeAllTabs {
  92. // Remove tabs and snapshots
  93. [_openTabs removeAllObjects];
  94. [_openTabSnapshots removeAllObjects];
  95. // Update active tab index
  96. self.activeTabIndex = NSNotFound;
  97. }
  98. - (void)updateSnapshotForActiveTab {
  99. if (self.activeTabIndex != NSNotFound) {
  100. UIImage *newSnapshot = [FLEXUtility previewImageForView:self.activeTab.view];
  101. [_openTabSnapshots replaceObjectAtIndex:self.activeTabIndex withObject:newSnapshot];
  102. }
  103. }
  104. @end