FLEXManager.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. @interface FLEXManager () <FLEXWindowEventDelegate, FLEXExplorerViewControllerDelegate>
  16. @property (nonatomic, strong) FLEXWindow *explorerWindow;
  17. @property (nonatomic, strong) FLEXExplorerViewController *explorerViewController;
  18. @property (nonatomic, readonly, strong) NSMutableArray *userGlobalEntries;
  19. @end
  20. @implementation FLEXManager
  21. + (instancetype)sharedManager
  22. {
  23. static FLEXManager *sharedManager = nil;
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. sharedManager = [[[self class] alloc] init];
  27. });
  28. return sharedManager;
  29. }
  30. - (instancetype)init
  31. {
  32. self = [super init];
  33. if (self) {
  34. _userGlobalEntries = [[NSMutableArray alloc] init];
  35. }
  36. return self;
  37. }
  38. - (FLEXWindow *)explorerWindow
  39. {
  40. NSAssert([NSThread isMainThread], @"You must use %@ from the main thread only.", NSStringFromClass([self class]));
  41. if (!_explorerWindow) {
  42. _explorerWindow = [[FLEXWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  43. _explorerWindow.eventDelegate = self;
  44. _explorerWindow.rootViewController = self.explorerViewController;
  45. }
  46. return _explorerWindow;
  47. }
  48. - (FLEXExplorerViewController *)explorerViewController
  49. {
  50. if (!_explorerViewController) {
  51. _explorerViewController = [[FLEXExplorerViewController alloc] init];
  52. _explorerViewController.delegate = self;
  53. }
  54. return _explorerViewController;
  55. }
  56. - (void)showExplorer
  57. {
  58. self.explorerWindow.hidden = NO;
  59. }
  60. - (void)hideExplorer
  61. {
  62. self.explorerWindow.hidden = YES;
  63. }
  64. - (BOOL)isHidden
  65. {
  66. return self.explorerWindow.isHidden;
  67. }
  68. - (BOOL)isNetworkDebuggingEnabled
  69. {
  70. return [FLEXNetworkObserver isEnabled];
  71. }
  72. - (void)setNetworkDebuggingEnabled:(BOOL)networkDebuggingEnabled
  73. {
  74. [FLEXNetworkObserver setEnabled:networkDebuggingEnabled];
  75. }
  76. #pragma mark - FLEXWindowEventDelegate
  77. - (BOOL)shouldHandleTouchAtPoint:(CGPoint)pointInWindow
  78. {
  79. // Ask the explorer view controller
  80. return [self.explorerViewController shouldReceiveTouchAtWindowPoint:pointInWindow];
  81. }
  82. #pragma mark - FLEXExplorerViewControllerDelegate
  83. - (void)explorerViewControllerDidFinish:(FLEXExplorerViewController *)explorerViewController
  84. {
  85. [self hideExplorer];
  86. }
  87. #pragma mark - Extensions
  88. - (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  89. {
  90. NSParameterAssert(entryName);
  91. NSParameterAssert(objectFutureBlock);
  92. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  93. entryName = entryName.copy;
  94. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  95. return entryName;
  96. } viewControllerFuture:^UIViewController *{
  97. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  98. }];
  99. [self.userGlobalEntries addObject:entry];
  100. }
  101. - (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock
  102. {
  103. NSParameterAssert(entryName);
  104. NSParameterAssert(viewControllerFutureBlock);
  105. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  106. entryName = entryName.copy;
  107. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  108. return entryName;
  109. } viewControllerFuture:^UIViewController *{
  110. UIViewController *viewController = viewControllerFutureBlock();
  111. NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
  112. return viewController;
  113. }];
  114. [self.userGlobalEntries addObject:entry];
  115. }
  116. @end