Skip to content

Commit

Permalink
Allow using the same password when editing it
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Oct 2, 2023
1 parent 2c05e57 commit b9c154d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9c154d

Please sign in to comment.