FLEXPropertyEditorViewController.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. @interface FLEXPropertyEditorViewController ()
  14. @property (nonatomic, assign) objc_property_t property;
  15. @end
  16. @implementation FLEXPropertyEditorViewController
  17. - (id)initWithTarget:(id)target property:(objc_property_t)property
  18. {
  19. self = [super initWithTarget:target];
  20. if (self) {
  21. self.property = property;
  22. self.title = @"Property";
  23. }
  24. return self;
  25. }
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility fullDescriptionForProperty:self.property];
  30. id currentValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  31. self.setterButton.enabled = [[self class] canEditProperty:self.property currentValue:currentValue];
  32. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:self.property] UTF8String];
  33. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:typeEncoding];
  34. inputView.backgroundColor = self.view.backgroundColor;
  35. inputView.targetSize = FLEXArgumentInputViewSizeLarge;
  36. inputView.inputOutput = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  37. self.fieldEditorView.argumentInputViews = @[inputView];
  38. }
  39. - (void)actionButtonPressed:(id)sender
  40. {
  41. [super actionButtonPressed:sender];
  42. id userInputObject = self.firstInputView.inputOutput;
  43. NSArray *arguments = userInputObject ? @[userInputObject] : nil;
  44. SEL setterSelector = [FLEXRuntimeUtility setterSelectorForProperty:self.property];
  45. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:NULL];
  46. self.firstInputView.inputOutput = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  47. }
  48. + (BOOL)canEditProperty:(objc_property_t)property currentValue:(id)value
  49. {
  50. const char *typeEncoding = [[FLEXRuntimeUtility typeEncodingForProperty:property] UTF8String];
  51. BOOL canEditType = [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:value];
  52. BOOL isReadonly = [FLEXRuntimeUtility isReadonlyProperty:property];
  53. return canEditType && !isReadonly;
  54. }
  55. @end