FLEXRuntime+UIKitHelpers.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //
  2. // FLEXRuntime+UIKitHelpers.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/16/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXRuntime+UIKitHelpers.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXPropertyAttributes.h"
  11. #import "FLEXArgumentInputViewFactory.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXFieldEditorViewController.h"
  14. #import "FLEXMethodCallingViewController.h"
  15. #import "FLEXTableView.h"
  16. #import "FLEXUtility.h"
  17. #import "NSArray+Functional.h"
  18. #import "NSString+FLEX.h"
  19. #define FLEXObjectExplorerDefaultsImpl \
  20. - (FLEXObjectExplorerDefaults *)defaults { \
  21. return self.tag; \
  22. } \
  23. \
  24. - (void)setDefaults:(FLEXObjectExplorerDefaults *)defaults { \
  25. self.tag = defaults; \
  26. }
  27. #pragma mark FLEXProperty
  28. @implementation FLEXProperty (UIKitHelpers)
  29. FLEXObjectExplorerDefaultsImpl
  30. /// Decide whether to use potentialTarget or [potentialTarget class] to get or set property
  31. - (id)appropriateTargetForPropertyType:(id)potentialTarget {
  32. if (!object_isClass(potentialTarget)) {
  33. if (self.isClassProperty) {
  34. return [potentialTarget class];
  35. } else {
  36. return potentialTarget;
  37. }
  38. } else {
  39. if (self.isClassProperty) {
  40. return potentialTarget;
  41. } else {
  42. // Instance property with a class object
  43. return nil;
  44. }
  45. }
  46. }
  47. - (BOOL)isEditable {
  48. if (self.attributes.isReadOnly) {
  49. return self.likelySetterExists;
  50. }
  51. const FLEXTypeEncoding *typeEncoding = self.attributes.typeEncoding.UTF8String;
  52. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
  53. }
  54. - (BOOL)isCallable {
  55. return YES;
  56. }
  57. - (id)currentValueWithTarget:(id)object {
  58. return [self getPotentiallyUnboxedValue:
  59. [self appropriateTargetForPropertyType:object]
  60. ];
  61. }
  62. - (id)currentValueBeforeUnboxingWithTarget:(id)object {
  63. return [self getValue:
  64. [self appropriateTargetForPropertyType:object]
  65. ];
  66. }
  67. - (NSString *)previewWithTarget:(id)object {
  68. if (object_isClass(object) && !self.isClassProperty) {
  69. return self.attributes.fullDeclaration;
  70. } else if (self.defaults.wantsDynamicPreviews) {
  71. return [FLEXRuntimeUtility
  72. summaryForObject:[self currentValueWithTarget:object]
  73. ];
  74. }
  75. return nil;
  76. }
  77. - (UIViewController *)viewerWithTarget:(id)object {
  78. id value = [self currentValueWithTarget:object];
  79. return [FLEXObjectExplorerFactory explorerViewControllerForObject:value];
  80. }
  81. - (UIViewController *)editorWithTarget:(id)object {
  82. id target = [self appropriateTargetForPropertyType:object];
  83. return [FLEXFieldEditorViewController target:target property:self];
  84. }
  85. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  86. id targetForValueCheck = [self appropriateTargetForPropertyType:object];
  87. if (!targetForValueCheck) {
  88. // Instance property with a class object
  89. return UITableViewCellAccessoryNone;
  90. }
  91. // We use .tag to store the cached value of .isEditable that is
  92. // initialized by FLEXObjectExplorer in -reloadMetada
  93. if ([self getPotentiallyUnboxedValue:targetForValueCheck]) {
  94. if (self.defaults.isEditable) {
  95. // Editable non-nil value, both
  96. return UITableViewCellAccessoryDetailDisclosureButton;
  97. } else {
  98. // Uneditable non-nil value, chevron only
  99. return UITableViewCellAccessoryDisclosureIndicator;
  100. }
  101. } else {
  102. if (self.defaults.isEditable) {
  103. // Editable nil value, just (i)
  104. return UITableViewCellAccessoryDetailButton;
  105. } else {
  106. // Non-editable nil value, neither
  107. return UITableViewCellAccessoryNone;
  108. }
  109. }
  110. }
  111. - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
  112. #if FLEX_AT_LEAST_IOS13_SDK
  113. - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
  114. Class propertyClass = self.attributes.typeEncoding.flex_typeClass;
  115. // "Explore PropertyClass" for properties with a concrete class name
  116. if (propertyClass) {
  117. NSString *title = [NSString stringWithFormat:@"Explore %@", NSStringFromClass(propertyClass)];
  118. return @[[UIAction actionWithTitle:title image:nil identifier:nil handler:^(UIAction *action) {
  119. UIViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:propertyClass];
  120. [sender.navigationController pushViewController:explorer animated:YES];
  121. }]];
  122. }
  123. return nil;
  124. }
  125. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  126. BOOL returnsObject = self.attributes.typeEncoding.flex_typeIsObjectOrClass;
  127. BOOL targetNotNil = [self appropriateTargetForPropertyType:object] != nil;
  128. NSMutableArray *items = [NSMutableArray arrayWithArray:@[
  129. @"Name", self.name ?: @"",
  130. @"Type", self.attributes.typeEncoding ?: @"",
  131. @"Declaration", self.fullDescription ?: @"",
  132. ]];
  133. if (targetNotNil) {
  134. id value = [self currentValueBeforeUnboxingWithTarget:object];
  135. [items addObjectsFromArray:@[
  136. @"Value Preview", [self previewWithTarget:object],
  137. @"Value Address", returnsObject ? [FLEXUtility addressOfObject:value] : @"",
  138. ]];
  139. }
  140. [items addObjectsFromArray:@[
  141. @"Getter", NSStringFromSelector(self.likelyGetter) ?: @"",
  142. @"Setter", self.likelySetterExists ? NSStringFromSelector(self.likelySetter) : @"",
  143. @"Image Name", self.imageName ?: @"",
  144. @"Attributes", self.attributes.string ?: @"",
  145. @"objc_property", [FLEXUtility pointerToString:self.objc_property],
  146. @"objc_property_attribute_t", [FLEXUtility pointerToString:self.attributes.list],
  147. ]];
  148. return items;
  149. }
  150. - (NSString *)contextualSubtitleWithTarget:(id)object {
  151. id target = [self appropriateTargetForPropertyType:object];
  152. if (target && self.attributes.typeEncoding.flex_typeIsObjectOrClass) {
  153. return [FLEXUtility addressOfObject:[self currentValueBeforeUnboxingWithTarget:target]];
  154. }
  155. return nil;
  156. }
  157. #endif
  158. @end
  159. #pragma mark FLEXIvar
  160. @implementation FLEXIvar (UIKitHelpers)
  161. FLEXObjectExplorerDefaultsImpl
  162. - (BOOL)isEditable {
  163. const FLEXTypeEncoding *typeEncoding = self.typeEncoding.UTF8String;
  164. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
  165. }
  166. - (BOOL)isCallable {
  167. return NO;
  168. }
  169. - (id)currentValueWithTarget:(id)object {
  170. if (!object_isClass(object)) {
  171. return [self getPotentiallyUnboxedValue:object];
  172. }
  173. return nil;
  174. }
  175. - (NSString *)previewWithTarget:(id)object {
  176. if (object_isClass(object)) {
  177. return self.details;
  178. } else if (self.defaults.wantsDynamicPreviews) {
  179. return [FLEXRuntimeUtility
  180. summaryForObject:[self currentValueWithTarget:object]
  181. ];
  182. }
  183. return nil;
  184. }
  185. - (UIViewController *)viewerWithTarget:(id)object {
  186. NSAssert(!object_isClass(object), @"Unreachable state: viewing ivar on class object");
  187. id value = [self currentValueWithTarget:object];
  188. return [FLEXObjectExplorerFactory explorerViewControllerForObject:value];
  189. }
  190. - (UIViewController *)editorWithTarget:(id)object {
  191. NSAssert(!object_isClass(object), @"Unreachable state: editing ivar on class object");
  192. return [FLEXFieldEditorViewController target:object ivar:self];
  193. }
  194. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  195. if (object_isClass(object)) {
  196. return UITableViewCellAccessoryNone;
  197. }
  198. // Could use .isEditable here, but we use .tag for speed since it is cached
  199. if ([self getPotentiallyUnboxedValue:object]) {
  200. if (self.defaults.isEditable) {
  201. // Editable non-nil value, both
  202. return UITableViewCellAccessoryDetailDisclosureButton;
  203. } else {
  204. // Uneditable non-nil value, chevron only
  205. return UITableViewCellAccessoryDisclosureIndicator;
  206. }
  207. } else {
  208. if (self.defaults.isEditable) {
  209. // Editable nil value, just (i)
  210. return UITableViewCellAccessoryDetailButton;
  211. } else {
  212. // Non-editable nil value, neither
  213. return UITableViewCellAccessoryNone;
  214. }
  215. }
  216. }
  217. - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
  218. #if FLEX_AT_LEAST_IOS13_SDK
  219. - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
  220. Class ivarClass = self.typeEncoding.flex_typeClass;
  221. // "Explore PropertyClass" for properties with a concrete class name
  222. if (ivarClass) {
  223. NSString *title = [NSString stringWithFormat:@"Explore %@", NSStringFromClass(ivarClass)];
  224. return @[[UIAction actionWithTitle:title image:nil identifier:nil handler:^(UIAction *action) {
  225. UIViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:ivarClass];
  226. [sender.navigationController pushViewController:explorer animated:YES];
  227. }]];
  228. }
  229. return nil;
  230. }
  231. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  232. BOOL isInstance = !object_isClass(object);
  233. BOOL returnsObject = self.typeEncoding.flex_typeIsObjectOrClass;
  234. id value = isInstance ? [self getValue:object] : nil;
  235. NSMutableArray *items = [NSMutableArray arrayWithArray:@[
  236. @"Name", self.name ?: @"",
  237. @"Type", self.typeEncoding ?: @"",
  238. @"Declaration", self.description ?: @"",
  239. ]];
  240. if (isInstance) {
  241. [items addObjectsFromArray:@[
  242. @"Value Preview", isInstance ? [self previewWithTarget:object] : @"",
  243. @"Value Address", returnsObject ? [FLEXUtility addressOfObject:value] : @"",
  244. ]];
  245. }
  246. [items addObjectsFromArray:@[
  247. @"Size", @(self.size).stringValue,
  248. @"Offset", @(self.offset).stringValue,
  249. @"objc_ivar", [FLEXUtility pointerToString:self.objc_ivar],
  250. ]];
  251. return items;
  252. }
  253. - (NSString *)contextualSubtitleWithTarget:(id)object {
  254. if (!object_isClass(object) && self.typeEncoding.flex_typeIsObjectOrClass) {
  255. return [FLEXUtility addressOfObject:[self getValue:object]];
  256. }
  257. return nil;
  258. }
  259. #endif
  260. @end
  261. #pragma mark FLEXMethod
  262. @implementation FLEXMethodBase (UIKitHelpers)
  263. FLEXObjectExplorerDefaultsImpl
  264. - (BOOL)isEditable {
  265. return NO;
  266. }
  267. - (BOOL)isCallable {
  268. return NO;
  269. }
  270. - (id)currentValueWithTarget:(id)object {
  271. // Methods can't be "edited" and have no "value"
  272. return nil;
  273. }
  274. - (NSString *)previewWithTarget:(id)object {
  275. return [self.selectorString stringByAppendingFormat:@" — %@", self.typeEncoding];
  276. }
  277. - (UIViewController *)viewerWithTarget:(id)object {
  278. // We disallow calling of FLEXMethodBase methods
  279. @throw NSInternalInconsistencyException;
  280. return nil;
  281. }
  282. - (UIViewController *)editorWithTarget:(id)object {
  283. // Methods cannot be edited
  284. @throw NSInternalInconsistencyException;
  285. return nil;
  286. }
  287. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  288. // We shouldn't be using any FLEXMethodBase objects for this
  289. @throw NSInternalInconsistencyException;
  290. return UITableViewCellAccessoryNone;
  291. }
  292. - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
  293. #if FLEX_AT_LEAST_IOS13_SDK
  294. - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
  295. return nil;
  296. }
  297. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  298. return @[
  299. @"Selector", self.name ?: @"",
  300. @"Type Encoding", self.typeEncoding ?: @"",
  301. @"Declaration", self.description ?: @"",
  302. ];
  303. }
  304. - (NSString *)contextualSubtitleWithTarget:(id)object {
  305. return nil;
  306. }
  307. #endif
  308. @end
  309. @implementation FLEXMethod (UIKitHelpers)
  310. - (BOOL)isCallable {
  311. return self.signature != nil;
  312. }
  313. - (UIViewController *)viewerWithTarget:(id)object {
  314. object = self.isInstanceMethod ? object : (object_isClass(object) ? object : [object class]);
  315. return [FLEXMethodCallingViewController target:object method:self];
  316. }
  317. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  318. if (self.isInstanceMethod) {
  319. if (object_isClass(object)) {
  320. // Instance method from class, can't call
  321. return UITableViewCellAccessoryNone;
  322. } else {
  323. // Instance method from instance, can call
  324. return UITableViewCellAccessoryDisclosureIndicator;
  325. }
  326. } else {
  327. return UITableViewCellAccessoryDisclosureIndicator;
  328. }
  329. }
  330. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  331. return [[super copiableMetadataWithTarget:object] arrayByAddingObjectsFromArray:@[
  332. @"NSMethodSignature *", [FLEXUtility addressOfObject:self.signature],
  333. @"Signature String", self.signatureString ?: @"",
  334. @"Number of Arguments", @(self.numberOfArguments).stringValue,
  335. @"Return Type", @(self.returnType ?: ""),
  336. @"Return Size", @(self.returnSize).stringValue,
  337. @"objc_method", [FLEXUtility pointerToString:self.objc_method],
  338. ]];
  339. }
  340. @end
  341. #pragma mark FLEXProtocol
  342. @implementation FLEXProtocol (UIKitHelpers)
  343. FLEXObjectExplorerDefaultsImpl
  344. - (BOOL)isEditable {
  345. return NO;
  346. }
  347. - (BOOL)isCallable {
  348. return NO;
  349. }
  350. - (id)currentValueWithTarget:(id)object {
  351. return nil;
  352. }
  353. - (NSString *)previewWithTarget:(id)object {
  354. return nil;
  355. }
  356. - (UIViewController *)viewerWithTarget:(id)object {
  357. return [FLEXObjectExplorerFactory explorerViewControllerForObject:self];
  358. }
  359. - (UIViewController *)editorWithTarget:(id)object {
  360. return nil;
  361. }
  362. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  363. return UITableViewCellAccessoryDisclosureIndicator;
  364. }
  365. - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
  366. #if FLEX_AT_LEAST_IOS13_SDK
  367. - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
  368. return nil;
  369. }
  370. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  371. NSArray<NSString *> *conformanceNames = [self.protocols valueForKeyPath:@"name"];
  372. NSString *conformances = [conformanceNames componentsJoinedByString:@"\n"];
  373. return @[
  374. @"Name", self.name ?: @"",
  375. @"Conformances", conformances,
  376. ];
  377. }
  378. - (NSString *)contextualSubtitleWithTarget:(id)object {
  379. return nil;
  380. }
  381. #endif
  382. @end
  383. #pragma mark FLEXStaticMetadata
  384. @interface FLEXStaticMetadata () {
  385. @protected
  386. NSString *_name;
  387. }
  388. @property (nonatomic) FLEXTableViewCellReuseIdentifier reuse;
  389. @property (nonatomic) NSString *subtitle;
  390. @property (nonatomic) id metadata;
  391. @end
  392. @interface FLEXStaticMetadata_Class : FLEXStaticMetadata
  393. + (instancetype)withClass:(Class)cls;
  394. @end
  395. @implementation FLEXStaticMetadata
  396. @synthesize name = _name;
  397. @synthesize tag = _tag;
  398. FLEXObjectExplorerDefaultsImpl
  399. + (NSArray<FLEXStaticMetadata *> *)classHierarchy:(NSArray<Class> *)classes {
  400. return [classes flex_mapped:^id(Class cls, NSUInteger idx) {
  401. return [FLEXStaticMetadata_Class withClass:cls];
  402. }];
  403. }
  404. + (instancetype)style:(FLEXStaticMetadataRowStyle)style title:(NSString *)title string:(NSString *)string {
  405. return [[self alloc] initWithStyle:style title:title subtitle:string];
  406. }
  407. + (instancetype)style:(FLEXStaticMetadataRowStyle)style title:(NSString *)title number:(NSNumber *)number {
  408. return [[self alloc] initWithStyle:style title:title subtitle:number.stringValue];
  409. }
  410. - (id)initWithStyle:(FLEXStaticMetadataRowStyle)style title:(NSString *)title subtitle:(NSString *)subtitle {
  411. self = [super init];
  412. if (self) {
  413. if (style == FLEXStaticMetadataRowStyleKeyValue) {
  414. _reuse = kFLEXKeyValueCell;
  415. } else {
  416. _reuse = kFLEXMultilineDetailCell;
  417. }
  418. _name = title;
  419. _subtitle = subtitle;
  420. }
  421. return self;
  422. }
  423. - (NSString *)description {
  424. return self.name;
  425. }
  426. - (NSString *)reuseIdentifierWithTarget:(id)object {
  427. return self.reuse;
  428. }
  429. - (BOOL)isEditable {
  430. return NO;
  431. }
  432. - (BOOL)isCallable {
  433. return NO;
  434. }
  435. - (id)currentValueWithTarget:(id)object {
  436. return nil;
  437. }
  438. - (NSString *)previewWithTarget:(id)object {
  439. return self.subtitle;
  440. }
  441. - (UIViewController *)viewerWithTarget:(id)object {
  442. return nil;
  443. }
  444. - (UIViewController *)editorWithTarget:(id)object {
  445. return nil;
  446. }
  447. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  448. return UITableViewCellAccessoryNone;
  449. }
  450. #if FLEX_AT_LEAST_IOS13_SDK
  451. - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
  452. return nil;
  453. }
  454. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  455. return @[self.name, self.subtitle];
  456. }
  457. - (NSString *)contextualSubtitleWithTarget:(id)object {
  458. return nil;
  459. }
  460. #endif
  461. @end
  462. #pragma mark FLEXStaticMetadata_Class
  463. @implementation FLEXStaticMetadata_Class
  464. + (instancetype)withClass:(Class)cls {
  465. NSParameterAssert(cls);
  466. FLEXStaticMetadata_Class *metadata = [self new];
  467. metadata.metadata = cls;
  468. metadata->_name = NSStringFromClass(cls);
  469. metadata.reuse = kFLEXDefaultCell;
  470. return metadata;
  471. }
  472. - (id)initWithStyle:(FLEXStaticMetadataRowStyle)style title:(NSString *)title subtitle:(NSString *)subtitle {
  473. @throw NSInternalInconsistencyException;
  474. return nil;
  475. }
  476. - (UIViewController *)viewerWithTarget:(id)object {
  477. return [FLEXObjectExplorerFactory explorerViewControllerForObject:self.metadata];
  478. }
  479. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  480. return UITableViewCellAccessoryDisclosureIndicator;
  481. }
  482. - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
  483. return @[
  484. @"Class Name", self.name,
  485. @"Class", [FLEXUtility addressOfObject:self.metadata]
  486. ];
  487. }
  488. - (NSString *)contextualSubtitleWithTarget:(id)object {
  489. return [FLEXUtility addressOfObject:self.metadata];
  490. }
  491. @end