FLEXPropertyEditorViewController.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:NULL];
  52. self.firstInputView.inputValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  53. }
  54. - (void)argumentInputViewValueDidChange:(FLEXArgumentInputView *)argumentInputView
  55. {
  56. if ([argumentInputView isKindOfClass:[FLEXArgumentInputSwitchView class]]) {
  57. [self actionButtonPressed:nil];
  58. }
  59. }
  60. + (BOOL)canEditProperty:(objc_property_t)property currentValue:(id)value
  61. {
  62. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:property] UTF8String];
  63. BOOL canEditType = [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:value];
  64. BOOL isReadonly = [FLEXRuntimeUtility isReadonlyProperty:property];
  65. return canEditType && !isReadonly;
  66. }
  67. @end