diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Managers/PasscodeManager.swift b/UnstoppableWallet/UnstoppableWallet/Core/Managers/PasscodeManager.swift index dacbd33672..7dffaf4da5 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Managers/PasscodeManager.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Managers/PasscodeManager.swift @@ -63,26 +63,36 @@ extension PasscodeManager { passcodes.contains(passcode) } - func setLastPasscode() { + func setLastPasscode() -> Bool { guard !passcodes.isEmpty else { - return + return false } - currentPasscodeLevel = passcodes.count - 1 + let level = passcodes.count - 1 + + guard currentPasscodeLevel != level else { + return false + } + + currentPasscodeLevel = level syncState() + + return true } - func set(currentPasscode: String) { + func set(currentPasscode: String) -> Bool { guard let level = passcodes.firstIndex(of: currentPasscode) else { - return + return false } guard currentPasscodeLevel != level else { - return + return false } currentPasscodeLevel = level syncState() + + return true } func set(passcode: String) throws { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Launch/LaunchModule.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Launch/LaunchModule.swift index dec28b9251..f6d1e28eee 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Launch/LaunchModule.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Launch/LaunchModule.swift @@ -14,10 +14,7 @@ class LaunchModule { case .passcodeNotSet: return NoPasscodeViewController(mode: .noPasscode) case .cannotCheckPasscode: return NoPasscodeViewController(mode: .cannotCheckPasscode) case .intro: return WelcomeScreenViewController() - case .unlock: return UnlockModule.appUnlockView { - UIApplication.shared.windows.first { $0.isKeyWindow }?.set(newRootController: MainModule.instance()) - } - .toViewController() + case .unlock: return UnlockModule.appUnlockView(appStart: true).toViewController() case .main: return MainModule.instance() } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/AppUnlockViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/AppUnlockViewModel.swift index 3d738fdb56..2612111c26 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/AppUnlockViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/AppUnlockViewModel.swift @@ -1,13 +1,11 @@ import Combine class AppUnlockViewModel: BaseUnlockViewModel { - private let autoDismiss: Bool - private let onUnlock: (() -> Void)? + private let appStart: Bool private let lockManager: LockManager - init(autoDismiss: Bool, onUnlock: (() -> Void)?, passcodeManager: PasscodeManager, biometryManager: BiometryManager, lockoutManager: LockoutManager, lockManager: LockManager, blurManager: BlurManager) { - self.autoDismiss = autoDismiss - self.onUnlock = onUnlock + init(appStart: Bool, passcodeManager: PasscodeManager, biometryManager: BiometryManager, lockoutManager: LockoutManager, lockManager: LockManager, blurManager: BlurManager) { + self.appStart = appStart self.lockManager = lockManager super.init(passcodeManager: passcodeManager, biometryManager: biometryManager, lockoutManager: lockoutManager, blurManager: blurManager, biometryAllowed: true) @@ -18,25 +16,19 @@ class AppUnlockViewModel: BaseUnlockViewModel { } override func onEnterValid(passcode: String) { - super.onEnterValid(passcode: passcode) - - passcodeManager.set(currentPasscode: passcode) - handleUnlock() + let levelChanged = passcodeManager.set(currentPasscode: passcode) + handleUnlock(levelChanged: levelChanged) } - override func onBiometryUnlock() { - super.onBiometryUnlock() + override func onBiometryUnlock() -> Bool { + let levelChanged = passcodeManager.setLastPasscode() + handleUnlock(levelChanged: levelChanged) - passcodeManager.setLastPasscode() - handleUnlock() + return !levelChanged } - private func handleUnlock() { + private func handleUnlock(levelChanged: Bool) { lockManager.onUnlock() - onUnlock?() - - if autoDismiss { - finishSubject.send() - } + finishSubject.send(appStart || levelChanged) } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift index 53f9246a63..3370b8ec76 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/BaseUnlockViewModel.swift @@ -29,7 +29,7 @@ class BaseUnlockViewModel: ObservableObject { } @Published var shakeTrigger: Int = 0 - let finishSubject = PassthroughSubject() + let finishSubject = PassthroughSubject() let unlockWithBiometrySubject = PassthroughSubject() let passcodeManager: PasscodeManager @@ -80,7 +80,7 @@ class BaseUnlockViewModel: ObservableObject { func isValid(passcode _: String) -> Bool { false } func onEnterValid(passcode _: String) {} - func onBiometryUnlock() {} + func onBiometryUnlock() -> Bool { false } @MainActor private func handlePasscodeChanged() { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockView.swift index 1d9b58dd31..58408e837a 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockView.swift @@ -6,7 +6,7 @@ struct ModuleUnlockView: View { @Environment(\.presentationMode) private var presentationMode var body: some View { - UnlockView(viewModel: viewModel, autoDismiss: true) + UnlockView(viewModel: viewModel) .navigationTitle("unlock.title".localized) .navigationBarTitleDisplayMode(.inline) .toolbar { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockViewModel.swift index 75a3aec03b..76af1a33dd 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/ModuleUnlockViewModel.swift @@ -15,15 +15,12 @@ class ModuleUnlockViewModel: BaseUnlockViewModel { } override func onEnterValid(passcode: String) { - super.onEnterValid(passcode: passcode) - onUnlock() - finishSubject.send() + finishSubject.send(false) } - override func onBiometryUnlock() { - super.onBiometryUnlock() - + override func onBiometryUnlock() -> Bool { onUnlock() + return true } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockModule.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockModule.swift index 838082fb47..514b96e690 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockModule.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockModule.swift @@ -1,10 +1,9 @@ import SwiftUI struct UnlockModule { - static func appUnlockView(autoDismiss: Bool = false, onUnlock: (() -> Void)? = nil) -> some View { + static func appUnlockView(appStart: Bool) -> some View { let viewModel = AppUnlockViewModel( - autoDismiss: autoDismiss, - onUnlock: onUnlock, + appStart: appStart, passcodeManager: App.shared.passcodeManager, biometryManager: App.shared.biometryManager, lockoutManager: App.shared.lockoutManager, @@ -12,7 +11,7 @@ struct UnlockModule { blurManager: App.shared.blurManager ) - return UnlockView(viewModel: viewModel, autoDismiss: autoDismiss) + return UnlockView(viewModel: viewModel) } static func moduleUnlockView(biometryAllowed: Bool = false, onUnlock: @escaping () -> Void) -> some View { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockView.swift index 90851e523d..1e21b28106 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Passcode/Unlock/UnlockView.swift @@ -3,7 +3,6 @@ import SwiftUI struct UnlockView: View { @ObservedObject var viewModel: BaseUnlockViewModel - let autoDismiss: Bool @Environment(\.presentationMode) private var presentationMode @@ -29,8 +28,12 @@ struct UnlockView: View { .onDisappear { viewModel.onDisappear() } - .onReceive(viewModel.finishSubject) { - presentationMode.wrappedValue.dismiss() + .onReceive(viewModel.finishSubject) { reloadApp in + if reloadApp { + UIApplication.shared.windows.first { $0.isKeyWindow }?.set(newRootController: MainModule.instance()) + } else { + presentationMode.wrappedValue.dismiss() + } } .onReceive(viewModel.unlockWithBiometrySubject) { unlockWithBiometry() @@ -42,9 +45,9 @@ struct UnlockView: View { localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "unlock.biometry_reason".localized) { success, _ in if success { DispatchQueue.main.async { - viewModel.onBiometryUnlock() + let shouldDismiss = viewModel.onBiometryUnlock() - if autoDismiss { + if shouldDismiss { presentationMode.wrappedValue.dismiss() } } diff --git a/UnstoppableWallet/UnstoppableWallet/UserInterface/LockDelegate.swift b/UnstoppableWallet/UnstoppableWallet/UserInterface/LockDelegate.swift index 7c3f267eb1..9eaf5b55ec 100644 --- a/UnstoppableWallet/UnstoppableWallet/UserInterface/LockDelegate.swift +++ b/UnstoppableWallet/UnstoppableWallet/UserInterface/LockDelegate.swift @@ -4,7 +4,7 @@ class LockDelegate { var viewController: UIViewController? func onLock() { - let module = UnlockModule.appUnlockView(autoDismiss: true).toViewController() + let module = UnlockModule.appUnlockView(appStart: false).toViewController() module.modalPresentationStyle = .fullScreen viewController?.visibleController.present(module, animated: false) }