-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitView.swift
195 lines (168 loc) · 6.25 KB
/
SplitView.swift
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
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
//
// SplitView.swift
// Ciphers
//
// Created by Arjun B on 15/04/23.
//
import SwiftUI
struct SplitView<LeadingView: View, TrailingView: View>: View {
@EnvironmentObject var state: AppState
let page: AppPage
let disabled: Bool
var leading: () -> LeadingView
var trailing: () -> TrailingView
@State private var aboutSheetShown: Bool = false
@State private var answer: String = ""
private var actionName: String {
if page == .four {
return "Play Again"
} else if page == .zero {
return "Start"
}
return "Next"
}
private var actionSymbol: String {
if page == .four {
return "arrow.clockwise"
}
return "arrow.right"
}
private var pageName: String {
switch page {
case .zero:
return "Introduction"
case .one:
return "Pigpen Cipher"
case .two:
return "Caesar Cipher"
case .three:
return "Grille Cipher"
case .four:
return "The End"
}
}
private var pageNumber: Int {
page.rawValue + 1
}
private func action() {
if page == .four {
state.reset()
} else {
state.nextPage()
}
}
var body: some View {
HStack(spacing: 16) {
VStack(alignment: .leading, spacing: 0) {
HStack(spacing: 0) {
HStack(spacing: 0) {
Text(pageNumber, format: .number)
.font(.body.weight(.bold).monospacedDigit())
.foregroundColor(Color.accentColor)
.padding(.horizontal, 2)
Rectangle()
.fill(Color.accentColor.opacity(0.2))
.frame(width: 2)
.padding(.horizontal)
Text(pageName)
.font(.body.weight(.semibold))
}
.accessibilityElement(children: .combine)
.accessibilityLabel("Section \(pageNumber), \(pageName)")
Spacer()
Menu {
Button(action: state.reset) {
Label("Restart", systemImage: "arrow.clockwise")
}
Button {
aboutSheetShown = true
} label: {
Label("About Ciphers", systemImage: "info.circle")
}
} label: {
Image(systemName: "ellipsis.circle")
.font(.title3.weight(.semibold))
.symbolRenderingMode(.hierarchical)
}
.padding(.vertical)
}
.padding(.horizontal)
.frame(maxWidth: .infinity, alignment: .leading)
.fixedSize(horizontal: false, vertical: true)
.background(Color.accentColor.opacity(0.15))
.cornerRadius(8)
.accessibilityAddTraits(.isHeader)
ScrollView(showsIndicators: false) {
leading()
.padding(.top, 12)
}
}
.frame(maxWidth: .infinity)
VStack {
ZStack {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.strokeBorder(Color.accentColor.opacity(0.2), lineWidth: 1)
trailing()
.padding()
}
if page != .three {
Button(action: action) {
HStack {
Text(actionName)
Spacer()
Image(systemName: actionSymbol)
}
.padding(8)
.font(.body.bold())
.frame(maxWidth: .infinity)
}
.disabled(disabled)
.buttonStyle(.borderedProminent)
.tint(Color.accentColor)
.accessibilityAddTraits(.isHeader)
} else {
HStack {
TextField("Enter the key here...", text: $answer)
.textInputAutocapitalization(.characters)
.disableAutocorrection(true)
.font(.body.monospaced())
.padding()
.background {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.strokeBorder(.thinMaterial, lineWidth: 1)
}
Button(action: state.nextPage) {
HStack {
Text("Finish")
Image(systemName: actionSymbol)
}
.font(.body.bold())
.padding(8)
}
.disabled(answer.uppercased() != state.solution)
.buttonStyle(.borderedProminent)
.tint(Color.accentColor)
}
.accessibilityAddTraits(.isHeader)
}
}
.frame(maxWidth: .infinity)
}
.padding()
.sheet(isPresented: $aboutSheetShown) {
AppInfo()
}
}
}
struct SplitView_Previews: PreviewProvider {
static var previews: some View {
SplitView(page: .zero, disabled: false) {
Text("Left")
} trailing: {
VStack {
Text("Right")
}
}
.environmentObject(AppState())
}
}