FLEXObjectExplorerFactory.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // FLEXObjectExplorerFactory.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/15/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectExplorerFactory.h"
  9. #import "FLEXGlobalsTableViewController.h"
  10. #import "FLEXAlert.h"
  11. #import "FLEXClassShortcuts.h"
  12. #import "FLEXViewShortcuts.h"
  13. #import "FLEXViewControllerShortcuts.h"
  14. #import "FLEXImageShortcuts.h"
  15. #import "FLEXLayerShortcuts.h"
  16. #import "FLEXColorPreviewSection.h"
  17. #import "FLEXDefaultsContentSection.h"
  18. #import "FLEXBundleShortcuts.h"
  19. #import "FLEXBlockShortcuts.h"
  20. #import <objc/runtime.h>
  21. @implementation FLEXObjectExplorerFactory
  22. static NSMutableDictionary<Class, Class> *classesToRegisteredSections = nil;
  23. + (void)initialize
  24. {
  25. if (self == [FLEXObjectExplorerFactory class]) {
  26. #define ClassKey(name) (Class<NSCopying>)[name class]
  27. #define ClassKeyByName(str) (Class<NSCopying>)NSClassFromString(@ #str)
  28. classesToRegisteredSections = [NSMutableDictionary dictionaryWithDictionary:@{
  29. ClassKey(NSArray) : [FLEXCollectionContentSection class],
  30. ClassKey(NSSet) : [FLEXCollectionContentSection class],
  31. ClassKey(NSDictionary) : [FLEXCollectionContentSection class],
  32. ClassKey(NSUserDefaults) : [FLEXDefaultsContentSection class],
  33. ClassKey(UIViewController) : [FLEXViewControllerShortcuts class],
  34. ClassKey(UIView) : [FLEXViewShortcuts class],
  35. ClassKey(UIImage) : [FLEXImageShortcuts class],
  36. ClassKey(CALayer) : [FLEXLayerShortcuts class],
  37. ClassKey(UIColor) : [FLEXColorPreviewSection class],
  38. ClassKey(NSBundle) : [FLEXBundleShortcuts class],
  39. ClassKeyByName(NSBlock) : [FLEXBlockShortcuts class],
  40. }];
  41. #undef ClassKey
  42. #undef ClassKeyByName
  43. }
  44. }
  45. + (FLEXObjectExplorerViewController *)explorerViewControllerForObject:(id)object
  46. {
  47. // Can't explore nil
  48. if (!object) {
  49. return nil;
  50. }
  51. // If we're given an object, this will look up it's class hierarchy
  52. // until it finds a registration. This will work for KVC classes,
  53. // since they are children of the original class, and not siblings.
  54. // If we are given an object, object_getClass will return a metaclass,
  55. // and the same thing will happen. FLEXClassShortcuts is the default
  56. // shortcut section for NSObject.
  57. //
  58. // TODO: rename it to FLEXNSObjectShortcuts or something?
  59. Class sectionClass = nil;
  60. Class cls = object_getClass(object);
  61. do {
  62. sectionClass = classesToRegisteredSections[(Class<NSCopying>)cls];
  63. } while (!sectionClass && (cls = [cls superclass]));
  64. if (!sectionClass) {
  65. sectionClass = [FLEXShortcutsSection class];
  66. }
  67. return [FLEXObjectExplorerViewController
  68. exploringObject:object
  69. customSection:[sectionClass forObject:object]
  70. ];
  71. }
  72. + (void)registerExplorerSection:(Class)explorerClass forClass:(Class)objectClass
  73. {
  74. classesToRegisteredSections[(Class<NSCopying>)objectClass] = explorerClass;
  75. }
  76. #pragma mark - FLEXGlobalsEntry
  77. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  78. {
  79. switch (row) {
  80. case FLEXGlobalsRowAppDelegate:
  81. return @"🎟 App Delegate";
  82. case FLEXGlobalsRowKeyWindow:
  83. return @"🔑 Key Window";
  84. case FLEXGlobalsRowRootViewController:
  85. return @"🌴 Root View Controller";
  86. case FLEXGlobalsRowProcessInfo:
  87. return @"🚦 NSProcessInfo.processInfo";
  88. case FLEXGlobalsRowUserDefaults:
  89. return @"💾 Preferences";
  90. case FLEXGlobalsRowMainBundle:
  91. return @"📦 NSBundle.mainBundle";
  92. case FLEXGlobalsRowApplication:
  93. return @"🚀 UIApplication.sharedApplication";
  94. case FLEXGlobalsRowMainScreen:
  95. return @"💻 UIScreen.mainScreen";
  96. case FLEXGlobalsRowCurrentDevice:
  97. return @"📱 UIDevice.currentDevice";
  98. case FLEXGlobalsRowPasteboard:
  99. return @"📋 UIPasteboard.generalPasteboard";
  100. default: return nil;
  101. }
  102. }
  103. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row
  104. {
  105. switch (row) {
  106. case FLEXGlobalsRowAppDelegate: {
  107. id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
  108. return [self explorerViewControllerForObject:appDelegate];
  109. }
  110. case FLEXGlobalsRowProcessInfo:
  111. return [self explorerViewControllerForObject:NSProcessInfo.processInfo];
  112. case FLEXGlobalsRowUserDefaults:
  113. return [self explorerViewControllerForObject:NSUserDefaults.standardUserDefaults];
  114. case FLEXGlobalsRowMainBundle:
  115. return [self explorerViewControllerForObject:NSBundle.mainBundle];
  116. case FLEXGlobalsRowApplication:
  117. return [self explorerViewControllerForObject:UIApplication.sharedApplication];
  118. case FLEXGlobalsRowMainScreen:
  119. return [self explorerViewControllerForObject:UIScreen.mainScreen];
  120. case FLEXGlobalsRowCurrentDevice:
  121. return [self explorerViewControllerForObject:UIDevice.currentDevice];
  122. case FLEXGlobalsRowPasteboard:
  123. return [self explorerViewControllerForObject:UIPasteboard.generalPasteboard];
  124. case FLEXGlobalsRowKeyWindow:
  125. return [FLEXObjectExplorerFactory
  126. explorerViewControllerForObject:FLEXUtility.appKeyWindow
  127. ];
  128. case FLEXGlobalsRowRootViewController: {
  129. id<UIApplicationDelegate> delegate = UIApplication.sharedApplication.delegate;
  130. if ([delegate respondsToSelector:@selector(window)]) {
  131. return [self explorerViewControllerForObject:delegate.window.rootViewController];
  132. }
  133. return nil;
  134. }
  135. default: return nil;
  136. }
  137. }
  138. + (FLEXGlobalsTableViewControllerRowAction)globalsEntryRowAction:(FLEXGlobalsRow)row
  139. {
  140. switch (row) {
  141. case FLEXGlobalsRowRootViewController: {
  142. // Check if the app delegate responds to -window. If not, present an alert
  143. return ^(FLEXGlobalsTableViewController *host) {
  144. id<UIApplicationDelegate> delegate = UIApplication.sharedApplication.delegate;
  145. if ([delegate respondsToSelector:@selector(window)]) {
  146. UIViewController *explorer = [self explorerViewControllerForObject:
  147. delegate.window.rootViewController
  148. ];
  149. [host.navigationController pushViewController:explorer animated:YES];
  150. } else {
  151. NSString *msg = @"The app delegate doesn't respond to -window";
  152. [FLEXAlert showAlert:@":(" message:msg from:host];
  153. }
  154. };
  155. }
  156. default: return nil;
  157. }
  158. }
  159. @end