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>
62 lines
1.7 KiB
Swift
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 }
|
|
}
|
|
}
|