FLEXVariableEditorViewController.m 5.1 KB

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