FLEXVariableEditorViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. NSLog(@"[FLEXLog] keyboardDidShow!");
  49. CGRect keyboardRectInWindow = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  50. CGSize keyboardSize = [self.view convertRect:keyboardRectInWindow fromView:nil].size;
  51. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  52. scrollInsets.bottom = keyboardSize.height;
  53. self.scrollView.contentInset = scrollInsets;
  54. self.scrollView.scrollIndicatorInsets = scrollInsets;
  55. // Find the active input view and scroll to make sure it's visible.
  56. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  57. if (argumentInputView.inputViewIsFirstResponder) {
  58. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  59. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  60. break;
  61. }
  62. }
  63. }
  64. - (void)keyboardWillHide:(NSNotification *)notification {
  65. NSLog(@"[FLEXLog] keyboardWillHide!");
  66. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  67. scrollInsets.bottom = 0.0;
  68. self.scrollView.contentInset = scrollInsets;
  69. self.scrollView.scrollIndicatorInsets = scrollInsets;
  70. }
  71. - (void)viewDidLoad {
  72. [super viewDidLoad];
  73. self.view.backgroundColor = FLEXColor.scrollViewBackgroundColor;
  74. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  75. self.scrollView.backgroundColor = self.view.backgroundColor;
  76. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  77. self.scrollView.delegate = self;
  78. [self.view addSubview:self.scrollView];
  79. _fieldEditorView = [FLEXFieldEditorView new];
  80. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  81. [self.scrollView addSubview:self.fieldEditorView];
  82. _actionButton = [[UIBarButtonItem alloc]
  83. initWithTitle:@"Set"
  84. style:UIBarButtonItemStyleDone
  85. target:self
  86. action:@selector(actionButtonPressed:)
  87. ];
  88. #if !TARGET_OS_TV
  89. self.navigationController.toolbarHidden = NO;
  90. self.toolbarItems = @[UIBarButtonItem.flex_flexibleSpace, self.actionButton];
  91. #endif
  92. }
  93. - (void)viewWillLayoutSubviews {
  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. return [self.fieldEditorView argumentInputViews].firstObject;
  102. }
  103. - (void)actionButtonPressed:(id)sender {
  104. // Subclasses can override
  105. [self.fieldEditorView endEditing:YES];
  106. if (_commitHandler) {
  107. _commitHandler();
  108. }
  109. }
  110. - (void)exploreObjectOrPopViewController:(id)objectOrNil {
  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