FLEXVariableEditorViewController.m 4.8 KB

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