-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
SetAppPasscodeView.swift
68 lines (63 loc) 路 2.23 KB
/
SetAppPasscodeView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// SetAppPaswordView.swift
// SimpleX (iOS)
//
// Created by Evgeny on 10/04/2023.
// Copyright 漏 2023 SimpleX Chat. All rights reserved.
//
import SwiftUI
import SimpleXChat
struct SetAppPasscodeView: View {
var passcodeKeychain: KeyChainItem = kcAppPassword
var title: LocalizedStringKey = "New Passcode"
var reason: String?
var submit: () -> Void
var cancel: () -> Void
@Environment(\.dismiss) var dismiss: DismissAction
@State private var showKeychainError = false
@State private var passcode = ""
@State private var enteredPassword = ""
@State private var confirming = false
var body: some View {
ZStack {
if confirming {
setPasswordView(
title: "Confirm Passcode",
submitLabel: "Confirm",
submitEnabled: { pwd in pwd == enteredPassword }
) {
if passcode == enteredPassword {
if passcodeKeychain.set(passcode) {
enteredPassword = ""
passcode = ""
dismiss()
submit()
} else {
showKeychainError = true
}
}
}
} else {
setPasswordView(title: title, submitLabel: "Save") {
enteredPassword = passcode
passcode = ""
confirming = true
}
}
}
.alert(isPresented: $showKeychainError) {
mkAlert(title: "KeyChain error", message: "Error saving passcode")
}
}
private func setPasswordView(title: LocalizedStringKey, submitLabel: LocalizedStringKey, submitEnabled: (((String) -> Bool))? = nil, submit: @escaping () -> Void) -> some View {
PasscodeView(passcode: $passcode, title: title, reason: reason, submitLabel: submitLabel, submitEnabled: submitEnabled, submit: submit) {
dismiss()
cancel()
}
}
}
struct SetAppPasscodeView_Previews: PreviewProvider {
static var previews: some View {
SetAppPasscodeView(submit: {}, cancel: {})
}
}