FLEXFieldEditorViewController.m 5.2 KB

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