123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2015-2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- #import "PickNicknameViewController.h"
- #import "MyIdentityStore.h"
- #import "RectUtil.h"
- #import "UIDefines.h"
- #import "Utils.h"
- #import "IntroQuestionView.h"
- #import "MDMSetup.h"
- #import "LicenseStore.h"
- #import "ValidationLogger.h"
- #import "UIImage+ColoredImage.h"
- #import "NibUtil.h"
- @interface PickNicknameViewController () <UITextFieldDelegate, IntroQuestionDelegate>
- @property BOOL didShowNicknameAlert;
- @property CGFloat nicknameViewYOffset;
- @property IntroQuestionView *nicknameQuestionView;
- @end
- @implementation PickNicknameViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self setup];
-
- _nicknameViewYOffset = _nicknameView.frame.origin.y;
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- _nicknameQuestionView.hidden = YES;
-
- [self updateData];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [_nicknameTextfield resignFirstResponder];
-
- NSString *nickname = self.nicknameTextfield.text;
- [MyIdentityStore sharedMyIdentityStore].pushFromName = nickname;
- [[LicenseStore sharedLicenseStore] performUpdateWorkInfo];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- [self registerForKeyboardNotifications];
- }
- - (void)viewDidDisappear:(BOOL)animated {
- [super viewDidDisappear:animated];
-
- [self unregisterForKeyboardNotifications];
- }
- - (void)updateData {
- if ([MyIdentityStore sharedMyIdentityStore].pushFromName != nil) {
- self.nicknameTextfield.text = [MyIdentityStore sharedMyIdentityStore].pushFromName;
- }
- }
- - (void)adaptToSmallScreen {
- [super adaptToSmallScreen];
-
- CGFloat yOffset = -16.0;
- _descriptionLabel.frame = [RectUtil offsetRect:_descriptionLabel.frame byX:0.0 byY:yOffset];
- yOffset = -48.0;
- _nicknameQuestionView.frame = [RectUtil offsetRect:_nicknameQuestionView.frame byX:0.0 byY:yOffset];
- }
- - (void)setup {
- _nicknameView.layer.cornerRadius = 3;
- _nicknameView.layer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
- _nicknameView.layer.borderWidth = 0.5;
- _nicknameBackgroundView.layer.cornerRadius = 3;
- _nicknameBackgroundView.layer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
- _nicknameBackgroundView.layer.borderWidth = 0.5;
- NSString *placeholder = [BundleUtil localizedStringForKey:@"id_enter_nickname"];
- _nicknameTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: THREEMA_COLOR_PLACEHOLDER}];
- _nicknameTextfield.delegate = self;
- [_nicknameTextfield setAccessibilityHint:[BundleUtil localizedStringForKey:@"id_completed_nickname"]];
-
- _titleLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_title"];
-
- if ([LicenseStore requiresLicenseKey]) {
- _descriptionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_description_work"];
- } else {
- _descriptionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_description"];
- }
-
- self.moreView.mainView = self.mainContentView;
- self.moreView.moreMessageText = [BundleUtil localizedStringForKey:@"more_information_pick_nickname"];
- UITapGestureRecognizer *mainTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMainView:)];
- [self.mainContentView addGestureRecognizer:mainTapGesture];
-
- MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:YES];
- _nicknameTextfield.enabled = ![mdmSetup readonlyProfile];
-
- _nicknameTextfield.tintColor = [Colors mainThemeDark];
-
- UIImage *contactImage = [BundleUtil imageNamed:@"Contact"];
- _contactImageView.image = [contactImage imageWithTint:[UIColor whiteColor]];
- }
- - (BOOL)isInputValid {
- if ([self.moreView isShown]) {
- return NO;
- }
-
- NSString *nickname = self.nicknameTextfield.text;
- if (nickname.length > 0 || _didShowNicknameAlert) {
- return YES;
- } else {
- [self showAlert];
-
- return NO;
- }
- }
- - (void)hideAlert {
- [self hideMessageView:_nicknameQuestionView];
- }
- - (void)showAlert {
- [_nicknameTextfield resignFirstResponder];
-
- if (_nicknameQuestionView == nil) {
- _nicknameQuestionView = (IntroQuestionView *)[NibUtil loadViewFromNibWithName:@"IntroQuestionView"];
- _nicknameQuestionView.questionLabel.text = [BundleUtil localizedStringForKey:@"id_pick_nickname_question"];
- _nicknameQuestionView.delegate = self;
- _nicknameQuestionView.frame = [RectUtil rect:_nicknameQuestionView.frame centerIn:self.view.frame round:YES];
- [self.view addSubview:_nicknameQuestionView];
- }
-
- [self showMessageView:_nicknameQuestionView];
- }
- #pragma mark - IntroQuestionViewDelegate
- - (void)selectedYes:(IntroQuestionView *)sender {
- [self hideAlert];
- _didShowNicknameAlert = YES;
- NSString *placeholder = [MyIdentityStore sharedMyIdentityStore].identity;
- _nicknameTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: THREEMA_COLOR_PLACEHOLDER}];
- }
- - (void)selectedNo:(IntroQuestionView *)sender {
- [self hideAlert];
- [_nicknameTextfield becomeFirstResponder];
- }
- #pragma mark - UITapGestureRecognizer
- - (void)tappedMainView:(UITapGestureRecognizer *)sender
- {
- if (sender.state == UIGestureRecognizerStateEnded) {
- [_nicknameTextfield resignFirstResponder];
- }
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- if (textField == _nicknameTextfield) {
- [_nicknameTextfield resignFirstResponder];
-
- return NO;
- }
-
- return YES;
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
- if ([newText lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > kPushFromNameLen)
- return NO;
- return YES;
- }
- # pragma mark Keyboard Notifications
- - (void)registerForKeyboardNotifications {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShow:)
- name:UIKeyboardWillShowNotification object:nil];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillHide:)
- name:UIKeyboardWillHideNotification object:nil];
- }
- - (void)unregisterForKeyboardNotifications {
- [[NSNotificationCenter defaultCenter] removeObserver: self];
- }
- - (void)keyboardWillShow: (NSNotification*) notification {
- NSDictionary* info = [notification userInfo];
- CGRect keyboardRect = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
- CGRect keyboardRectConverted = [self.view convertRect:keyboardRect fromView:nil];
-
- CGFloat diff = CGRectGetMinY(keyboardRectConverted) - CGRectGetMaxY(_nicknameView.frame) - 32.0;
- if (diff < 0.0) {
- NSTimeInterval animationDuration;
- UIViewAnimationOptions options = [Utils animationOptionsFor:notification animationDuration:&animationDuration];
-
- [UIView animateWithDuration:animationDuration delay:0 options:options animations:^{
-
- _nicknameView.frame = [RectUtil offsetRect:_nicknameView.frame byX:0.0 byY:diff];
- } completion:^(BOOL finished) {}];
- }
- }
- - (void)keyboardWillHide:(NSNotification*)aNotification {
- _nicknameView.frame = [RectUtil setYPositionOf:_nicknameView.frame y:_nicknameViewYOffset];
- }
- @end
|