FLEXObjectExplorerFactory.m 6.7 KB

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