FLEXObjectExplorerFactory.m 6.2 KB

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