FLEXFieldEditorViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // FLEXFieldEditorViewController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 11/22/18.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXFieldEditorViewController.h"
  9. #import "FLEXFieldEditorView.h"
  10. #import "FLEXArgumentInputViewFactory.h"
  11. #import "FLEXPropertyAttributes.h"
  12. #import "FLEXRuntimeUtility.h"
  13. #import "FLEXUtility.h"
  14. #import "FLEXColor.h"
  15. #import "UIBarButtonItem+FLEX.h"
  16. #import <TargetConditionals.h>
  17. @interface FLEXFieldEditorViewController () <FLEXArgumentInputViewDelegate>
  18. @property (nonatomic) FLEXProperty *property;
  19. @property (nonatomic) FLEXIvar *ivar;
  20. @property (nonatomic, readonly) id currentValue;
  21. @property (nonatomic, readonly) const FLEXTypeEncoding *typeEncoding;
  22. @property (nonatomic, readonly) NSString *fieldDescription;
  23. @end
  24. @implementation FLEXFieldEditorViewController
  25. #pragma mark - Initialization
  26. + (instancetype)target:(id)target property:(nonnull FLEXProperty *)property commitHandler:(void(^_Nullable)())onCommit {
  27. id value = [property getValue:target];
  28. if (![self canEditProperty:property onObject:target currentValue:value]) {
  29. return nil;
  30. }
  31. FLEXFieldEditorViewController *editor = [self target:target data:property commitHandler:onCommit];
  32. editor.title = [@"Property: " stringByAppendingString:property.name];
  33. editor.property = property;
  34. return editor;
  35. }
  36. + (instancetype)target:(id)target ivar:(nonnull FLEXIvar *)ivar commitHandler:(void(^_Nullable)())onCommit {
  37. FLEXFieldEditorViewController *editor = [self target:target data:ivar commitHandler:onCommit];
  38. editor.title = [@"Ivar: " stringByAppendingString:ivar.name];
  39. editor.ivar = ivar;
  40. return editor;
  41. }
  42. #pragma mark - Overrides
  43. - (void)viewDidLoad {
  44. [super viewDidLoad];
  45. self.view.backgroundColor = FLEXColor.groupedBackgroundColor;
  46. #if !TARGET_OS_TV
  47. // Create getter button
  48. _getterButton = [[UIBarButtonItem alloc]
  49. initWithTitle:@"Get"
  50. style:UIBarButtonItemStyleDone
  51. target:self
  52. action:@selector(getterButtonPressed:)
  53. ];
  54. self.toolbarItems = @[
  55. UIBarButtonItem.flex_flexibleSpace, self.getterButton, self.actionButton
  56. ];
  57. #else
  58. _getterButton = [UIButton buttonWithType:UIButtonTypeSystem];
  59. [_getterButton setTitle:@"Get" forState:UIControlStateNormal];
  60. [_getterButton addTarget:self action:@selector(getterButtonPressed:) forControlEvents:UIControlEventPrimaryActionTriggered];
  61. _getterButton.frame = CGRectMake(100, 600, 200, 60);
  62. [self.view addSubview:_getterButton];
  63. #endif
  64. // Configure input view
  65. self.fieldEditorView.fieldDescription = self.fieldDescription;
  66. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:self.typeEncoding];
  67. inputView.inputValue = self.currentValue;
  68. inputView.delegate = self;
  69. self.fieldEditorView.argumentInputViews = @[inputView];
  70. // Don't show a "set" button for switches; we mutate when the switch is flipped
  71. if ([inputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  72. self.actionButton.enabled = NO;
  73. #if !TARGET_OS_TV
  74. self.actionButton.title = @"Flip the switch to call the setter";
  75. // Put getter button before setter button
  76. self.toolbarItems = @[
  77. UIBarButtonItem.flex_flexibleSpace, self.actionButton, self.getterButton
  78. ];
  79. #endif
  80. }
  81. }
  82. - (void)actionButtonPressed:(id)sender {
  83. if (self.property) {
  84. id userInputObject = self.firstInputView.inputValue;
  85. NSArray *arguments = userInputObject ? @[userInputObject] : nil;
  86. SEL setterSelector = self.property.likelySetter;
  87. NSError *error = nil;
  88. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:&error];
  89. if (error) {
  90. [FLEXAlert showAlert:@"Property Setter Failed" message:error.localizedDescription from:self];
  91. sender = nil; // Don't pop back
  92. }
  93. } else {
  94. // TODO: check mutability and use mutableCopy if necessary;
  95. // this currently could and would assign NSArray to NSMutableArray
  96. [self.ivar setValue:self.firstInputView.inputValue onObject:self.target];
  97. }
  98. // Dismiss keyboard and handle committed changes
  99. [super actionButtonPressed:sender];
  100. // Go back after setting, but not for switches.
  101. if (sender) {
  102. [self.navigationController popViewControllerAnimated:YES];
  103. } else {
  104. self.firstInputView.inputValue = self.currentValue;
  105. }
  106. }
  107. - (void)getterButtonPressed:(id)sender {
  108. [self.fieldEditorView endEditing:YES];
  109. [self exploreObjectOrPopViewController:self.currentValue];
  110. }
  111. - (void)argumentInputViewValueDidChange:(FLEXArgumentInputView *)argumentInputView {
  112. if ([argumentInputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  113. [self actionButtonPressed:nil];
  114. }
  115. }
  116. - (void)viewWillLayoutSubviews {
  117. [super viewWillLayoutSubviews];
  118. #if TARGET_OS_TV
  119. CGRect getterFrame = _getterButton.frame;
  120. CGFloat actionOffset = [[self actionButton] frame].origin.y;
  121. //CGRect fieldEditorFrame = self.fieldEditorView.frame;
  122. //CGFloat buttonOffset = (fieldEditorFrame.origin.y + fieldEditorFrame.size.height) + (130 + 67);
  123. getterFrame.origin.y = actionOffset;
  124. _getterButton.frame = getterFrame;
  125. #endif
  126. }
  127. #pragma mark - Private
  128. - (id)currentValue {
  129. if (self.property) {
  130. return [self.property getValue:self.target];
  131. } else {
  132. return [self.ivar getValue:self.target];
  133. }
  134. }
  135. - (const FLEXTypeEncoding *)typeEncoding {
  136. if (self.property) {
  137. return self.property.attributes.typeEncoding.UTF8String;
  138. } else {
  139. return self.ivar.typeEncoding.UTF8String;
  140. }
  141. }
  142. - (NSString *)fieldDescription {
  143. if (self.property) {
  144. return self.property.fullDescription;
  145. } else {
  146. return self.ivar.description;
  147. }
  148. }
  149. + (BOOL)canEditProperty:(FLEXProperty *)property onObject:(id)object currentValue:(id)value {
  150. const FLEXTypeEncoding *typeEncoding = property.attributes.typeEncoding.UTF8String;
  151. BOOL canEditType = [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:value];
  152. return canEditType && [object respondsToSelector:property.likelySetter];
  153. }
  154. + (BOOL)canEditIvar:(Ivar)ivar currentValue:(id)value {
  155. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:ivar_getTypeEncoding(ivar) currentValue:value];
  156. }
  157. @end