From 66e562d6c5538ba9bfeddf3535ff3723b44a2f67 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Mon, 12 Nov 2018 02:56:13 +0400 Subject: [PATCH] More 2FA improvements --- TelegramUI/SecureIdAuthController.swift | 4 ---- .../SetupTwoStepVerificationContentNode.swift | 16 ++++++++++++++ ...tupTwoStepVerificationControllerNode.swift | 21 ++++++++++++++----- .../TwoStepVerificationUnlockController.swift | 3 +++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/TelegramUI/SecureIdAuthController.swift b/TelegramUI/SecureIdAuthController.swift index c0655c9430..2bf10dc833 100644 --- a/TelegramUI/SecureIdAuthController.swift +++ b/TelegramUI/SecureIdAuthController.swift @@ -568,13 +568,9 @@ final class SecureIdAuthController: ViewController { if let password = password { strongSelf.checkPassword(password: password, inBackground: true, completion: { [weak controller] in controller?.dismiss() - if let strongSelf = self { - strongSelf.present(OverlayStatusController(theme: strongSelf.presentationData.theme, strings: strongSelf.presentationData.strings, type: .genericSuccess(strongSelf.presentationData.strings.TwoStepAuth_EnabledSuccess)), in: .window(.root)) - } }) } else if shouldDismiss { controller.dismiss() - strongSelf.present(OverlayStatusController(theme: strongSelf.presentationData.theme, strings: strongSelf.presentationData.strings, type: .genericSuccess(strongSelf.presentationData.strings.TwoStepAuth_EnabledSuccess)), in: .window(.root)) } } }) diff --git a/TelegramUI/SetupTwoStepVerificationContentNode.swift b/TelegramUI/SetupTwoStepVerificationContentNode.swift index ad9e1bcbd8..f03c3ad54a 100644 --- a/TelegramUI/SetupTwoStepVerificationContentNode.swift +++ b/TelegramUI/SetupTwoStepVerificationContentNode.swift @@ -29,6 +29,7 @@ final class SetupTwoStepVerificationContentNode: ASDisplayNode, UITextFieldDeleg private let rightActionButton: HighlightableButtonNode private var isEnabled = true + private var clearOnce: Bool = false init(theme: PresentationTheme, kind: SetupTwoStepVerificationStateKind, title: String, subtitle: String, inputType: SetupTwoStepVerificationInputType, placeholder: String, text: String, isPassword: Bool, textUpdated: @escaping (String) -> Void, returnPressed: @escaping () -> Void, leftAction: SetupTwoStepVerificationContentAction?, rightAction: SetupTwoStepVerificationContentAction?) { self.leftAction = leftAction @@ -153,10 +154,25 @@ final class SetupTwoStepVerificationContentNode: ASDisplayNode, UITextFieldDeleg self.inputNode.textField.becomeFirstResponder() } + func dataEntryError() { + self.clearOnce = true + } + @objc private func inputNodeTextChanged(_ textField: UITextField) { self.textUpdated(textField.text ?? "") } + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + if self.clearOnce { + self.clearOnce = false + if range.length > string.count { + textField.text = "" + return false + } + } + return true + } + func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.returnPressed() return false diff --git a/TelegramUI/SetupTwoStepVerificationControllerNode.swift b/TelegramUI/SetupTwoStepVerificationControllerNode.swift index fdb32cc860..620f104f00 100644 --- a/TelegramUI/SetupTwoStepVerificationControllerNode.swift +++ b/TelegramUI/SetupTwoStepVerificationControllerNode.swift @@ -32,7 +32,7 @@ private enum EnterEmailState: Equatable { private enum ConfirmEmailState: Equatable { case create(password: String, hint: String, email: String) - case add(password: String, hasSecureValues: Bool, email: String) + case add(password: String, hadRecoveryEmail: Bool, hasSecureValues: Bool, email: String) case confirm(password: String?, hasSecureValues: Bool, pattern: String, codeLength: Int32?) } @@ -326,8 +326,18 @@ final class SetupTwoStepVerificationControllerNode: ViewControllerTracingNode { }, transition: .animated(duration: 0.5, curve: .spring)) })) }) - case let .add(_, _, email): + case let .add(password, hadRecoveryEmail, hasSecureValues, email): emailPattern = email + leftAction = SetupTwoStepVerificationContentAction(title: "Change E-Mail", action: { [weak self] in + guard let strongSelf = self else { + return + } + strongSelf.updateState({ state in + var state = state + state.data.state = .enterEmail(state: .add(hadRecoveryEmail: hadRecoveryEmail, hasSecureValues: hasSecureValues, password: password), email: "") + return state + }, transition: .animated(duration: 0.5, curve: .spring)) + }) case let .confirm(_, _, pattern, _): emailPattern = pattern } @@ -541,7 +551,7 @@ final class SetupTwoStepVerificationControllerNode: ViewControllerTracingNode { return state }, transition: .animated(duration: 0.5, curve: .spring)) })) - case let .add(_, hasSecureValues, password): + case let .add(hadRecoveryEmail, hasSecureValues, password): strongSelf.updateState({ state in var state = state state.data.activity = true @@ -561,7 +571,7 @@ final class SetupTwoStepVerificationControllerNode: ViewControllerTracingNode { break case let .password(password, pendingEmail): if let pendingEmail = pendingEmail { - state.data.state = .confirmEmail(state: .add(password: password, hasSecureValues: hasSecureValues, email: email), pattern: pendingEmail.pattern, codeLength: pendingEmail.codeLength, code: "") + state.data.state = .confirmEmail(state: .add(password: password, hadRecoveryEmail: hadRecoveryEmail, hasSecureValues: hasSecureValues, email: email), pattern: pendingEmail.pattern, codeLength: pendingEmail.codeLength, code: "") strongSelf.stateUpdated(.awaitingEmailConfirmation(password: password, pattern: pendingEmail.pattern, codeLength: pendingEmail.codeLength), false) } else { strongSelf.stateUpdated(.passwordSet(password: password, hasRecoveryEmail: true, hasSecureValues: hasSecureValues), true) @@ -598,6 +608,7 @@ final class SetupTwoStepVerificationControllerNode: ViewControllerTracingNode { text = strongSelf.presentationData.strings.TwoStepAuth_EmailInvalid case .invalidCode: text = strongSelf.presentationData.strings.Login_InvalidCodeError + strongSelf.contentNode?.dataEntryError() case .expired: text = strongSelf.presentationData.strings.TwoStepAuth_EmailCodeExpired case .flood: @@ -619,7 +630,7 @@ final class SetupTwoStepVerificationControllerNode: ViewControllerTracingNode { switch confirmState { case let .create(password, _, _): strongSelf.stateUpdated(.passwordSet(password: password, hasRecoveryEmail: true, hasSecureValues: false), true) - case let .add(password, hasSecureValues, email): + case let .add(password, _, hasSecureValues, email): strongSelf.stateUpdated(.passwordSet(password: password, hasRecoveryEmail: !email.isEmpty, hasSecureValues: hasSecureValues), true) case let .confirm(password, hasSecureValues, _, _): strongSelf.stateUpdated(.passwordSet(password: password, hasRecoveryEmail: true, hasSecureValues: hasSecureValues), true) diff --git a/TelegramUI/TwoStepVerificationUnlockController.swift b/TelegramUI/TwoStepVerificationUnlockController.swift index 4ef91fe5ee..c31a442bda 100644 --- a/TelegramUI/TwoStepVerificationUnlockController.swift +++ b/TelegramUI/TwoStepVerificationUnlockController.swift @@ -471,6 +471,9 @@ func twoStepVerificationUnlockSettingsController(account: Account, mode: TwoStep dataPromise.set(.single(TwoStepVerificationUnlockSettingsControllerData.access(configuration: .notSet(pendingEmail: nil)))) controller?.view.endEditing(true) controller?.dismiss() + + let presentationData = account.telegramApplicationContext.currentPresentationData.with { $0 } + presentControllerImpl?(OverlayStatusController(theme: presentationData.theme, strings: presentationData.strings, type: .genericSuccess(presentationData.strings.TwoStepAuth_DisableSuccess)), nil) } presentControllerImpl?(controller, ViewControllerPresentationArguments(presentationAnimation: .modalSheet)) }, error: { _ in