DayByDay/Parent/ParentGateView.swift
122 lines · 4092 bytes
3 symbols in this file
1//
2// ParentGateView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// Press-and-hold, then a sum. Calls `onPass` when both are done.
9struct ParentGateView: View {
10 let onPass: () -> Void
11 let onCancel: () -> Void
12
13 @State private var holdProgress: CGFloat = 0
14 @State private var held = false
15 @State private var challenge = ParentGateView.newChallenge()
16 @State private var input = ""
17 @FocusState private var fieldFocused: Bool
18
19 init(onPass: @escaping () -> Void, onCancel: @escaping () -> Void) {
20 self.onPass = onPass
21 self.onCancel = onCancel
22 }
23
24 var body: some View {
25 CenteredScrollView {
26 VStack(spacing: 32) {
27 Text("For Grown-Ups")
28 .font(.system(.largeTitle, design: .rounded, weight: .bold))
29
30 if held {
31 sumStep
32 } else {
33 holdStep
34 }
35
36 Button("Cancel", action: onCancel)
37 .font(.system(.body, design: .rounded, weight: .medium))
38 .foregroundStyle(.primary)
39 }
40 .padding(40)
41 .frame(maxWidth: 520)
42 .multilineTextAlignment(.center)
43 }
44 }
45
46 private var holdStep: some View {
47 VStack(spacing: 16) {
48 ZStack {
49 Circle()
50 .stroke(Color(.systemGray5), lineWidth: 12)
51 Circle()
52 .trim(from: 0, to: holdProgress)
53 .stroke(Color.accentColor, style: StrokeStyle(lineWidth: 12, lineCap: .round))
54 .rotationEffect(.degrees(-90))
55 Image(systemName: "hand.tap.fill")
56 .font(.system(size: 44))
57 .foregroundStyle(Color.accentColor)
58 }
59 .frame(width: 140, height: 140)
60 .contentShape(Circle())
61 .onLongPressGesture(minimumDuration: ParentGateChallenge.holdDuration) {
62 held = true
63 fieldFocused = true
64 } onPressingChanged: { pressing in
65 if pressing {
66 withAnimation(.linear(duration: ParentGateChallenge.holdDuration)) { holdProgress = 1 }
67 } else if !held {
68 withAnimation(.easeOut(duration: 0.2)) { holdProgress = 0 }
69 }
70 }
71 .accessibilityLabel("Hold for grown-ups")
72 .accessibilityAddTraits(.isButton)
73 // Assistive technologies skip the hold; the sum still keeps children out.
74 .accessibilityAction {
75 held = true
76 fieldFocused = true
77 }
78
79 Text("Press and hold")
80 .font(.system(.body, design: .rounded))
81 .fixedSize(horizontal: false, vertical: true)
82 }
83 }
84
85 private var sumStep: some View {
86 VStack(spacing: 20) {
87 Text("What is \(challenge.a) + \(challenge.b)?")
88 .font(.system(.title, design: .rounded, weight: .semibold))
89 TextField("Answer", text: $input)
90 .keyboardType(.numberPad)
91 .focused($fieldFocused)
92 .multilineTextAlignment(.center)
93 .font(.system(.title, design: .rounded, weight: .bold))
94 .padding(12)
95 .frame(width: 160)
96 .background(Color(.systemGray6), in: RoundedRectangle(cornerRadius: 12))
97 Button("Continue", action: submit)
98 .buttonStyle(.borderedProminent)
99 .font(.system(.title3, design: .rounded, weight: .semibold))
100 }
101 }
102
103 private func submit() {
104 if challenge.accepts(input) {
105 onPass()
106 } else {
107 challenge = Self.newChallenge()
108 input = ""
109 held = false
110 holdProgress = 0
111 }
112 }
113
114 private static func newChallenge() -> ParentGateChallenge {
115 var rng = SystemRandomNumberGenerator()
116 return .random(using: &rng)
117 }
118}
119
120#Preview {
121 ParentGateView(onPass: {}, onCancel: {})
122}