Skip to content

Commit 979ad60

Browse files
committed
IntegerView
1 parent 1109806 commit 979ad60

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/config/IntegerView.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import SwiftUI
2+
3+
let numberFormatter: NumberFormatter = {
4+
let formatter = NumberFormatter()
5+
formatter.numberStyle = .decimal
6+
formatter.allowsFloats = false
7+
formatter.usesGroupingSeparator = false
8+
return formatter
9+
}()
10+
11+
struct IntegerView: View {
12+
let description: String
13+
let minValue: Int?
14+
let maxValue: Int?
15+
@ObservedObject private var viewModel: OptionViewModel<Int>
16+
17+
init(data: [String: Any], onUpdate: @escaping (String) -> Void) {
18+
description = data["Description"] as! String
19+
minValue = Int(data["IntMin"] as? String ?? "")
20+
maxValue = Int(data["IntMax"] as? String ?? "")
21+
viewModel = OptionViewModel(
22+
value: Int(data["Value"] as! String) ?? 0,
23+
defaultValue: Int(data["DefaultValue"] as! String) ?? 0,
24+
onUpdate: { value in
25+
onUpdate(String(value))
26+
}
27+
)
28+
}
29+
30+
var body: some View {
31+
HStack {
32+
Text(description)
33+
TextField("", value: $viewModel.value, formatter: numberFormatter).resettable(
34+
viewModel
35+
).multilineTextAlignment(.trailing)
36+
if let minValue = minValue, let maxValue = maxValue {
37+
Stepper(
38+
value: $viewModel.value,
39+
in: minValue...maxValue,
40+
step: 1
41+
) {}
42+
} else {
43+
Stepper {
44+
} onIncrement: {
45+
viewModel.value += 1
46+
} onDecrement: {
47+
viewModel.value -= 1
48+
}
49+
}
50+
}
51+
}
52+
}

src/config/option.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func toOptionView(_ data: [String: Any], onUpdate: @escaping (Encodable) -> Void
3838
return BooleanView(data: data, onUpdate: onUpdate)
3939
case "Enum":
4040
return EnumView(data: data, onUpdate: onUpdate)
41+
case "Integer":
42+
return IntegerView(data: data, onUpdate: onUpdate)
4143
default:
4244
return UnknownView()
4345
}

0 commit comments

Comments
 (0)