2023-08-09 16:29:18 +02:00
|
|
|
import SwiftUI
|
|
|
|
|
2024-05-25 16:08:51 +02:00
|
|
|
struct SettingsNumberInputView<Value>: View where Value: FixedWidthInteger {
|
2023-08-09 16:29:18 +02:00
|
|
|
|
|
|
|
let title: String
|
|
|
|
|
|
|
|
@Binding
|
2024-05-25 16:08:51 +02:00
|
|
|
var value: Value
|
2023-08-09 16:29:18 +02:00
|
|
|
|
|
|
|
@State
|
|
|
|
private var text: String = ""
|
|
|
|
|
|
|
|
let footnote: String
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
TextField(title, text: $text)
|
|
|
|
.onSubmit {
|
2024-05-25 16:08:51 +02:00
|
|
|
guard let newValue = Value(text) else {
|
2023-08-09 16:29:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
value = newValue
|
|
|
|
}
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
Text(footnote)
|
|
|
|
.font(.footnote)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.navigationTitle(title)
|
|
|
|
.navigationBarBackButtonHidden(false)
|
|
|
|
.onAppear {
|
|
|
|
text = "\(value)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SettingsNumberInputView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
SettingsNumberInputView(
|
|
|
|
title: "Title",
|
|
|
|
value: .constant(0),
|
|
|
|
footnote: "Some more text explaining the purpose of the text field.")
|
|
|
|
}
|
|
|
|
}
|