Skip to content

Commit

Permalink
Reload app interface when passcode level changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Sep 29, 2023
1 parent 1d8ada1 commit 6631236
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseUnlockViewModel: ObservableObject {
}
@Published var shakeTrigger: Int = 0

let finishSubject = PassthroughSubject<Void, Never>()
let finishSubject = PassthroughSubject<Bool, Never>()
let unlockWithBiometrySubject = PassthroughSubject<Void, Never>()

let passcodeManager: PasscodeManager
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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,
lockManager: App.shared.lockManager,
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import SwiftUI

struct UnlockView: View {
@ObservedObject var viewModel: BaseUnlockViewModel
let autoDismiss: Bool

@Environment(\.presentationMode) private var presentationMode

Expand All @@ -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()
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 6631236

Please sign in to comment.