FLEXFieldEditorViewController.m 6.7 KB

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