FLEXVariableEditorViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #import "FLEXArgumentInputDateView.h"
  19. @interface FLEXVariableEditorViewController () <UIScrollViewDelegate>
  20. @property (nonatomic) UIScrollView *scrollView;
  21. @end
  22. @implementation FLEXVariableEditorViewController
  23. #pragma mark - Initialization
  24. + (instancetype)target:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit {
  25. return [[self alloc] initWithTarget:target data:data commitHandler:onCommit];
  26. }
  27. - (id)initWithTarget:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit {
  28. self = [super init];
  29. if (self) {
  30. _target = target;
  31. _data = data;
  32. _commitHandler = onCommit;
  33. [NSNotificationCenter.defaultCenter
  34. addObserver:self selector:@selector(keyboardDidShow:)
  35. name:UIKeyboardDidShowNotification object:nil
  36. ];
  37. [NSNotificationCenter.defaultCenter
  38. addObserver:self selector:@selector(keyboardWillHide:)
  39. name:UIKeyboardWillHideNotification object:nil
  40. ];
  41. }
  42. return self;
  43. }
  44. - (void)dealloc {
  45. [NSNotificationCenter.defaultCenter removeObserver:self];
  46. }
  47. #pragma mark - UIViewController methods
  48. - (void)keyboardDidShow:(NSNotification *)notification {
  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. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  66. scrollInsets.bottom = 0.0;
  67. self.scrollView.contentInset = scrollInsets;
  68. self.scrollView.scrollIndicatorInsets = scrollInsets;
  69. }
  70. - (void)viewDidLoad {
  71. [super viewDidLoad];
  72. self.view.backgroundColor = FLEXColor.scrollViewBackgroundColor;
  73. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  74. self.scrollView.backgroundColor = self.view.backgroundColor;
  75. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  76. self.scrollView.delegate = self;
  77. [self.view addSubview:self.scrollView];
  78. _fieldEditorView = [FLEXFieldEditorView new];
  79. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  80. [self.scrollView addSubview:self.fieldEditorView];
  81. #if !TARGET_OS_TV
  82. _actionButton = [[UIBarButtonItem alloc]
  83. initWithTitle:@"Set"
  84. style:UIBarButtonItemStyleDone
  85. target:self
  86. action:@selector(actionButtonPressed:)
  87. ];
  88. self.navigationController.toolbarHidden = NO;
  89. self.toolbarItems = @[UIBarButtonItem.flex_flexibleSpace, self.actionButton];
  90. #else
  91. _actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
  92. [_actionButton setTitle:@"Set" forState:UIControlStateNormal];
  93. [_actionButton addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventPrimaryActionTriggered];
  94. _actionButton.frame = CGRectMake(500, 600, 200, 60);
  95. [self.view addSubview:_actionButton];
  96. #endif
  97. }
  98. - (void)viewWillLayoutSubviews {
  99. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  100. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  101. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  102. #if TARGET_OS_TV
  103. CGRect actionFrame = _actionButton.frame;
  104. CGRect fieldEditorFrame = self.fieldEditorView.frame;
  105. CGFloat buttonOffset = (fieldEditorFrame.origin.y + fieldEditorFrame.size.height) + (130 + 67);
  106. actionFrame.origin.y = buttonOffset;
  107. _actionButton.frame = actionFrame;
  108. #endif
  109. self.scrollView.contentSize = fieldEditorSize;
  110. }
  111. #pragma mark - Public
  112. - (FLEXArgumentInputView *)firstInputView {
  113. return [self.fieldEditorView argumentInputViews].firstObject;
  114. }
  115. - (void)actionButtonPressed:(id)sender {
  116. // Subclasses can override
  117. [self.fieldEditorView endEditing:YES];
  118. if (_commitHandler) {
  119. _commitHandler();
  120. }
  121. }
  122. - (void)exploreObjectOrPopViewController:(id)objectOrNil {
  123. if (objectOrNil) {
  124. // For non-nil (or void) return types, push an explorer view controller to display the object
  125. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:objectOrNil];
  126. [self.navigationController pushViewController:explorerViewController animated:YES];
  127. } else {
  128. // If we didn't get a returned object but the method call succeeded,
  129. // pop this view controller off the stack to indicate that the call went through.
  130. [self.navigationController popViewControllerAnimated:YES];
  131. }
  132. }
  133. @end