FLEXGlobalsTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // FLEXGlobalsTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXLibrariesTableViewController.h"
  11. #import "FLEXClassesTableViewController.h"
  12. #import "FLEXObjectExplorerViewController.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXLiveObjectsTableViewController.h"
  15. #import "FLEXFileBrowserTableViewController.h"
  16. typedef NSString *(^FLEXGlobalsTableViewControllerEntryNameFuture)(void);
  17. typedef UIViewController *(^FLEXGlobalsTableViewControllerViewControllerFuture)(void);
  18. static UIWindow *s_applicationWindow = nil;
  19. static NSMutableArray *s_globalEntries = nil;
  20. @interface FLEXGlobalsTableViewControllerEntry : NSObject
  21. @property (nonatomic, readonly, copy) FLEXGlobalsTableViewControllerEntryNameFuture entryName;
  22. @property (nonatomic, readonly, copy) FLEXGlobalsTableViewControllerViewControllerFuture viewControllerToPush;
  23. + (instancetype)entryWithName:(FLEXGlobalsTableViewControllerEntryNameFuture)name
  24. viewControllerToPush:(FLEXGlobalsTableViewControllerViewControllerFuture)viewControllerToPush;
  25. @end
  26. @implementation FLEXGlobalsTableViewControllerEntry
  27. + (instancetype)entryWithName:(FLEXGlobalsTableViewControllerEntryNameFuture)name
  28. viewControllerToPush:(FLEXGlobalsTableViewControllerViewControllerFuture)viewControllerToPush
  29. {
  30. NSParameterAssert(name);
  31. NSParameterAssert(viewControllerToPush);
  32. FLEXGlobalsTableViewControllerEntry *entry = [[self alloc] init];
  33. entry->_entryName = [name copy];
  34. entry->_viewControllerToPush = [viewControllerToPush copy];
  35. return entry;
  36. }
  37. @end
  38. typedef NS_ENUM(NSUInteger, FLEXGlobalsRow) {
  39. FLEXGlobalsRowAppClasses,
  40. FLEXGlobalsRowSystemLibraries,
  41. FLEXGlobalsRowLiveObjects,
  42. FLEXGlobalsRowAppDelegate,
  43. FLEXGlobalsRowRootViewController,
  44. FLEXGlobalsRowUserDefaults,
  45. FLEXGlobalsRowApplication,
  46. FLEXGlobalsRowKeyWindow,
  47. FLEXGlobalsRowMainScreen,
  48. FLEXGlobalsRowCurrentDevice,
  49. FLEXGlobalsRowFileBrowser,
  50. FLEXGlobalsRowCount
  51. };
  52. @interface FLEXGlobalsTableViewController ()
  53. /// [FLEXGlobalsTableViewControllerEntry *]
  54. @property (nonatomic, readonly, copy) NSArray *entries;
  55. @end
  56. @implementation FLEXGlobalsTableViewController
  57. + (void)initialize
  58. {
  59. if (self == [FLEXGlobalsTableViewController class]) {
  60. [self initializeStandardGlobalEntries];
  61. }
  62. }
  63. + (void)initializeStandardGlobalEntries {
  64. s_globalEntries = [NSMutableArray array];
  65. for (FLEXGlobalsRow defaultRowIndex = 0; defaultRowIndex < FLEXGlobalsRowCount; defaultRowIndex++) {
  66. FLEXGlobalsTableViewControllerEntryNameFuture title = nil;
  67. FLEXGlobalsTableViewControllerViewControllerFuture viewControllerFuture = nil;
  68. switch (defaultRowIndex) {
  69. case FLEXGlobalsRowAppClasses:
  70. title = ^NSString *{
  71. return [NSString stringWithFormat:@"📕 %@ Classes", [FLEXUtility applicationName]];;
  72. };
  73. viewControllerFuture = ^UIViewController *{
  74. FLEXClassesTableViewController *classesViewController = [[FLEXClassesTableViewController alloc] init];
  75. classesViewController.binaryImageName = [FLEXUtility applicationImageName];
  76. return classesViewController;
  77. };
  78. break;
  79. case FLEXGlobalsRowSystemLibraries: {
  80. NSString *titleString = @"📚 System Libraries";
  81. title = ^NSString *{
  82. return titleString;
  83. };
  84. viewControllerFuture = ^UIViewController *{
  85. FLEXLibrariesTableViewController *librariesViewController = [[FLEXLibrariesTableViewController alloc] init];
  86. librariesViewController.title = titleString;
  87. return librariesViewController;
  88. };
  89. break;
  90. }
  91. case FLEXGlobalsRowLiveObjects:
  92. title = ^NSString *{
  93. return @"💩 Heap Objects";
  94. };
  95. viewControllerFuture = ^UIViewController *{
  96. return [[FLEXLiveObjectsTableViewController alloc] init];
  97. };
  98. break;
  99. case FLEXGlobalsRowAppDelegate:
  100. title = ^NSString *{
  101. return [NSString stringWithFormat:@"👉 %@", [[[UIApplication sharedApplication] delegate] class]];
  102. };
  103. viewControllerFuture = ^UIViewController *{
  104. id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
  105. return [FLEXObjectExplorerFactory explorerViewControllerForObject:appDelegate];
  106. };
  107. break;
  108. case FLEXGlobalsRowRootViewController:
  109. title = ^NSString *{
  110. return [NSString stringWithFormat:@"🌴 %@", [[s_applicationWindow rootViewController] class]];
  111. };
  112. viewControllerFuture = ^UIViewController *{
  113. UIViewController *rootViewController = [s_applicationWindow rootViewController];
  114. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  115. };
  116. break;
  117. case FLEXGlobalsRowUserDefaults:
  118. title = ^NSString *{
  119. return @"🚶 +[NSUserDefaults standardUserDefaults]";
  120. };
  121. viewControllerFuture = ^UIViewController *{
  122. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  123. return [FLEXObjectExplorerFactory explorerViewControllerForObject:standardUserDefaults];
  124. };
  125. break;
  126. case FLEXGlobalsRowApplication:
  127. title = ^NSString *{
  128. return @"💾 +[UIApplication sharedApplication]";
  129. };
  130. viewControllerFuture = ^UIViewController *{
  131. UIApplication *sharedApplication = [UIApplication sharedApplication];
  132. return [FLEXObjectExplorerFactory explorerViewControllerForObject:sharedApplication];
  133. };
  134. break;
  135. case FLEXGlobalsRowKeyWindow:
  136. title = ^NSString *{
  137. return @"🔑 -[UIApplication keyWindow]";
  138. };
  139. viewControllerFuture = ^UIViewController *{
  140. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  141. };
  142. break;
  143. case FLEXGlobalsRowMainScreen:
  144. title = ^NSString *{
  145. return @"💻 +[UIScreen mainScreen]";
  146. };
  147. viewControllerFuture = ^UIViewController *{
  148. UIScreen *mainScreen = [UIScreen mainScreen];
  149. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainScreen];
  150. };
  151. break;
  152. case FLEXGlobalsRowCurrentDevice:
  153. title = ^NSString *{
  154. return @"📱 +[UIDevice currentDevice]";
  155. };
  156. viewControllerFuture = ^UIViewController *{
  157. UIDevice *currentDevice = [UIDevice currentDevice];
  158. return [FLEXObjectExplorerFactory explorerViewControllerForObject:currentDevice];
  159. };
  160. break;
  161. case FLEXGlobalsRowFileBrowser:
  162. title = ^NSString *{
  163. return @"📁 File Browser";
  164. };
  165. viewControllerFuture = ^UIViewController *{
  166. return [[FLEXFileBrowserTableViewController alloc] init];
  167. };
  168. break;
  169. case FLEXGlobalsRowCount:
  170. break;
  171. }
  172. NSParameterAssert(title);
  173. NSParameterAssert(viewControllerFuture);
  174. [s_globalEntries addObject:[FLEXGlobalsTableViewControllerEntry entryWithName:title
  175. viewControllerToPush:viewControllerFuture]];
  176. }
  177. }
  178. - (id)initWithStyle:(UITableViewStyle)style
  179. {
  180. self = [super initWithStyle:style];
  181. if (self) {
  182. self.title = @"🌎 Global State";
  183. _entries = [s_globalEntries copy];
  184. }
  185. return self;
  186. }
  187. #pragma mark - Public
  188. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  189. {
  190. s_applicationWindow = applicationWindow;
  191. }
  192. + (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  193. {
  194. NSParameterAssert(entryName);
  195. NSParameterAssert(objectFutureBlock);
  196. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  197. entryName = entryName.copy;
  198. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithName:^NSString *{
  199. return entryName;
  200. }
  201. viewControllerToPush:^UIViewController *
  202. {
  203. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  204. }];
  205. [s_globalEntries addObject:entry];
  206. }
  207. #pragma mark - UIViewController
  208. - (void)viewDidLoad
  209. {
  210. [super viewDidLoad];
  211. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  212. }
  213. #pragma mark -
  214. - (void)donePressed:(id)sender
  215. {
  216. [self.delegate globalsViewControllerDidFinish:self];
  217. }
  218. #pragma mark - Table Data Helpers
  219. - (FLEXGlobalsTableViewControllerEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  220. {
  221. return self.entries[indexPath.row];
  222. }
  223. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  224. {
  225. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  226. return entry.entryName();
  227. }
  228. - (UIViewController *)viewControllerToPushForRowAtIndexPath:(NSIndexPath *)indexPath
  229. {
  230. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  231. return entry.viewControllerToPush();
  232. }
  233. #pragma mark - Table View Data Source
  234. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  235. {
  236. return 1;
  237. }
  238. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  239. {
  240. return [self.entries count];
  241. }
  242. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  243. {
  244. static NSString *CellIdentifier = @"Cell";
  245. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  246. if (!cell) {
  247. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  248. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  249. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  250. }
  251. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  252. return cell;
  253. }
  254. #pragma mark - Table View Delegate
  255. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  256. {
  257. UIViewController *viewControllerToPush = [self viewControllerToPushForRowAtIndexPath:indexPath];
  258. [self.navigationController pushViewController:viewControllerToPush animated:YES];
  259. }
  260. @end