FLEXManager.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // FLEXManager.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXManager.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXWindow.h"
  11. #import "FLEXGlobalsTableViewControllerEntry.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXObjectExplorerViewController.h"
  14. #import "FLEXNetworkObserver.h"
  15. #import "FLEXNetworkRecorder.h"
  16. @interface FLEXManager () <FLEXWindowEventDelegate, FLEXExplorerViewControllerDelegate>
  17. @property (nonatomic, strong) FLEXWindow *explorerWindow;
  18. @property (nonatomic, strong) FLEXExplorerViewController *explorerViewController;
  19. @property (nonatomic, readonly, strong) NSMutableArray *userGlobalEntries;
  20. @end
  21. @implementation FLEXManager
  22. + (instancetype)sharedManager
  23. {
  24. static FLEXManager *sharedManager = nil;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. sharedManager = [[[self class] alloc] init];
  28. });
  29. return sharedManager;
  30. }
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self) {
  35. _userGlobalEntries = [[NSMutableArray alloc] init];
  36. }
  37. return self;
  38. }
  39. - (FLEXWindow *)explorerWindow
  40. {
  41. NSAssert([NSThread isMainThread], @"You must use %@ from the main thread only.", NSStringFromClass([self class]));
  42. if (!_explorerWindow) {
  43. _explorerWindow = [[FLEXWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  44. _explorerWindow.eventDelegate = self;
  45. _explorerWindow.rootViewController = self.explorerViewController;
  46. }
  47. return _explorerWindow;
  48. }
  49. - (FLEXExplorerViewController *)explorerViewController
  50. {
  51. if (!_explorerViewController) {
  52. _explorerViewController = [[FLEXExplorerViewController alloc] init];
  53. _explorerViewController.delegate = self;
  54. }
  55. return _explorerViewController;
  56. }
  57. - (void)showExplorer
  58. {
  59. self.explorerWindow.hidden = NO;
  60. }
  61. - (void)hideExplorer
  62. {
  63. self.explorerWindow.hidden = YES;
  64. }
  65. - (BOOL)isHidden
  66. {
  67. return self.explorerWindow.isHidden;
  68. }
  69. - (BOOL)isNetworkDebuggingEnabled
  70. {
  71. return [FLEXNetworkObserver isEnabled];
  72. }
  73. - (void)setNetworkDebuggingEnabled:(BOOL)networkDebuggingEnabled
  74. {
  75. [FLEXNetworkObserver setEnabled:networkDebuggingEnabled];
  76. }
  77. - (NSUInteger)networkResponseCacheByteLimit
  78. {
  79. return [[FLEXNetworkRecorder defaultRecorder] responseCacheByteLimit];
  80. }
  81. - (void)setNetworkResponseCacheByteLimit:(NSUInteger)networkResponseCacheByteLimit
  82. {
  83. [[FLEXNetworkRecorder defaultRecorder] setResponseCacheByteLimit:networkResponseCacheByteLimit];
  84. }
  85. #pragma mark - FLEXWindowEventDelegate
  86. - (BOOL)shouldHandleTouchAtPoint:(CGPoint)pointInWindow
  87. {
  88. // Ask the explorer view controller
  89. return [self.explorerViewController shouldReceiveTouchAtWindowPoint:pointInWindow];
  90. }
  91. - (BOOL)canBecomeKeyWindow
  92. {
  93. // Only when the explorer view controller wants it because it needs to accept key input & affect the status bar.
  94. return [self.explorerViewController wantsWindowToBecomeKey];
  95. }
  96. #pragma mark - FLEXExplorerViewControllerDelegate
  97. - (void)explorerViewControllerDidFinish:(FLEXExplorerViewController *)explorerViewController
  98. {
  99. [self hideExplorer];
  100. }
  101. #pragma mark - Extensions
  102. - (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  103. {
  104. NSParameterAssert(entryName);
  105. NSParameterAssert(objectFutureBlock);
  106. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  107. entryName = entryName.copy;
  108. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  109. return entryName;
  110. } viewControllerFuture:^UIViewController *{
  111. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  112. }];
  113. [self.userGlobalEntries addObject:entry];
  114. }
  115. - (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock
  116. {
  117. NSParameterAssert(entryName);
  118. NSParameterAssert(viewControllerFutureBlock);
  119. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  120. entryName = entryName.copy;
  121. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  122. return entryName;
  123. } viewControllerFuture:^UIViewController *{
  124. UIViewController *viewController = viewControllerFutureBlock();
  125. NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
  126. return viewController;
  127. }];
  128. [self.userGlobalEntries addObject:entry];
  129. }
  130. @end