Files
simplex-chat/apps/ios/Shared/Views/LocalAuth/SetAppPasscodeView.swift
Stanislav Dmitrenko 99a9fb2e1f ios: self destruct improvements (#3640)
* ios: self destruct improvements

* test

* adapted to stopped chat

* wait until ctrl initialization finishes

* Revert "test"

This reverts commit 7c199293cc.

* refactor

* simplify,fix

* refactor2

* refactor3

* comment

* fix

* fix

* comment

Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>

* flip and rename flag

---------

Co-authored-by: Avently <avently@local>
Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
2024-01-09 21:01:41 +00:00

73 lines
2.6 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 passcodeKeychain: KeyChainItem = kcAppPassword
var prohibitedPasscodeKeychain: KeyChainItem = kcSelfDestructPassword
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",
// Do not allow to set app passcode == selfDestruct passcode
submitEnabled: { pwd in pwd != prohibitedPasscodeKeychain.get() }) {
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, buttonsEnabled: Binding.constant(true), submit: submit) {
dismiss()
cancel()
}
}
}
struct SetAppPasscodeView_Previews: PreviewProvider {
static var previews: some View {
SetAppPasscodeView(submit: {}, cancel: {})
}
}