FLEXFieldEditorViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "FLEXColor.h"
  9. #import "FLEXFieldEditorViewController.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXRuntimeUtility.h"
  12. #import "FLEXUtility.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXArgumentInputView.h"
  15. #import "FLEXArgumentInputViewFactory.h"
  16. #import "FLEXObjectExplorerViewController.h"
  17. @interface FLEXFieldEditorViewController () <UIScrollViewDelegate>
  18. @property (nonatomic) UIScrollView *scrollView;
  19. @property (nonatomic, readwrite) id target;
  20. @property (nonatomic, readwrite) FLEXFieldEditorView *fieldEditorView;
  21. @property (nonatomic, readwrite) UIBarButtonItem *setterButton;
  22. @end
  23. @implementation FLEXFieldEditorViewController
  24. - (id)initWithTarget:(id)target
  25. {
  26. self = [super initWithNibName:nil bundle:nil];
  27. if (self) {
  28. self.target = target;
  29. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  30. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  31. }
  32. return self;
  33. }
  34. - (void)dealloc
  35. {
  36. [NSNotificationCenter.defaultCenter removeObserver:self];
  37. }
  38. - (void)keyboardDidShow:(NSNotification *)notification
  39. {
  40. CGRect keyboardRectInWindow = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  41. CGSize keyboardSize = [self.view convertRect:keyboardRectInWindow fromView:nil].size;
  42. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  43. scrollInsets.bottom = keyboardSize.height;
  44. self.scrollView.contentInset = scrollInsets;
  45. self.scrollView.scrollIndicatorInsets = scrollInsets;
  46. // Find the active input view and scroll to make sure it's visible.
  47. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  48. if (argumentInputView.inputViewIsFirstResponder) {
  49. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  50. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  51. break;
  52. }
  53. }
  54. }
  55. - (void)keyboardWillHide:(NSNotification *)notification
  56. {
  57. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  58. scrollInsets.bottom = 0.0;
  59. self.scrollView.contentInset = scrollInsets;
  60. self.scrollView.scrollIndicatorInsets = scrollInsets;
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. self.view.backgroundColor = [FLEXColor scrollViewBackgroundColor];
  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 new];
  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. self.setterButton = [[UIBarButtonItem alloc] initWithTitle:[self titleForActionButton] style:UIBarButtonItemStyleDone target:self action:@selector(actionButtonPressed:)];
  76. self.navigationItem.rightBarButtonItem = self.setterButton;
  77. }
  78. - (void)viewWillLayoutSubviews
  79. {
  80. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  81. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  82. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  83. self.scrollView.contentSize = fieldEditorSize;
  84. }
  85. - (FLEXArgumentInputView *)firstInputView
  86. {
  87. return [self.fieldEditorView argumentInputViews].firstObject;
  88. }
  89. - (void)actionButtonPressed:(id)sender
  90. {
  91. // Subclasses can override
  92. [self.fieldEditorView endEditing:YES];
  93. }
  94. - (NSString *)titleForActionButton
  95. {
  96. // Subclasses can override.
  97. return @"Set";
  98. }
  99. - (void)exploreObjectOrPopViewController:(id)objectOrNil {
  100. if (objectOrNil) {
  101. // For non-nil (or void) return types, push an explorer view controller to display the object
  102. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:objectOrNil];
  103. [self.navigationController pushViewController:explorerViewController animated:YES];
  104. } else {
  105. // If we didn't get a returned object but the method call succeeded,
  106. // pop this view controller off the stack to indicate that the call went through.
  107. [self.navigationController popViewControllerAnimated:YES];
  108. }
  109. }
  110. @end