FLEXPropertyEditorViewController.m 2.1 KB

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