FLEXFieldEditorViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // FLEXFieldEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/16/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXFieldEditorViewController.h"
  9. #import "FLEXFieldEditorView.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXUtility.h"
  12. #import "FLEXArgumentInputView.h"
  13. #import "FLEXArgumentInputViewFactory.h"
  14. @interface FLEXFieldEditorViewController () <UIScrollViewDelegate>
  15. @property (nonatomic, strong) UIScrollView *scrollView;
  16. @property (nonatomic, strong, readwrite) id target;
  17. @property (nonatomic, strong, readwrite) FLEXFieldEditorView *fieldEditorView;
  18. @property (nonatomic, strong, readwrite) UIBarButtonItem *setterButton;
  19. @end
  20. @implementation FLEXFieldEditorViewController
  21. - (id)initWithTarget:(id)target
  22. {
  23. self = [super initWithNibName:nil bundle:nil];
  24. if (self) {
  25. self.target = target;
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  28. }
  29. return self;
  30. }
  31. - (void)dealloc
  32. {
  33. [[NSNotificationCenter defaultCenter] removeObserver:self];
  34. }
  35. - (void)keyboardDidShow:(NSNotification *)notification
  36. {
  37. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  38. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  39. scrollInsets.bottom = keyboardSize.height;
  40. self.scrollView.contentInset = scrollInsets;
  41. self.scrollView.scrollIndicatorInsets = scrollInsets;
  42. // Find the active input view and scroll to make sure it's visible.
  43. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  44. if (argumentInputView.inputViewIsFirstResponder) {
  45. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  46. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  47. break;
  48. }
  49. }
  50. }
  51. - (void)keyboardWillHide:(NSNotification *)notification
  52. {
  53. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  54. scrollInsets.bottom = 0.0;
  55. self.scrollView.contentInset = scrollInsets;
  56. self.scrollView.scrollIndicatorInsets = scrollInsets;
  57. }
  58. - (BOOL)prefersStatusBarHidden
  59. {
  60. return YES;
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. self.view.backgroundColor = [FLEXUtility scrollViewGrayColor];
  66. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  67. self.scrollView.backgroundColor = self.view.backgroundColor;
  68. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  69. self.scrollView.delegate = self;
  70. [self.view addSubview:self.scrollView];
  71. self.fieldEditorView = [[FLEXFieldEditorView alloc] init];
  72. self.fieldEditorView.backgroundColor = self.view.backgroundColor;
  73. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  74. [self.scrollView addSubview:self.fieldEditorView];
  75. // One argument input view by default. Subclasses can configure the field editor view with more/different argument input views if needed.
  76. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:NULL];
  77. inputView.backgroundColor = self.view.backgroundColor;
  78. inputView.targetSize = FLEXArgumentInputViewSizeLarge;
  79. self.fieldEditorView.argumentInputViews = @[inputView];
  80. self.setterButton = [[UIBarButtonItem alloc] initWithTitle:[self titleForActionButton] style:UIBarButtonItemStyleDone target:self action:@selector(actionButtonPressed:)];
  81. self.navigationItem.rightBarButtonItem = self.setterButton;
  82. }
  83. - (void)viewWillLayoutSubviews
  84. {
  85. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  86. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  87. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  88. self.scrollView.contentSize = fieldEditorSize;
  89. }
  90. - (FLEXArgumentInputView *)firstInputView
  91. {
  92. return [[self.fieldEditorView argumentInputViews] firstObject];
  93. }
  94. - (void)actionButtonPressed:(id)sender
  95. {
  96. // Subclasses can override
  97. [self.fieldEditorView endEditing:YES];
  98. }
  99. - (NSString *)titleForActionButton
  100. {
  101. // Subclasses can override.
  102. return @"Set";
  103. }
  104. + (BOOL)canEditType:(NSString *)typeEncoding currentObjectValue:(id)value
  105. {
  106. // Many primitive types can always be edited (numbers and supported structs).
  107. static NSArray *primitiveTypes = nil;
  108. static dispatch_once_t onceToken;
  109. dispatch_once(&onceToken, ^{
  110. primitiveTypes = @[@(@encode(char)),
  111. @(@encode(int)),
  112. @(@encode(short)),
  113. @(@encode(long)),
  114. @(@encode(long long)),
  115. @(@encode(unsigned char)),
  116. @(@encode(unsigned int)),
  117. @(@encode(unsigned short)),
  118. @(@encode(unsigned long)),
  119. @(@encode(unsigned long long)),
  120. @(@encode(float)),
  121. @(@encode(double)),
  122. @(@encode(CGRect)),
  123. @(@encode(CGSize)),
  124. @(@encode(CGPoint)),
  125. @(@encode(CGAffineTransform)),
  126. @(@encode(NSRange)),
  127. @(@encode(UIEdgeInsets)),
  128. @(@encode(UIOffset))];
  129. });
  130. BOOL canEdit = [primitiveTypes containsObject:typeEncoding];
  131. // Object types may be
  132. if (!canEdit) {
  133. if (value) {
  134. // If the current value is non-nil and we can represent it with an editable string, then we can suppor editing.
  135. canEdit = canEdit || [FLEXRuntimeUtility editiableDescriptionForObject:value] != nil;
  136. } else {
  137. // Also always edit types that are explicitly typed NSString, NSNumber, NSArray, or NSDictionary and are nil.
  138. // This kind of type encoding is only kept by ivars and properties. Method agruments and return types drop the class from the type encoding.
  139. // The editor supports populating these types through NSJSONSerilization parsing of the input string.
  140. canEdit = canEdit || [typeEncoding isEqual:[self stringTypeEncoding]];
  141. canEdit = canEdit || [typeEncoding isEqual:[self numberTypeEncoding]];
  142. canEdit = canEdit || [typeEncoding isEqual:[self arrayTypeEncoding]];
  143. canEdit = canEdit || [typeEncoding isEqual:[self dictionaryTypeEncoding]];
  144. }
  145. }
  146. return canEdit;
  147. }
  148. + (NSString *)stringTypeEncoding
  149. {
  150. return @"@\"NSString\"";
  151. }
  152. + (NSString *)numberTypeEncoding
  153. {
  154. return @"@\"NSNumber\"";
  155. }
  156. + (NSString *)arrayTypeEncoding
  157. {
  158. return @"@\"NSArray\"";
  159. }
  160. + (NSString *)dictionaryTypeEncoding
  161. {
  162. return @"@\"NSDictionary\"";
  163. }
  164. @end