Meshtastic-Apple/Meshtastic/Views/Helpers/SecureInput.swift

80 lines
2.1 KiB
Swift
Raw Permalink Normal View History

//
// SecureInput.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 8/12/24.
//
import SwiftUI
struct SecureInput: View {
private var idiom: UIUserInterfaceIdiom { UIDevice.current.userInterfaceIdiom }
@Binding private var text: String
2024-08-19 21:50:28 -07:00
@Binding private var isValid: Bool
private var title: String
2025-06-16 17:24:54 -04:00
// Local state to store the value of iSSecure, or optionally a binding
private var isSecureBinding: Binding<Bool>?
@State private var isSecureLocal: Bool = true
private var isSecure: Binding<Bool> {
// Use the binding if we have one, otherwise fallback to the local state variable
isSecureBinding ?? $isSecureLocal
}
init(_ title: String, text: Binding<String>, isValid: Binding<Bool>, isSecure: Binding<Bool>? = nil) {
self.title = title
self._text = text
2024-08-19 21:50:28 -07:00
self._isValid = isValid
2025-06-16 17:24:54 -04:00
self.isSecureBinding = isSecure
}
var body: some View {
ZStack(alignment: .trailing) {
Group {
2025-06-16 17:24:54 -04:00
if isSecure.wrappedValue {
SecureField(title, text: $text)
.font(idiom == .phone ? .caption : .callout)
.allowsTightening(true)
.monospaced()
.keyboardType(.alphabet)
.foregroundStyle(.tertiary)
.disableAutocorrection(true)
} else {
TextField(title, text: $text, axis: .vertical)
.font(idiom == .phone ? .caption : .callout)
.allowsTightening(true)
.monospaced()
.keyboardType(.alphabet)
.foregroundStyle(.tertiary)
.disableAutocorrection(true)
.textSelection(.enabled)
.lineLimit(...3)
2024-08-19 21:50:28 -07:00
.background(
RoundedRectangle(cornerRadius: 10.0)
.stroke(isValid ? Color.clear : Color.red, lineWidth: 2.0)
)
}
}.padding(.trailing, 36)
if !text.isEmpty {
Button(action: {
2025-06-16 17:24:54 -04:00
isSecure.wrappedValue.toggle()
}) {
2025-06-16 17:24:54 -04:00
Image(systemName: self.isSecure.wrappedValue ? "eye.slash" : "eye")
.accentColor(.secondary)
}.buttonStyle(BorderlessButtonStyle())
}
}
}
}
Add missing SwiftUI #Preview blocks across 65 views (#1649) * Add SwiftUI previews for simple helper views Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add previews for action buttons, ChannelForm, MetricsColumnDetail, and DeviceOnboarding Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add previews for config views, log views, AppLog, Firmware, AppData, and UserConfig Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add preview for PositionConfig Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Fix formatting bugs in #Preview blocks: restore missing .environmentObject/.environment modifiers and add proper tab indentation Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/7eeb7a54-7928-466f-8e39-b00d0012a09d Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Linting fixes --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> Co-authored-by: Garth Vander Houwen <garthvh@yahoo.com>
2026-04-04 18:02:32 -07:00
#Preview {
List {
SecureInput("Password", text: .constant("s3cretP@ss"), isValid: .constant(true))
SecureInput("Invalid Key", text: .constant("short"), isValid: .constant(false))
SecureInput("Empty", text: .constant(""), isValid: .constant(true))
}
}