FLEXVariableEditorViewController.m 4.7 KB

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