You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.8 KiB

  1. //
  2. // FLEXFieldEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/16/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXFieldEditorViewController.h"
  9. #import "FLEXFieldEditorView.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXUtility.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXArgumentInputView.h"
  14. #import "FLEXArgumentInputViewFactory.h"
  15. #import "FLEXObjectExplorerViewController.h"
  16. @interface FLEXFieldEditorViewController () <UIScrollViewDelegate>
  17. @property (nonatomic, strong) UIScrollView *scrollView;
  18. @property (nonatomic, strong, readwrite) id target;
  19. @property (nonatomic, strong, readwrite) FLEXFieldEditorView *fieldEditorView;
  20. @property (nonatomic, strong, readwrite) UIBarButtonItem *setterButton;
  21. @end
  22. @implementation FLEXFieldEditorViewController
  23. - (id)initWithTarget:(id)target
  24. {
  25. self = [super initWithNibName:nil bundle:nil];
  26. if (self) {
  27. self.target = target;
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (void)keyboardDidShow:(NSNotification *)notification
  38. {
  39. CGRect keyboardRectInWindow = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  40. CGSize keyboardSize = [self.view convertRect:keyboardRectInWindow fromView:nil].size;
  41. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  42. scrollInsets.bottom = keyboardSize.height;
  43. self.scrollView.contentInset = scrollInsets;
  44. self.scrollView.scrollIndicatorInsets = scrollInsets;
  45. // Find the active input view and scroll to make sure it's visible.
  46. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  47. if (argumentInputView.inputViewIsFirstResponder) {
  48. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  49. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  50. break;
  51. }
  52. }
  53. }
  54. - (void)keyboardWillHide:(NSNotification *)notification
  55. {
  56. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  57. scrollInsets.bottom = 0.0;
  58. self.scrollView.contentInset = scrollInsets;
  59. self.scrollView.scrollIndicatorInsets = scrollInsets;
  60. }
  61. - (void)viewDidLoad
  62. {
  63. [super viewDidLoad];
  64. self.view.backgroundColor = [FLEXUtility scrollViewGrayColor];
  65. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  66. self.scrollView.backgroundColor = self.view.backgroundColor;
  67. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  68. self.scrollView.delegate = self;
  69. [self.view addSubview:self.scrollView];
  70. self.fieldEditorView = [[FLEXFieldEditorView alloc] init];
  71. self.fieldEditorView.backgroundColor = self.view.backgroundColor;
  72. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  73. [self.scrollView addSubview:self.fieldEditorView];
  74. self.setterButton = [[UIBarButtonItem alloc] initWithTitle:[self titleForActionButton] style:UIBarButtonItemStyleDone target:self action:@selector(actionButtonPressed:)];
  75. self.navigationItem.rightBarButtonItem = self.setterButton;
  76. }
  77. - (void)viewWillLayoutSubviews
  78. {
  79. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  80. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  81. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  82. self.scrollView.contentSize = fieldEditorSize;
  83. }
  84. - (FLEXArgumentInputView *)firstInputView
  85. {
  86. return [[self.fieldEditorView argumentInputViews] firstObject];
  87. }
  88. - (void)actionButtonPressed:(id)sender
  89. {
  90. // Subclasses can override
  91. [self.fieldEditorView endEditing:YES];
  92. }
  93. - (NSString *)titleForActionButton
  94. {
  95. // Subclasses can override.
  96. return @"Set";
  97. }
  98. - (void)exploreObjectOrPopViewController:(id)objectOrNil {
  99. if (objectOrNil) {
  100. // For non-nil (or void) return types, push an explorer view controller to display the object
  101. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:objectOrNil];
  102. [self.navigationController pushViewController:explorerViewController animated:YES];
  103. } else {
  104. // If we didn't get a returned object but the method call succeeded,
  105. // pop this view controller off the stack to indicate that the call went through.
  106. [self.navigationController popViewControllerAnimated:YES];
  107. }
  108. }
  109. @end