FLEXVariableEditorViewController.m 4.7 KB

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