-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumberPad.swift
More file actions
168 lines (154 loc) · 5.27 KB
/
Copy pathNumberPad.swift
File metadata and controls
168 lines (154 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import SwiftUI
enum NumberPadType {
case simple
case integer
case decimal
}
struct NumberPad: View {
let type: NumberPadType
let decimalSeparator: String
let errorKey: String?
let onDeleteLongPress: (() -> Void)?
let onPress: (String) -> Void
static var contentHeight: CGFloat {
buttonHeight * 4
}
private static var buttonHeight: CGFloat {
UIScreen.main.isSmall ? 65 : 44 + 34
}
init(
type: NumberPadType = .simple,
decimalSeparator: String = ".",
errorKey: String? = nil,
onDeleteLongPress: (() -> Void)? = nil,
onPress: @escaping (String) -> Void
) {
self.type = type
self.decimalSeparator = decimalSeparator
self.errorKey = errorKey
self.onDeleteLongPress = onDeleteLongPress
self.onPress = onPress
}
private let gridItems = Array(repeating: GridItem(.flexible(), spacing: 0), count: 3)
private let numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
private var buttonHeight: CGFloat {
Self.buttonHeight
}
var body: some View {
VStack(spacing: 0) {
// Top 3 rows (1-9)
LazyVGrid(columns: gridItems, spacing: 0) {
ForEach(numbers, id: \.self) { number in
NumberPadButton(
text: number,
height: buttonHeight,
hasError: errorKey == number,
testID: "N\(number)"
) {
Haptics.play(.buttonTap)
onPress(number)
}
}
}
// Bottom row with type-specific left button, 0, and delete
HStack(spacing: 0) {
// Left button based on type
Group {
switch type {
case .simple:
Rectangle()
.fill(Color.clear)
.frame(height: buttonHeight)
case .integer:
NumberPadButton(
text: "000",
height: buttonHeight,
hasError: errorKey == "000",
testID: "N000"
) {
Haptics.play(.buttonTap)
onPress("000")
}
case .decimal:
NumberPadButton(
text: decimalSeparator,
height: buttonHeight,
hasError: errorKey == ".",
testID: "NDecimal"
) {
Haptics.play(.buttonTap)
onPress(".")
}
}
}
.frame(maxWidth: .infinity)
// Zero button
NumberPadButton(
text: "0",
height: buttonHeight,
hasError: errorKey == "0",
testID: "N0"
) {
Haptics.play(.buttonTap)
onPress("0")
}
.frame(maxWidth: .infinity)
// Delete button
Button(action: {
Haptics.play(.buttonTap)
onPress("delete")
}) {
Image(systemName: "delete.left")
.font(.title2)
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.frame(height: buttonHeight)
.contentShape(Rectangle())
}
.buttonStyle(NumberPadButtonStyle())
.accessibilityIdentifier("NRemove")
.frame(maxWidth: .infinity)
.simultaneousGesture(
LongPressGesture(minimumDuration: 0.45).onEnded { _ in
guard let onDeleteLongPress else { return }
Haptics.play(.buttonTap)
onDeleteLongPress()
}
)
}
}
}
}
private struct NumberPadButton: View {
let text: String
let height: CGFloat
let hasError: Bool
var testID: String?
let action: () -> Void
var body: some View {
Button(action: action) {
Text(text)
.font(.custom(Fonts.medium, size: 24))
.foregroundColor(hasError ? .redAccent : .white)
.kerning(-0.1)
.frame(maxWidth: .infinity)
.frame(height: height)
.contentShape(Rectangle())
}
.accessibilityIdentifier(testID ?? text)
.buttonStyle(NumberPadButtonStyle())
}
}
private struct NumberPadButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.background(configuration.isPressed ? Color.white.opacity(0.15) : Color.clear)
}
}
#Preview {
NumberPad(type: .integer, errorKey: nil) { key in
print("Pressed: \(key)")
}
.frame(height: 310)
.background(Color.black)
}