2019-04-26 07:57:06 +02:00
2019-09-24 03:04:49 +02:00
final class DisplayNameVC : OnboardingBaseViewController {
2019-04-26 07:57:06 +02:00
2019-05-01 05:58:35 +02:00
private lazy var userNameTextField : UITextField = {
2019-04-26 07:57:06 +02:00
let result = UITextField ( )
result . textColor = Theme . primaryColor
2019-10-15 00:43:58 +02:00
result . font = . ows_dynamicTypeBodyClamped
2019-04-26 07:57:06 +02:00
result . textAlignment = . center
2019-10-11 04:19:57 +02:00
let placeholder = NSMutableAttributedString ( string : NSLocalizedString ( " Display Name " , comment : " " ) )
2019-06-14 07:25:39 +02:00
placeholder . addAttribute ( . foregroundColor , value : Theme . placeholderColor , range : NSRange ( location : 0 , length : placeholder . length ) )
result . attributedPlaceholder = placeholder
2019-10-15 00:43:58 +02:00
result . tintColor = . lokiGreen ( )
2019-05-01 05:58:35 +02:00
result . accessibilityIdentifier = " onboarding.accountDetailsStep.userNameTextField "
2019-06-14 07:25:39 +02:00
result . keyboardAppearance = . dark
2019-04-26 07:57:06 +02:00
return result
} ( )
2019-04-29 05:48:27 +02:00
2019-04-26 07:57:06 +02:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
view . backgroundColor = Theme . backgroundColor
view . layoutMargins = . zero
let titleLabel = self . createTitleLabel ( text : NSLocalizedString ( " Create Your Loki Messenger Account " , comment : " " ) )
titleLabel . accessibilityIdentifier = " onboarding.accountDetailsStep.titleLabel "
let topSpacer = UIView . vStretchingSpacer ( )
2019-04-29 03:34:11 +02:00
let displayNameLabel = createExplanationLabel ( text : NSLocalizedString ( " Enter a name to be shown to your contacts " , comment : " " ) )
2019-04-26 07:57:06 +02:00
displayNameLabel . accessibilityIdentifier = " onboarding.accountDetailsStep.displayNameLabel "
let bottomSpacer = UIView . vStretchingSpacer ( )
2019-04-30 06:27:39 +02:00
let nextButton = createButton ( title : NSLocalizedString ( " Next " , comment : " " ) , selector : #selector ( handleNextButtonPressed ) )
2019-04-26 07:57:06 +02:00
nextButton . accessibilityIdentifier = " onboarding.accountDetailsStep.nextButton "
let stackView = UIStackView ( arrangedSubviews : [
titleLabel ,
topSpacer ,
displayNameLabel ,
UIView . spacer ( withHeight : 8 ) ,
2019-05-01 05:58:35 +02:00
userNameTextField ,
2019-04-26 07:57:06 +02:00
bottomSpacer ,
nextButton
2019-04-26 08:13:29 +02:00
] )
2019-04-26 07:57:06 +02:00
stackView . axis = . vertical
stackView . alignment = . fill
stackView . layoutMargins = UIEdgeInsets ( top : 32 , left : 32 , bottom : 32 , right : 32 )
stackView . isLayoutMarginsRelativeArrangement = true
view . addSubview ( stackView )
stackView . autoPinWidthToSuperview ( )
stackView . autoPin ( toTopLayoutGuideOf : self , withInset : 0 )
autoPinView ( toBottomOfViewControllerOrKeyboard : stackView , avoidNotch : true )
topSpacer . autoMatch ( . height , to : . height , of : bottomSpacer )
2019-10-03 03:30:45 +02:00
Analytics . shared . track ( " Display Name Screen Viewed " )
2019-04-26 07:57:06 +02:00
}
2019-05-13 07:21:16 +02:00
override func viewDidAppear ( _ animated : Bool ) {
2019-04-26 07:57:06 +02:00
super . viewDidAppear ( animated )
2019-05-01 05:58:35 +02:00
userNameTextField . becomeFirstResponder ( )
2019-04-26 07:57:06 +02:00
}
2019-04-30 06:27:39 +02:00
@objc private func handleNextButtonPressed ( ) {
2019-10-11 04:19:57 +02:00
let displayName = userNameTextField . text ! . ows_stripped ( )
guard ! displayName . isEmpty else {
return OWSAlerts . showErrorAlert ( message : NSLocalizedString ( " Please pick a display name " , comment : " " ) )
}
2019-11-18 05:59:50 +01:00
guard displayName . allSatisfy ( { " 0 " . . . " 9 " ~= $0 || " a " . . . " z " ~= $0 || " A " . . . " Z " ~= $0 || $0 = = " _ " || $0 = = " " } ) else {
2019-10-21 06:59:06 +02:00
return OWSAlerts . showErrorAlert ( message : NSLocalizedString ( " Please pick a display name that consists of only a-z, A-Z, 0-9 and _ characters " , comment : " " ) )
}
2019-10-11 04:19:57 +02:00
guard ! OWSProfileManager . shared ( ) . isProfileNameTooLong ( displayName ) else {
return OWSAlerts . showErrorAlert ( message : NSLocalizedString ( " Please pick a shorter display name " , comment : " " ) )
2019-04-29 05:48:27 +02:00
}
2019-09-18 03:09:09 +02:00
TSAccountManager . sharedInstance ( ) . didRegister ( )
UserDefaults . standard . set ( true , forKey : " didUpdateForMainnet " )
onboardingController . verificationDidComplete ( fromView : self )
2019-10-15 01:50:06 +02:00
let appDelegate = UIApplication . shared . delegate as ! AppDelegate
appDelegate . setUpDefaultPublicChatsIfNeeded ( )
appDelegate . createRSSFeedsIfNeeded ( )
LokiPublicChatManager . shared . startPollersIfNeeded ( )
appDelegate . startRSSFeedPollersIfNeeded ( )
2019-10-11 04:19:57 +02:00
OWSProfileManager . shared ( ) . updateLocalProfileName ( displayName , avatarImage : nil , success : { } , failure : { } ) // T r y t o s a v e t h e u s e r n a m e b u t i g n o r e t h e r e s u l t
2019-04-26 07:57:06 +02:00
}
}