mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			142 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			142 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			Swift
		
	
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 | final class DisplayNameVC : BaseVC { | ||
| 
											6 years ago
										 |     private var spacer1HeightConstraint: NSLayoutConstraint! | ||
|  |     private var spacer2HeightConstraint: NSLayoutConstraint! | ||
|  |     private var registerButtonBottomOffsetConstraint: NSLayoutConstraint! | ||
|  |     private var bottomConstraint: NSLayoutConstraint! | ||
|  |      | ||
|  |     // MARK: Components | ||
|  |     private lazy var displayNameTextField: TextField = { | ||
| 
											5 years ago
										 |         let result = TextField(placeholder: NSLocalizedString("vc_display_name_text_field_hint", comment: "")) | ||
| 
											6 years ago
										 |         result.layer.borderColor = Colors.text.cgColor | ||
| 
											5 years ago
										 |         result.accessibilityLabel = "Display name text field" | ||
| 
											6 years ago
										 |         return result | ||
|  |     }() | ||
|  |      | ||
|  |     // MARK: Lifecycle | ||
|  |     override func viewDidLoad() { | ||
| 
											6 years ago
										 |         super.viewDidLoad() | ||
| 
											6 years ago
										 |         setUpGradientBackground() | ||
|  |         setUpNavBarStyle() | ||
|  |         setUpNavBarSessionIcon() | ||
| 
											6 years ago
										 |         // Set up title label | ||
|  |         let titleLabel = UILabel() | ||
|  |         titleLabel.textColor = Colors.text | ||
| 
											5 years ago
										 |         titleLabel.font = .boldSystemFont(ofSize: isIPhone5OrSmaller ? Values.largeFontSize : Values.veryLargeFontSize) | ||
| 
											5 years ago
										 |         titleLabel.text = NSLocalizedString("vc_display_name_title_2", comment: "") | ||
| 
											6 years ago
										 |         titleLabel.numberOfLines = 0 | ||
|  |         titleLabel.lineBreakMode = .byWordWrapping | ||
|  |         // Set up explanation label | ||
|  |         let explanationLabel = UILabel() | ||
|  |         explanationLabel.textColor = Colors.text | ||
|  |         explanationLabel.font = .systemFont(ofSize: Values.smallFontSize) | ||
| 
											5 years ago
										 |         explanationLabel.text = NSLocalizedString("vc_display_name_explanation", comment: "") | ||
| 
											6 years ago
										 |         explanationLabel.numberOfLines = 0 | ||
|  |         explanationLabel.lineBreakMode = .byWordWrapping | ||
|  |         // Set up spacers | ||
|  |         let topSpacer = UIView.vStretchingSpacer() | ||
|  |         let spacer1 = UIView() | ||
| 
											5 years ago
										 |         spacer1HeightConstraint = spacer1.set(.height, to: isIPhone5OrSmaller ? Values.smallSpacing : Values.veryLargeSpacing) | ||
| 
											6 years ago
										 |         let spacer2 = UIView() | ||
| 
											5 years ago
										 |         spacer2HeightConstraint = spacer2.set(.height, to: isIPhone5OrSmaller ? Values.smallSpacing : Values.veryLargeSpacing) | ||
| 
											6 years ago
										 |         let bottomSpacer = UIView.vStretchingSpacer() | ||
|  |         let registerButtonBottomOffsetSpacer = UIView() | ||
|  |         registerButtonBottomOffsetConstraint = registerButtonBottomOffsetSpacer.set(.height, to: Values.onboardingButtonBottomOffset) | ||
|  |         // Set up register button | ||
|  |         let registerButton = Button(style: .prominentFilled, size: .large) | ||
| 
											5 years ago
										 |         registerButton.setTitle(NSLocalizedString("continue_2", comment: ""), for: UIControl.State.normal) | ||
| 
											6 years ago
										 |         registerButton.titleLabel!.font = .boldSystemFont(ofSize: Values.mediumFontSize) | ||
|  |         registerButton.addTarget(self, action: #selector(register), for: UIControl.Event.touchUpInside) | ||
|  |         // Set up register button container | ||
|  |         let registerButtonContainer = UIView() | ||
|  |         registerButtonContainer.addSubview(registerButton) | ||
|  |         registerButton.pin(.leading, to: .leading, of: registerButtonContainer, withInset: Values.massiveSpacing) | ||
|  |         registerButton.pin(.top, to: .top, of: registerButtonContainer) | ||
|  |         registerButtonContainer.pin(.trailing, to: .trailing, of: registerButton, withInset: Values.massiveSpacing) | ||
|  |         registerButtonContainer.pin(.bottom, to: .bottom, of: registerButton) | ||
|  |         // Set up top stack view | ||
|  |         let topStackView = UIStackView(arrangedSubviews: [ titleLabel, spacer1, explanationLabel, spacer2, displayNameTextField ]) | ||
|  |         topStackView.axis = .vertical | ||
|  |         topStackView.alignment = .fill | ||
|  |         // Set up top stack view container | ||
|  |         let topStackViewContainer = UIView() | ||
|  |         topStackViewContainer.addSubview(topStackView) | ||
|  |         topStackView.pin(.leading, to: .leading, of: topStackViewContainer, withInset: Values.veryLargeSpacing) | ||
|  |         topStackView.pin(.top, to: .top, of: topStackViewContainer) | ||
|  |         topStackViewContainer.pin(.trailing, to: .trailing, of: topStackView, withInset: Values.veryLargeSpacing) | ||
|  |         topStackViewContainer.pin(.bottom, to: .bottom, of: topStackView) | ||
|  |         // Set up main stack view | ||
|  |         let mainStackView = UIStackView(arrangedSubviews: [ topSpacer, topStackViewContainer, bottomSpacer, registerButtonContainer, registerButtonBottomOffsetSpacer ]) | ||
|  |         mainStackView.axis = .vertical | ||
|  |         mainStackView.alignment = .fill | ||
|  |         view.addSubview(mainStackView) | ||
|  |         mainStackView.pin(.leading, to: .leading, of: view) | ||
|  |         mainStackView.pin(.top, to: .top, of: view) | ||
|  |         mainStackView.pin(.trailing, to: .trailing, of: view) | ||
|  |         bottomConstraint = mainStackView.pin(.bottom, to: .bottom, of: view) | ||
|  |         topSpacer.heightAnchor.constraint(equalTo: bottomSpacer.heightAnchor, multiplier: 1).isActive = true | ||
|  |         // Dismiss keyboard on tap | ||
|  |         let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) | ||
|  |         view.addGestureRecognizer(tapGestureRecognizer) | ||
|  |         // Listen to keyboard notifications | ||
|  |         let notificationCenter = NotificationCenter.default | ||
|  |         notificationCenter.addObserver(self, selector: #selector(handleKeyboardWillChangeFrameNotification(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) | ||
|  |         notificationCenter.addObserver(self, selector: #selector(handleKeyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) | ||
|  |     } | ||
|  |      | ||
|  |     override func viewDidAppear(_ animated: Bool) { | ||
|  |         super.viewDidAppear(animated) | ||
|  |         displayNameTextField.becomeFirstResponder() | ||
|  |     } | ||
|  |      | ||
|  |     deinit { | ||
|  |         NotificationCenter.default.removeObserver(self) | ||
|  |     } | ||
|  |      | ||
|  |     // MARK: General | ||
|  |     @objc private func dismissKeyboard() { | ||
|  |         displayNameTextField.resignFirstResponder() | ||
|  |     } | ||
|  |      | ||
|  |     // MARK: Updating | ||
|  |     @objc private func handleKeyboardWillChangeFrameNotification(_ notification: Notification) { | ||
|  |         guard let newHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height else { return } | ||
|  |         bottomConstraint.constant = -newHeight // Negative due to how the constraint is set up | ||
| 
											5 years ago
										 |         registerButtonBottomOffsetConstraint.constant = isIPhone5OrSmaller ? Values.smallSpacing : Values.largeSpacing | ||
|  |         spacer1HeightConstraint.constant = isIPhone5OrSmaller ? Values.smallSpacing : Values.mediumSpacing | ||
|  |         spacer2HeightConstraint.constant = isIPhone5OrSmaller ? Values.smallSpacing : Values.mediumSpacing | ||
| 
											6 years ago
										 |         UIView.animate(withDuration: 0.25) { | ||
|  |             self.view.layoutIfNeeded() | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     @objc private func handleKeyboardWillHideNotification(_ notification: Notification) { | ||
|  |         bottomConstraint.constant = 0 | ||
|  |         registerButtonBottomOffsetConstraint.constant = Values.onboardingButtonBottomOffset | ||
| 
											5 years ago
										 |         spacer1HeightConstraint.constant = isIPhone5OrSmaller ? Values.smallSpacing : Values.veryLargeSpacing | ||
|  |         spacer2HeightConstraint.constant = isIPhone5OrSmaller ? Values.smallSpacing : Values.veryLargeSpacing | ||
| 
											6 years ago
										 |         UIView.animate(withDuration: 0.25) { | ||
|  |             self.view.layoutIfNeeded() | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     // MARK: Interaction | ||
|  |     @objc private func register() { | ||
|  |         func showError(title: String, message: String = "") { | ||
|  |             let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | ||
|  |             alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)) | ||
|  |             presentAlert(alert) | ||
|  |         } | ||
|  |         let displayName = displayNameTextField.text!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) | ||
|  |         guard !displayName.isEmpty else { | ||
| 
											5 years ago
										 |             return showError(title: NSLocalizedString("vc_display_name_display_name_missing_error", comment: "")) | ||
| 
											6 years ago
										 |         } | ||
|  |         guard !OWSProfileManager.shared().isProfileNameTooLong(displayName) else { | ||
| 
											5 years ago
										 |             return showError(title: NSLocalizedString("vc_display_name_display_name_too_long_error", comment: "")) | ||
| 
											6 years ago
										 |         } | ||
| 
											6 years ago
										 |         OWSProfileManager.shared().updateLocalProfileName(displayName, avatarImage: nil, success: { }, failure: { _ in }, requiresSync: false) // Try to save the user name but ignore the result | ||
| 
											6 years ago
										 |         let pnModeVC = PNModeVC() | ||
|  |         navigationController!.pushViewController(pnModeVC, animated: true) | ||
| 
											6 years ago
										 |     } | ||
|  | } |