PickNicknameViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "PickNicknameViewController.h"
  21. #import "MyIdentityStore.h"
  22. #import "RectUtil.h"
  23. #import "UIDefines.h"
  24. #import "Utils.h"
  25. #import "IntroQuestionView.h"
  26. #import "MDMSetup.h"
  27. #import "LicenseStore.h"
  28. #import "ValidationLogger.h"
  29. #import "UIImage+ColoredImage.h"
  30. #import "NibUtil.h"
  31. @interface PickNicknameViewController () <UITextFieldDelegate, IntroQuestionDelegate>
  32. @property BOOL didShowNicknameAlert;
  33. @property CGFloat nicknameViewYOffset;
  34. @property IntroQuestionView *nicknameQuestionView;
  35. @end
  36. @implementation PickNicknameViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. [self setup];
  40. _nicknameViewYOffset = _nicknameView.frame.origin.y;
  41. }
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. _nicknameQuestionView.hidden = YES;
  45. [self updateData];
  46. }
  47. - (void)viewWillDisappear:(BOOL)animated {
  48. [super viewWillDisappear:animated];
  49. [_nicknameTextfield resignFirstResponder];
  50. NSString *nickname = self.nicknameTextfield.text;
  51. [MyIdentityStore sharedMyIdentityStore].pushFromName = nickname;
  52. [[LicenseStore sharedLicenseStore] performUpdateWorkInfo];
  53. }
  54. - (void)viewDidAppear:(BOOL)animated {
  55. [super viewDidAppear:animated];
  56. [self registerForKeyboardNotifications];
  57. }
  58. - (void)viewDidDisappear:(BOOL)animated {
  59. [super viewDidDisappear:animated];
  60. [self unregisterForKeyboardNotifications];
  61. }
  62. - (void)updateData {
  63. if ([MyIdentityStore sharedMyIdentityStore].pushFromName != nil) {
  64. self.nicknameTextfield.text = [MyIdentityStore sharedMyIdentityStore].pushFromName;
  65. }
  66. }
  67. - (void)adaptToSmallScreen {
  68. [super adaptToSmallScreen];
  69. CGFloat yOffset = -16.0;
  70. _descriptionLabel.frame = [RectUtil offsetRect:_descriptionLabel.frame byX:0.0 byY:yOffset];
  71. yOffset = -48.0;
  72. _nicknameQuestionView.frame = [RectUtil offsetRect:_nicknameQuestionView.frame byX:0.0 byY:yOffset];
  73. }
  74. - (void)setup {
  75. _nicknameView.layer.cornerRadius = 3;
  76. _nicknameView.layer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
  77. _nicknameView.layer.borderWidth = 0.5;
  78. _nicknameBackgroundView.layer.cornerRadius = 3;
  79. _nicknameBackgroundView.layer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
  80. _nicknameBackgroundView.layer.borderWidth = 0.5;
  81. NSString *placeholder = [BundleUtil localizedStringForKey:@"id_enter_nickname"];
  82. _nicknameTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: THREEMA_COLOR_PLACEHOLDER}];
  83. _nicknameTextfield.delegate = self;
  84. [_nicknameTextfield setAccessibilityHint:[BundleUtil localizedStringForKey:@"id_completed_nickname"]];
  85. _titleLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_title"];
  86. if ([LicenseStore requiresLicenseKey]) {
  87. _descriptionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_description_work"];
  88. } else {
  89. _descriptionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_description"];
  90. }
  91. self.moreView.mainView = self.mainContentView;
  92. self.moreView.moreMessageText = [BundleUtil localizedStringForKey:@"more_information_pick_nickname"];
  93. UITapGestureRecognizer *mainTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMainView:)];
  94. [self.mainContentView addGestureRecognizer:mainTapGesture];
  95. MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:YES];
  96. _nicknameTextfield.enabled = ![mdmSetup readonlyProfile];
  97. _nicknameTextfield.tintColor = [Colors mainThemeDark];
  98. UIImage *contactImage = [BundleUtil imageNamed:@"Contact"];
  99. _contactImageView.image = [contactImage imageWithTint:[UIColor whiteColor]];
  100. }
  101. - (BOOL)isInputValid {
  102. if ([self.moreView isShown]) {
  103. return NO;
  104. }
  105. NSString *nickname = self.nicknameTextfield.text;
  106. if (nickname.length > 0 || _didShowNicknameAlert) {
  107. return YES;
  108. } else {
  109. [self showAlert];
  110. return NO;
  111. }
  112. }
  113. - (void)hideAlert {
  114. [self hideMessageView:_nicknameQuestionView];
  115. }
  116. - (void)showAlert {
  117. [_nicknameTextfield resignFirstResponder];
  118. if (_nicknameQuestionView == nil) {
  119. _nicknameQuestionView = (IntroQuestionView *)[NibUtil loadViewFromNibWithName:@"IntroQuestionView"];
  120. _nicknameQuestionView.questionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_question"];
  121. _nicknameQuestionView.delegate = self;
  122. _nicknameQuestionView.frame = [RectUtil rect:_nicknameQuestionView.frame centerIn:self.view.frame round:YES];
  123. [self.view addSubview:_nicknameQuestionView];
  124. }
  125. [self showMessageView:_nicknameQuestionView];
  126. }
  127. #pragma mark - IntroQuestionViewDelegate
  128. - (void)selectedYes:(IntroQuestionView *)sender {
  129. [self hideAlert];
  130. _didShowNicknameAlert = YES;
  131. NSString *placeholder = [MyIdentityStore sharedMyIdentityStore].identity;
  132. _nicknameTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: THREEMA_COLOR_PLACEHOLDER}];
  133. }
  134. - (void)selectedNo:(IntroQuestionView *)sender {
  135. [self hideAlert];
  136. [_nicknameTextfield becomeFirstResponder];
  137. }
  138. #pragma mark - UITapGestureRecognizer
  139. - (void)tappedMainView:(UITapGestureRecognizer *)sender
  140. {
  141. if (sender.state == UIGestureRecognizerStateEnded) {
  142. [_nicknameTextfield resignFirstResponder];
  143. }
  144. }
  145. #pragma mark - UITextFieldDelegate
  146. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  147. if (textField == _nicknameTextfield) {
  148. [_nicknameTextfield resignFirstResponder];
  149. return NO;
  150. }
  151. return YES;
  152. }
  153. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  154. NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
  155. if ([newText lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > kPushFromNameLen)
  156. return NO;
  157. return YES;
  158. }
  159. # pragma mark Keyboard Notifications
  160. - (void)registerForKeyboardNotifications {
  161. [[NSNotificationCenter defaultCenter] addObserver:self
  162. selector:@selector(keyboardWillShow:)
  163. name:UIKeyboardWillShowNotification object:nil];
  164. [[NSNotificationCenter defaultCenter] addObserver:self
  165. selector:@selector(keyboardWillHide:)
  166. name:UIKeyboardWillHideNotification object:nil];
  167. }
  168. - (void)unregisterForKeyboardNotifications {
  169. [[NSNotificationCenter defaultCenter] removeObserver: self];
  170. }
  171. - (void)keyboardWillShow: (NSNotification*) notification {
  172. NSDictionary* info = [notification userInfo];
  173. CGRect keyboardRect = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
  174. CGRect keyboardRectConverted = [self.view convertRect:keyboardRect fromView:nil];
  175. CGFloat diff = CGRectGetMinY(keyboardRectConverted) - CGRectGetMaxY(_nicknameView.frame) - 32.0;
  176. if (diff < 0.0) {
  177. NSTimeInterval animationDuration;
  178. UIViewAnimationOptions options = [Utils animationOptionsFor:notification animationDuration:&animationDuration];
  179. [UIView animateWithDuration:animationDuration delay:0 options:options animations:^{
  180. _nicknameView.frame = [RectUtil offsetRect:_nicknameView.frame byX:0.0 byY:diff];
  181. } completion:^(BOOL finished) {}];
  182. }
  183. }
  184. - (void)keyboardWillHide:(NSNotification*)aNotification {
  185. _nicknameView.frame = [RectUtil setYPositionOf:_nicknameView.frame y:_nicknameViewYOffset];
  186. }
  187. @end