Ferrite-backup/Ferrite/Views/CommonViews/HybridSecureField.swift
kingbri ecdd0199f6 Tree: Cleanup access levels
Public should not be used in an app since it declares public to
additional modules. However, an app is one module. Some structs/
classes need to be left public to conform to CoreData's generation.

Signed-off-by: kingbri <bdashore3@proton.me>
2024-06-16 15:00:35 -05:00

62 lines
1.7 KiB
Swift

//
// HybridSecureField.swift
// Ferrite
//
// Created by Brian Dashore on 3/4/23.
//
import SwiftUI
struct HybridSecureField: View {
enum Field: Hashable {
case plain
case secure
}
@Binding var text: String
var onCommit: () -> Void = {}
@State private var showPassword = false
@FocusState private var focusedField: Field?
private var isFieldDisabled: Bool = false
init(text: Binding<String>, onCommit: (() -> Void)? = nil, showPassword: Bool = false) {
_text = text
if let onCommit {
self.onCommit = onCommit
}
self.showPassword = showPassword
}
var body: some View {
HStack {
Group {
if showPassword {
TextField("Password", text: $text, onCommit: onCommit)
.focused($focusedField, equals: .plain)
} else {
SecureField("Password", text: $text, onCommit: onCommit)
.focused($focusedField, equals: .secure)
}
}
.autocorrectionDisabled(true)
.autocapitalization(.none)
.disabledAppearance(isFieldDisabled)
Button {
showPassword.toggle()
focusedField = showPassword ? .plain : .secure
} label: {
Image(systemName: showPassword ? "eye.slash.fill" : "eye.fill")
.foregroundColor(.secondary)
}
.buttonStyle(.borderless)
}
}
}
extension HybridSecureField {
func fieldDisabled(_ isFieldDisabled: Bool) -> Self {
modifyViewProp { $0.isFieldDisabled = isFieldDisabled }
}
}