Files
simplex-chat/apps/ios/Shared/Views/LocalAuth/SetAppPasscodeView.swift
Evgeny Poberezkin ec6cee1389 ios: digital password (instead of device auth) (#2169)
* ios: digital password (instead of device auth)

* set, ask, change password

* kind of working, sometimes

* ZSTack

* fix cancel

* update title

* fix password showing after settings dismissed

* disable button when 16 digits entered

* fixes

* layout on larger screens

* do not disable auth when switching to system if system auth failed, refactor

* fix enabling auth via the initial alert

* support landscape orientation
2023-04-12 11:22:55 +01:00

66 lines
2.1 KiB
Swift

//
// 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 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 kcAppPassword.set(passcode) {
enteredPassword = ""
passcode = ""
dismiss()
submit()
} else {
showKeychainError = true
}
}
}
} else {
setPasswordView(title: "New Passcode", 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, submitLabel: submitLabel, submitEnabled: submitEnabled, submit: submit) {
dismiss()
cancel()
}
}
}
struct SetAppPasscodeView_Previews: PreviewProvider {
static var previews: some View {
SetAppPasscodeView(submit: {}, cancel: {})
}
}