123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- //
- // FLEXRuntime+UIKitHelpers.m
- // FLEX
- //
- // Created by Tanner Bennett on 12/16/19.
- // Copyright © 2020 FLEX Team. All rights reserved.
- //
- #import "FLEXRuntime+UIKitHelpers.h"
- #import "FLEXRuntimeUtility.h"
- #import "FLEXPropertyAttributes.h"
- #import "FLEXArgumentInputViewFactory.h"
- #import "FLEXObjectExplorerFactory.h"
- #import "FLEXFieldEditorViewController.h"
- #import "FLEXMethodCallingViewController.h"
- #import "FLEXTableView.h"
- #import "FLEXUtility.h"
- #import "NSArray+FLEX.h"
- #import "NSString+FLEX.h"
- #if TARGET_OS_TV
- #import "fakes.h"
- #endif
- #define FLEXObjectExplorerDefaultsImpl \
- - (FLEXObjectExplorerDefaults *)defaults { \
- return self.tag; \
- } \
- \
- - (void)setDefaults:(FLEXObjectExplorerDefaults *)defaults { \
- self.tag = defaults; \
- }
- #pragma mark FLEXProperty
- @implementation FLEXProperty (UIKitHelpers)
- FLEXObjectExplorerDefaultsImpl
- /// Decide whether to use potentialTarget or [potentialTarget class] to get or set property
- - (id)appropriateTargetForPropertyType:(id)potentialTarget {
- if (!object_isClass(potentialTarget)) {
- if (self.isClassProperty) {
- return [potentialTarget class];
- } else {
- return potentialTarget;
- }
- } else {
- if (self.isClassProperty) {
- return potentialTarget;
- } else {
- // Instance property with a class object
- return nil;
- }
- }
- }
- - (BOOL)isEditable {
- if (self.attributes.isReadOnly) {
- return self.likelySetterExists;
- }
-
- const FLEXTypeEncoding *typeEncoding = self.attributes.typeEncoding.UTF8String;
- return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
- }
- - (BOOL)isCallable {
- return YES;
- }
- - (id)currentValueWithTarget:(id)object {
- return [self getPotentiallyUnboxedValue:
- [self appropriateTargetForPropertyType:object]
- ];
- }
- - (id)currentValueBeforeUnboxingWithTarget:(id)object {
- return [self getValue:
- [self appropriateTargetForPropertyType:object]
- ];
- }
- - (NSString *)previewWithTarget:(id)object {
- if (object_isClass(object) && !self.isClassProperty) {
- return self.attributes.fullDeclaration;
- } else if (self.defaults.wantsDynamicPreviews) {
- return [FLEXRuntimeUtility
- summaryForObject:[self currentValueWithTarget:object]
- ];
- }
-
- return nil;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- id value = [self currentValueWithTarget:object];
- return [FLEXObjectExplorerFactory explorerViewControllerForObject:value];
- }
- - (UIViewController *)editorWithTarget:(id)object section:(FLEXTableViewSection *)section {
- id target = [self appropriateTargetForPropertyType:object];
- return [FLEXFieldEditorViewController target:target property:self commitHandler:^{
- [section reloadData:YES];
- }];
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- id targetForValueCheck = [self appropriateTargetForPropertyType:object];
- if (!targetForValueCheck) {
- // Instance property with a class object
- return UITableViewCellAccessoryNone;
- }
- // We use .tag to store the cached value of .isEditable that is
- // initialized by FLEXObjectExplorer in -reloadMetada
- if ([self getPotentiallyUnboxedValue:targetForValueCheck]) {
- if (self.defaults.isEditable) {
- // Editable non-nil value, both
- #if !TARGET_OS_TV
- return UITableViewCellAccessoryDetailDisclosureButton;
- #else
- return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
- #endif
- } else {
- // Uneditable non-nil value, chevron only
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- } else {
- if (self.defaults.isEditable) {
- // Editable nil value, just (i)
- #if !TARGET_OS_TV
- return UITableViewCellAccessoryDetailButton;
- #else
- return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailButton;
- #endif
- } else {
- // Non-editable nil value, neither
- return UITableViewCellAccessoryNone;
- }
- }
- }
- - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
- #if FLEX_AT_LEAST_IOS13_SDK
- - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
- Class propertyClass = self.attributes.typeEncoding.flex_typeClass;
-
- // "Explore PropertyClass" for properties with a concrete class name
- if (propertyClass) {
- NSString *title = [NSString stringWithFormat:@"Explore %@", NSStringFromClass(propertyClass)];
- return @[[UIAction actionWithTitle:title image:nil identifier:nil handler:^(UIAction *action) {
- UIViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:propertyClass];
- [sender.navigationController pushViewController:explorer animated:YES];
- }]];
- }
-
- return nil;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- BOOL returnsObject = self.attributes.typeEncoding.flex_typeIsObjectOrClass;
- BOOL targetNotNil = [self appropriateTargetForPropertyType:object] != nil;
-
- NSMutableArray *items = [NSMutableArray arrayWithArray:@[
- @"Name", self.name ?: @"",
- @"Type", self.attributes.typeEncoding ?: @"",
- @"Declaration", self.fullDescription ?: @"",
- ]];
-
- if (targetNotNil) {
- id value = [self currentValueBeforeUnboxingWithTarget:object];
- [items addObjectsFromArray:@[
- @"Value Preview", [self previewWithTarget:object] ?: @"",
- @"Value Address", returnsObject ? [FLEXUtility addressOfObject:value] : @"",
- ]];
- }
-
- [items addObjectsFromArray:@[
- @"Getter", NSStringFromSelector(self.likelyGetter) ?: @"",
- @"Setter", self.likelySetterExists ? NSStringFromSelector(self.likelySetter) : @"",
- @"Image Name", self.imageName ?: @"",
- @"Attributes", self.attributes.string ?: @"",
- @"objc_property", [FLEXUtility pointerToString:self.objc_property],
- @"objc_property_attribute_t", [FLEXUtility pointerToString:self.attributes.list],
- ]];
-
- return items;
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- id target = [self appropriateTargetForPropertyType:object];
- if (target && self.attributes.typeEncoding.flex_typeIsObjectOrClass) {
- return [FLEXUtility addressOfObject:[self currentValueBeforeUnboxingWithTarget:target]];
- }
-
- return nil;
- }
- #endif
- @end
- #pragma mark FLEXIvar
- @implementation FLEXIvar (UIKitHelpers)
- FLEXObjectExplorerDefaultsImpl
- - (BOOL)isEditable {
- const FLEXTypeEncoding *typeEncoding = self.typeEncoding.UTF8String;
- return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
- }
- - (BOOL)isCallable {
- return NO;
- }
- - (id)currentValueWithTarget:(id)object {
- if (!object_isClass(object)) {
- return [self getPotentiallyUnboxedValue:object];
- }
- return nil;
- }
- - (NSString *)previewWithTarget:(id)object {
- if (object_isClass(object)) {
- return self.details;
- } else if (self.defaults.wantsDynamicPreviews) {
- return [FLEXRuntimeUtility
- summaryForObject:[self currentValueWithTarget:object]
- ];
- }
-
- return nil;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- NSAssert(!object_isClass(object), @"Unreachable state: viewing ivar on class object");
- id value = [self currentValueWithTarget:object];
- return [FLEXObjectExplorerFactory explorerViewControllerForObject:value];
- }
- - (UIViewController *)editorWithTarget:(id)object section:(FLEXTableViewSection *)section {
- NSAssert(!object_isClass(object), @"Unreachable state: editing ivar on class object");
- return [FLEXFieldEditorViewController target:object ivar:self commitHandler:^{
- [section reloadData:YES];
- }];
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- if (object_isClass(object)) {
- return UITableViewCellAccessoryNone;
- }
- // Could use .isEditable here, but we use .tag for speed since it is cached
- if ([self getPotentiallyUnboxedValue:object]) {
- if (self.defaults.isEditable) {
- // Editable non-nil value, both
- #if !TARGET_OS_TV
- return UITableViewCellAccessoryDetailDisclosureButton;
- #else
- return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
- #endif
- } else {
- // Uneditable non-nil value, chevron only
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- } else {
- if (self.defaults.isEditable) {
- // Editable nil value, just (i)
- #if !TARGET_OS_TV
- return UITableViewCellAccessoryDetailButton;
- #else
- return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailButton;
- #endif
- } else {
- // Non-editable nil value, neither
- return UITableViewCellAccessoryNone;
- }
- }
- }
- - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
- #if FLEX_AT_LEAST_IOS13_SDK
- - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
- Class ivarClass = self.typeEncoding.flex_typeClass;
-
- // "Explore PropertyClass" for properties with a concrete class name
- if (ivarClass) {
- NSString *title = [NSString stringWithFormat:@"Explore %@", NSStringFromClass(ivarClass)];
- return @[[UIAction actionWithTitle:title image:nil identifier:nil handler:^(UIAction *action) {
- UIViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:ivarClass];
- [sender.navigationController pushViewController:explorer animated:YES];
- }]];
- }
-
- return nil;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- BOOL isInstance = !object_isClass(object);
- BOOL returnsObject = self.typeEncoding.flex_typeIsObjectOrClass;
- id value = isInstance ? [self getValue:object] : nil;
-
- NSMutableArray *items = [NSMutableArray arrayWithArray:@[
- @"Name", self.name ?: @"",
- @"Type", self.typeEncoding ?: @"",
- @"Declaration", self.description ?: @"",
- ]];
-
- if (isInstance) {
- [items addObjectsFromArray:@[
- @"Value Preview", isInstance ? [self previewWithTarget:object] : @"",
- @"Value Address", returnsObject ? [FLEXUtility addressOfObject:value] : @"",
- ]];
- }
-
- [items addObjectsFromArray:@[
- @"Size", @(self.size).stringValue,
- @"Offset", @(self.offset).stringValue,
- @"objc_ivar", [FLEXUtility pointerToString:self.objc_ivar],
- ]];
-
- return items;
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- if (!object_isClass(object) && self.typeEncoding.flex_typeIsObjectOrClass) {
- return [FLEXUtility addressOfObject:[self getValue:object]];
- }
-
- return nil;
- }
- #endif
- @end
- #pragma mark FLEXMethod
- @implementation FLEXMethodBase (UIKitHelpers)
- FLEXObjectExplorerDefaultsImpl
- - (BOOL)isEditable {
- return NO;
- }
- - (BOOL)isCallable {
- return NO;
- }
- - (id)currentValueWithTarget:(id)object {
- // Methods can't be "edited" and have no "value"
- return nil;
- }
- - (NSString *)previewWithTarget:(id)object {
- return [self.selectorString stringByAppendingFormat:@" — %@", self.typeEncoding];
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- // We disallow calling of FLEXMethodBase methods
- @throw NSInternalInconsistencyException;
- return nil;
- }
- - (UIViewController *)editorWithTarget:(id)object section:(FLEXTableViewSection *)section {
- // Methods cannot be edited
- @throw NSInternalInconsistencyException;
- return nil;
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- // We shouldn't be using any FLEXMethodBase objects for this
- @throw NSInternalInconsistencyException;
- return UITableViewCellAccessoryNone;
- }
- - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
- #if FLEX_AT_LEAST_IOS13_SDK
- - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
- return nil;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- return @[
- @"Selector", self.name ?: @"",
- @"Type Encoding", self.typeEncoding ?: @"",
- @"Declaration", self.description ?: @"",
- ];
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- return nil;
- }
- #endif
- @end
- @implementation FLEXMethod (UIKitHelpers)
- - (BOOL)isCallable {
- return self.signature != nil;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- object = self.isInstanceMethod ? object : (object_isClass(object) ? object : [object class]);
- return [FLEXMethodCallingViewController target:object method:self];
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- if (self.isInstanceMethod) {
- if (object_isClass(object)) {
- // Instance method from class, can't call
- return UITableViewCellAccessoryNone;
- } else {
- // Instance method from instance, can call
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- } else {
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- return [[super copiableMetadataWithTarget:object] arrayByAddingObjectsFromArray:@[
- @"NSMethodSignature *", [FLEXUtility addressOfObject:self.signature],
- @"Signature String", self.signatureString ?: @"",
- @"Number of Arguments", @(self.numberOfArguments).stringValue,
- @"Return Type", @(self.returnType ?: ""),
- @"Return Size", @(self.returnSize).stringValue,
- @"objc_method", [FLEXUtility pointerToString:self.objc_method],
- ]];
- }
- @end
- #pragma mark FLEXProtocol
- @implementation FLEXProtocol (UIKitHelpers)
- FLEXObjectExplorerDefaultsImpl
- - (BOOL)isEditable {
- return NO;
- }
- - (BOOL)isCallable {
- return NO;
- }
- - (id)currentValueWithTarget:(id)object {
- return nil;
- }
- - (NSString *)previewWithTarget:(id)object {
- return nil;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- return [FLEXObjectExplorerFactory explorerViewControllerForObject:self];
- }
- - (UIViewController *)editorWithTarget:(id)object section:(FLEXTableViewSection *)section {
- // Protocols cannot be edited
- @throw NSInternalInconsistencyException;
- return nil;
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- - (NSString *)reuseIdentifierWithTarget:(id)object { return nil; }
- #if FLEX_AT_LEAST_IOS13_SDK
- - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
- return nil;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- NSArray<NSString *> *conformanceNames = [self.protocols valueForKeyPath:@"name"];
- NSString *conformances = [conformanceNames componentsJoinedByString:@"\n"];
- return @[
- @"Name", self.name ?: @"",
- @"Conformances", conformances ?: @"",
- ];
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- return nil;
- }
- #endif
- @end
- #pragma mark FLEXStaticMetadata
- @interface FLEXStaticMetadata () {
- @protected
- NSString *_name;
- }
- @property (nonatomic) FLEXTableViewCellReuseIdentifier reuse;
- @property (nonatomic) NSString *subtitle;
- @property (nonatomic) id metadata;
- @end
- @interface FLEXStaticMetadata_Class : FLEXStaticMetadata
- + (instancetype)withClass:(Class)cls;
- @end
- @implementation FLEXStaticMetadata
- @synthesize name = _name;
- @synthesize tag = _tag;
- FLEXObjectExplorerDefaultsImpl
- + (NSArray<FLEXStaticMetadata *> *)classHierarchy:(NSArray<Class> *)classes {
- return [classes flex_mapped:^id(Class cls, NSUInteger idx) {
- return [FLEXStaticMetadata_Class withClass:cls];
- }];
- }
- + (instancetype)style:(FLEXStaticMetadataRowStyle)style title:(NSString *)title string:(NSString *)string {
- return [[self alloc] initWithStyle:style title:title subtitle:string];
- }
- + (instancetype)style:(FLEXStaticMetadataRowStyle)style title:(NSString *)title number:(NSNumber *)number {
- return [[self alloc] initWithStyle:style title:title subtitle:number.stringValue];
- }
- - (id)initWithStyle:(FLEXStaticMetadataRowStyle)style title:(NSString *)title subtitle:(NSString *)subtitle {
- self = [super init];
- if (self) {
- if (style == FLEXStaticMetadataRowStyleKeyValue) {
- _reuse = kFLEXKeyValueCell;
- } else {
- _reuse = kFLEXMultilineDetailCell;
- }
- _name = title;
- _subtitle = subtitle;
- }
- return self;
- }
- - (NSString *)description {
- return self.name;
- }
- - (NSString *)reuseIdentifierWithTarget:(id)object {
- return self.reuse;
- }
- - (BOOL)isEditable {
- return NO;
- }
- - (BOOL)isCallable {
- return NO;
- }
- - (id)currentValueWithTarget:(id)object {
- return nil;
- }
- - (NSString *)previewWithTarget:(id)object {
- return self.subtitle;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- return nil;
- }
- - (UIViewController *)editorWithTarget:(id)object section:(FLEXTableViewSection *)section {
- // Static metadata cannot be edited
- @throw NSInternalInconsistencyException;
- return nil;
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- return UITableViewCellAccessoryNone;
- }
- #if FLEX_AT_LEAST_IOS13_SDK
- - (NSArray<UIAction *> *)additionalActionsWithTarget:(id)object sender:(UIViewController *)sender __IOS_AVAILABLE(13.0) {
- return nil;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- return @[self.name, self.subtitle];
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- return nil;
- }
- #endif
- @end
- #pragma mark FLEXStaticMetadata_Class
- @implementation FLEXStaticMetadata_Class
- + (instancetype)withClass:(Class)cls {
- NSParameterAssert(cls);
-
- FLEXStaticMetadata_Class *metadata = [self new];
- metadata.metadata = cls;
- metadata->_name = NSStringFromClass(cls);
- metadata.reuse = kFLEXDefaultCell;
- return metadata;
- }
- - (id)initWithStyle:(FLEXStaticMetadataRowStyle)style title:(NSString *)title subtitle:(NSString *)subtitle {
- @throw NSInternalInconsistencyException;
- return nil;
- }
- - (UIViewController *)viewerWithTarget:(id)object {
- return [FLEXObjectExplorerFactory explorerViewControllerForObject:self.metadata];
- }
- - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
- return UITableViewCellAccessoryDisclosureIndicator;
- }
- - (NSArray<NSString *> *)copiableMetadataWithTarget:(id)object {
- return @[
- @"Class Name", self.name,
- @"Class", [FLEXUtility addressOfObject:self.metadata]
- ];
- }
- - (NSString *)contextualSubtitleWithTarget:(id)object {
- return [FLEXUtility addressOfObject:self.metadata];
- }
- @end
|