FLEXPropertyEditorViewController.m 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // FLEXPropertyEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/20/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXPropertyEditorViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXArgumentInputView.h"
  12. #import "FLEXArgumentInputViewFactory.h"
  13. #import "FLEXArgumentInputSwitchView.h"
  14. @interface FLEXPropertyEditorViewController () <FLEXArgumentInputViewDelegate>
  15. @property (nonatomic, assign) objc_property_t property;
  16. @end
  17. @implementation FLEXPropertyEditorViewController
  18. - (id)initWithTarget:(id)target property:(objc_property_t)property
  19. {
  20. self = [super initWithTarget:target];
  21. if (self) {
  22. self.property = property;
  23. self.title = @"Property";
  24. }
  25. return self;
  26. }
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility fullDescriptionForProperty:self.property];
  31. id currentValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  32. self.setterButton.enabled = [[self class] canEditProperty:self.property currentValue:currentValue];
  33. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:self.property] UTF8String];
  34. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:typeEncoding];
  35. inputView.backgroundColor = self.view.backgroundColor;
  36. inputView.inputValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  37. inputView.delegate = self;
  38. self.fieldEditorView.argumentInputViews = @[inputView];
  39. // Don't show a "set" button for switches - just call the setter immediately after the switch toggles.
  40. if ([inputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  41. self.navigationItem.rightBarButtonItem = nil;
  42. }
  43. }
  44. - (void)actionButtonPressed:(id)sender
  45. {
  46. [super actionButtonPressed:sender];
  47. id userInputObject = self.firstInputView.inputValue;
  48. NSArray *arguments = userInputObject ? @[userInputObject] : nil;
  49. SEL setterSelector = [FLEXRuntimeUtility setterSelectorForProperty:self.property];
  50. NSError *error = nil;
  51. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:&error];
  52. if (error) {
  53. NSString *title = @"Property Setter Failed";
  54. NSString *message = [error localizedDescription];
  55. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  56. [alert show];
  57. self.firstInputView.inputValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  58. } else {
  59. // If the setter was called without error, pop the view controller to indicate that and make the user's life easier.
  60. // Don't do this for simulated taps on the action button (i.e. from switch/BOOL editors). The experience is weird there.
  61. if (sender) {
  62. [self.navigationController popViewControllerAnimated:YES];
  63. }
  64. }
  65. }
  66. - (void)argumentInputViewValueDidChange:(FLEXArgumentInputView *)argumentInputView
  67. {
  68. if ([argumentInputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  69. [self actionButtonPressed:nil];
  70. }
  71. }
  72. + (BOOL)canEditProperty:(objc_property_t)property currentValue:(id)value
  73. {
  74. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:property] UTF8String];
  75. BOOL canEditType = [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:value];
  76. BOOL isReadonly = [FLEXRuntimeUtility isReadonlyProperty:property];
  77. return canEditType && !isReadonly;
  78. }
  79. @end