FLEXPropertyEditorViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.targetSize = FLEXArgumentInputViewSizeLarge;
  37. inputView.inputValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  38. inputView.delegate = self;
  39. self.fieldEditorView.argumentInputViews = @[inputView];
  40. // Don't show a "set" button for switches - just call the setter immediately after the switch toggles.
  41. if ([inputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  42. self.navigationItem.rightBarButtonItem = nil;
  43. }
  44. }
  45. - (void)actionButtonPressed:(id)sender
  46. {
  47. [super actionButtonPressed:sender];
  48. id userInputObject = self.firstInputView.inputValue;
  49. NSArray *arguments = userInputObject ? @[userInputObject] : nil;
  50. SEL setterSelector = [FLEXRuntimeUtility setterSelectorForProperty:self.property];
  51. NSError *error = nil;
  52. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:&error];
  53. if (error) {
  54. NSString *title = @"Property Setter Failed";
  55. NSString *message = [error localizedDescription];
  56. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  57. [alert show];
  58. self.firstInputView.inputValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  59. } else {
  60. // If the setter was called without error, pop the view controller to indicate that and make the user's life easier.
  61. // Don't do this for simulated taps on the action button (i.e. from switch/BOOL editors). The experience is weird there.
  62. if (sender) {
  63. [self.navigationController popViewControllerAnimated:YES];
  64. }
  65. }
  66. }
  67. - (void)argumentInputViewValueDidChange:(FLEXArgumentInputView *)argumentInputView
  68. {
  69. if ([argumentInputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  70. [self actionButtonPressed:nil];
  71. }
  72. }
  73. + (BOOL)canEditProperty:(objc_property_t)property currentValue:(id)value
  74. {
  75. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:property] UTF8String];
  76. BOOL canEditType = [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:value];
  77. BOOL isReadonly = [FLEXRuntimeUtility isReadonlyProperty:property];
  78. return canEditType && !isReadonly;
  79. }
  80. @end