質問編集履歴
1
ご指摘通り<code>にコードを書きました。よろしくお願い致します。
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,111 +26,113 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
-
|
29
|
+
```SwiftUI
|
30
|
+
|
31
|
+
|
30
32
|
|
31
33
|
public struct Lock: View {
|
32
34
|
|
33
35
|
|
34
36
|
|
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
|
-
|
37
|
+
var maxDigits: Int = 4
|
38
|
+
|
39
|
+
var label = "Enter One Time Password"
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
@State var pin: String = ""
|
44
|
+
|
45
|
+
@State var showPin = false
|
46
|
+
|
47
|
+
@State var isDisabled = false
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
var handler: (String, (Bool) -> Void) -> Void
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public var body: some View {
|
56
|
+
|
57
|
+
VStack(spacing: 30) {
|
58
|
+
|
59
|
+
Text(label).font(.title)
|
60
|
+
|
61
|
+
ZStack {
|
62
|
+
|
63
|
+
pinDots
|
64
|
+
|
65
|
+
backgroundField
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
showPinStack
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
private var pinDots: some View {
|
80
|
+
|
81
|
+
HStack {
|
82
|
+
|
83
|
+
Spacer()
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
//エラー箇所
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
ForEach(0..<maxDigits) { index in
|
92
|
+
|
93
|
+
Image(systemName: self.getImageName(at: index))
|
94
|
+
|
95
|
+
.font(.system(size:30, weight:30, design: .default))
|
96
|
+
|
97
|
+
Spacer()
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
private var backgroundField: some View {
|
108
|
+
|
109
|
+
let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
|
110
|
+
|
111
|
+
self.pin = newValue
|
112
|
+
|
113
|
+
self.submitPin()
|
114
|
+
|
115
|
+
})
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
return TextField("", text: boundPin, onCommit: submitPin)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
// Introspect library can used to make the textField become first resonder on appearing
|
124
|
+
|
125
|
+
// if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
.accentColor(.clear)
|
130
|
+
|
131
|
+
.foregroundColor(.clear)
|
132
|
+
|
133
|
+
.keyboardType(.numberPad)
|
134
|
+
|
135
|
+
.disabled(isDisabled)
|
134
136
|
|
135
137
|
|
136
138
|
|
@@ -148,129 +150,129 @@
|
|
148
150
|
|
149
151
|
// }
|
150
152
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
private var showPinStack: some View {
|
158
|
+
|
159
|
+
HStack {
|
160
|
+
|
161
|
+
Spacer()
|
162
|
+
|
163
|
+
if !pin.isEmpty {
|
164
|
+
|
165
|
+
showPinButton
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
.frame(height:30)
|
172
|
+
|
173
|
+
.padding([.trailing])
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
private var showPinButton: some View {
|
180
|
+
|
181
|
+
Button(action: {
|
182
|
+
|
183
|
+
self.showPin.toggle()
|
184
|
+
|
185
|
+
}, label: {
|
186
|
+
|
187
|
+
self.showPin ?
|
188
|
+
|
189
|
+
Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
|
190
|
+
|
191
|
+
Image(systemName: "eye.fill").foregroundColor(.primary)
|
192
|
+
|
193
|
+
})
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
private func submitPin() {
|
200
|
+
|
201
|
+
guard !pin.isEmpty else {
|
202
|
+
|
203
|
+
showPin = false
|
204
|
+
|
205
|
+
return
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
if pin.count == maxDigits {
|
212
|
+
|
213
|
+
isDisabled = true
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
handler(pin) { isSuccess in
|
218
|
+
|
219
|
+
if isSuccess {
|
220
|
+
|
221
|
+
print("pin matched, go to next page, no action to perfrom here")
|
222
|
+
|
223
|
+
} else {
|
224
|
+
|
225
|
+
pin = ""
|
226
|
+
|
227
|
+
isDisabled = false
|
228
|
+
|
229
|
+
print("this has to called after showing toast why is the failure")
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// this code is never reached under normal circumstances. If the user pastes a text with count higher than the
|
240
|
+
|
241
|
+
// max digits, we remove the additional characters and make a recursive call.
|
242
|
+
|
243
|
+
if pin.count > maxDigits {
|
244
|
+
|
245
|
+
pin = String(pin.prefix(maxDigits))
|
246
|
+
|
247
|
+
submitPin()
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
private func getImageName(at index: Int) -> String {
|
256
|
+
|
257
|
+
if index >= self.pin.count {
|
258
|
+
|
259
|
+
return "circle"
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
if self.showPin {
|
266
|
+
|
267
|
+
return self.pin.digits[index].numberString + ".circle"
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
return "circle.fill"
|
274
|
+
|
275
|
+
}
|
274
276
|
|
275
277
|
}
|
276
278
|
|
@@ -280,27 +282,27 @@
|
|
280
282
|
|
281
283
|
|
282
284
|
|
283
|
-
|
285
|
+
var digits: [Int] {
|
284
|
-
|
286
|
+
|
285
|
-
|
287
|
+
var result = [Int]()
|
286
|
-
|
287
|
-
|
288
|
-
|
288
|
+
|
289
|
+
|
290
|
+
|
289
|
-
|
291
|
+
for char in self {
|
290
|
-
|
292
|
+
|
291
|
-
|
293
|
+
if let number = Int(String(char)) {
|
292
|
-
|
294
|
+
|
293
|
-
|
295
|
+
result.append(number)
|
294
|
-
|
296
|
+
|
295
|
-
|
297
|
+
}
|
296
|
-
|
298
|
+
|
297
|
-
|
299
|
+
}
|
298
|
-
|
299
|
-
|
300
|
-
|
300
|
+
|
301
|
+
|
302
|
+
|
301
|
-
|
303
|
+
return result
|
302
|
-
|
304
|
+
|
303
|
-
|
305
|
+
}
|
304
306
|
|
305
307
|
|
306
308
|
|
@@ -312,16 +314,18 @@
|
|
312
314
|
|
313
315
|
|
314
316
|
|
315
|
-
|
317
|
+
var numberString: String {
|
316
|
-
|
317
|
-
|
318
|
-
|
318
|
+
|
319
|
+
|
320
|
+
|
319
|
-
|
321
|
+
guard self < 10 else { return "0" }
|
320
|
-
|
321
|
-
|
322
|
-
|
322
|
+
|
323
|
+
|
324
|
+
|
323
|
-
|
325
|
+
return String(self)
|
324
|
-
|
326
|
+
|
325
|
-
|
327
|
+
}
|
328
|
+
|
326
|
-
|
329
|
+
}
|
330
|
+
|
327
|
-
|
331
|
+
```
|