FLEXManager.m 3.0 KB

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