diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditDuressPasscodeViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditDuressPasscodeViewModel.swift index 46aa676d85..ccf9ad81fa 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditDuressPasscodeViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditDuressPasscodeViewModel.swift @@ -13,6 +13,10 @@ class EditDuressPasscodeViewModel: SetPasscodeViewModel { "edit_duress_passcode.confirm_new_passcode".localized } + override func isCurrent(passcode: String) -> Bool { + passcodeManager.isValid(duressPasscode: passcode) + } + override func onEnter(passcode: String) { do { try passcodeManager.set(duressPasscode: passcode) diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditPasscodeViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditPasscodeViewModel.swift index 574d55e77a..f767915595 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditPasscodeViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/EditPasscodeViewModel.swift @@ -13,6 +13,10 @@ class EditPasscodeViewModel: SetPasscodeViewModel { "edit_passcode.confirm_new_passcode".localized } + override func isCurrent(passcode: String) -> Bool { + passcodeManager.isValid(passcode: passcode) + } + override func onEnter(passcode: String) { do { try passcodeManager.set(passcode: passcode) diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/SetPasscodeViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/SetPasscodeViewModel.swift index 45b85b8ed9..b6ae7e24ca 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/SetPasscodeViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Manage/SetPasscodeViewModel.swift @@ -8,10 +8,10 @@ class SetPasscodeViewModel: ObservableObject { @Published var errorText: String = "" @Published var passcode: String = "" { didSet { + let passcode = passcode if passcode.count == passcodeLength { - Task { - try await Task.sleep(nanoseconds: 200_000_000) - await handlePasscodeChanged() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak self] in + self?.handleEntered(passcode: passcode) } } else if passcode.count != 0 { errorText = "" @@ -35,29 +35,29 @@ class SetPasscodeViewModel: ObservableObject { var title: String { "" } var passcodeDescription: String { "" } var confirmDescription: String { "" } + func isCurrent(passcode: String) -> Bool { false } func onEnter(passcode _: String) {} func onCancel() {} - @MainActor - private func handlePasscodeChanged() { + private func handleEntered(passcode: String) { if let enteredPasscode { if enteredPasscode == passcode { onEnter(passcode: passcode) } else { self.enteredPasscode = nil - passcode = "" + self.passcode = "" syncDescription() errorText = "set_passcode.invalid_confirmation".localized } - } else if passcodeManager.has(passcode: passcode) { - passcode = "" + } else if passcodeManager.has(passcode: passcode) && !isCurrent(passcode: passcode) { + self.passcode = "" errorText = "set_passcode.already_used".localized shakeTrigger += 1 UINotificationFeedbackGenerator().notificationOccurred(.error) } else { enteredPasscode = passcode - passcode = "" + self.passcode = "" syncDescription() } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift index 3370b8ec76..12137ca989 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift @@ -10,10 +10,10 @@ class BaseUnlockViewModel: ObservableObject { @Published var errorText: String = "" @Published var passcode: String = "" { didSet { + let passcode = passcode if passcode.count == passcodeLength { - Task { - try await Task.sleep(nanoseconds: 200_000_000) - await handlePasscodeChanged() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak self] in + self?.handleEntered(passcode: passcode) } } } @@ -82,13 +82,12 @@ class BaseUnlockViewModel: ObservableObject { func onEnterValid(passcode _: String) {} func onBiometryUnlock() -> Bool { false } - @MainActor - private func handlePasscodeChanged() { + private func handleEntered(passcode: String) { if isValid(passcode: passcode) { onEnterValid(passcode: passcode) lockoutManager.didUnlock() } else { - passcode = "" + self.passcode = "" lockoutManager.didFailUnlock() shakeTrigger += 1