CustomTokenDataEntryViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "CustomTokenDataEntryViewController.h"
  17. /** @var navigationBarDefaultHeight
  18. @brief The default height to use for new navigation bars' frames.
  19. */
  20. static const NSUInteger navigationBarDefaultHeight = 55;
  21. /** @var navigationBarSystemHeight
  22. @brief Set after a navigation bar has been created to obtain the system-specified height of the
  23. navigation bar.
  24. */
  25. static NSUInteger navigationBarSystemHeight = navigationBarDefaultHeight;
  26. /** @var kTitle
  27. @brief The title of the view controller as it appears at the top of the screen in the navigation
  28. bar.
  29. */
  30. static NSString *const kTitle = @"Enter Token";
  31. /** @var kCancel
  32. @brief The text for the "Cancel" button.
  33. */
  34. static NSString *const kCancel = @"Cancel";
  35. /** @var kDone
  36. @brief The text for the "Done" button.
  37. */
  38. static NSString *const kDone = @"Done";
  39. @implementation CustomTokenDataEntryViewController {
  40. /** @var _completion
  41. @brief The block we will call when the user presses the "cancel" or "done" buttons.
  42. @remarks Passed into the initializer.
  43. */
  44. CustomTokenDataEntryViewControllerCompletion _completion;
  45. /** @var _tokenTextView
  46. @brief The text view allowing the user to enter their custom token text.
  47. @remarks Constructed and set in the method: @c loadTextView.
  48. */
  49. __weak UITextView *_Nullable _tokenTextView;
  50. }
  51. - (nullable instancetype)initWithCompletion:
  52. (CustomTokenDataEntryViewControllerCompletion)completion {
  53. self = [super initWithNibName:nil bundle:nil];
  54. if (self) {
  55. _completion = completion;
  56. }
  57. return self;
  58. }
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. [self loadHeader];
  62. [self loadTextView];
  63. }
  64. #pragma mark - View
  65. /** @fn loadHeader
  66. @brief Loads the header bar along the top of the view with "Cancel" and "Done" buttons, as well
  67. as a brief title asking the user to enter the custom token text.
  68. @remarks Updates navigationBarSystemHeight, so should be called before any method which depends
  69. on that variable being updated (like the @c loadTextView method, which uses the value to
  70. determine how much room is left on the screen.)
  71. */
  72. - (void)loadHeader {
  73. CGRect navBarFrame = CGRectMake(0, 0, self.view.bounds.size.width, navigationBarDefaultHeight);
  74. UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];
  75. navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth
  76. | UIViewAutoresizingFlexibleBottomMargin;
  77. UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:kTitle];
  78. UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:kCancel
  79. style:UIBarButtonItemStylePlain
  80. target:self
  81. action:@selector(cancelPressed:)];
  82. UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:kDone
  83. style:UIBarButtonItemStylePlain
  84. target:self
  85. action:@selector(donePressed:)];
  86. navItem.leftBarButtonItem = cancelButton;
  87. navItem.rightBarButtonItem = doneButton;
  88. [navBar setItems:@[ navItem ] animated:NO];
  89. [self.view addSubview:navBar];
  90. // Obtain the system-specified height of the navigation bar.
  91. navigationBarSystemHeight = navBar.frame.size.height;
  92. }
  93. /** @fn loadTextView
  94. @brief Loads the text field for the user to enter their custom token text.
  95. @remarks Relies on the navigationBarSystemHeight variable being correct.
  96. */
  97. - (void)loadTextView {
  98. CGRect tokenTextViewFrame =
  99. CGRectMake(0,
  100. navigationBarSystemHeight,
  101. self.view.bounds.size.width,
  102. self.view.bounds.size.height - navigationBarSystemHeight);
  103. UITextView *tokenTextView = [[UITextView alloc] initWithFrame:tokenTextViewFrame];
  104. tokenTextView.backgroundColor = [UIColor whiteColor];
  105. tokenTextView.textAlignment = NSTextAlignmentLeft;
  106. [self.view addSubview:tokenTextView];
  107. _tokenTextView = tokenTextView;
  108. }
  109. #pragma mark - Actions
  110. - (void)cancelPressed:(id)sender {
  111. [self finishByCancelling:YES withUserEnteredTokenText:nil];
  112. }
  113. - (void)donePressed:(id)sender {
  114. [self finishByCancelling:NO withUserEnteredTokenText:_tokenTextView.text];
  115. }
  116. #pragma mark - Workflow
  117. - (void)finishByCancelling:(BOOL)cancelled
  118. withUserEnteredTokenText:(nullable NSString *)userEnteredTokenText {
  119. [self dismissViewControllerAnimated:YES completion:^{
  120. _completion(cancelled,
  121. cancelled ? nil : userEnteredTokenText);
  122. }];
  123. }
  124. @end