-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWouldYouRather.swift
223 lines (187 loc) Β· 8.61 KB
/
WouldYouRather.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
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
import SwiftUI
struct WouldYouRatherView: View {
@State private var selectedOption: String?
@State private var progress: Float = 0.00
@State private var isFirstQuestionAnswered = false
@State private var isSecondQuestionAnswered = false
@State private var isThirdQuestionAnswered = false
@State private var selectedOptions: [String] = []
var body: some View {
VStack {
VStack {
ZStack {
Color.darkGrayColor()
.edgesIgnoringSafeArea(.horizontal)
.frame(height: 150)
VStack {
VStack {
HStack {
Text("WOULD")
.font(.system(size: 30, weight: .bold))
.fontDesign(.monospaced)
Text("YOU")
.fontDesign(.monospaced)
.font(.system(size: 30, weight: .bold))
.foregroundColor(.white)
}
HStack {
Text("\u{1F448}")
.font(.system(size: 30))
Text("RATHER")
.font(.system(size: 30, weight: .bold))
.fontDesign(.monospaced)
Text("\u{1F449}")
.font(.system(size: 30))
}
}
Text("Select what makes the most sense to you")
.font(.caption2)
.foregroundColor(.white)
.padding(.top, 10)
.multilineTextAlignment(.center)
}
.padding()
}
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.darkGrayColor(), lineWidth: 1)
)
.padding()
if !isFirstQuestionAnswered {
createButton(text: "Never use a smartphone again π΅", isAnswered: isFirstQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Never use a smartphone again π΅")
self.isFirstQuestionAnswered = true
}
}
orDivider()
createButton(text: "Never use a laptop again π»", isAnswered: isFirstQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Never use a laptop again π»")
self.isFirstQuestionAnswered = true
}
}
} else if !isSecondQuestionAnswered {
createButton(text: "Have the power to teleport π", isAnswered: isSecondQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Have the power to teleport π")
self.isSecondQuestionAnswered = true
}
}
orDivider()
createButton(text: "Be able to time travel β³", isAnswered: isSecondQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Be able to time travel β³")
self.isSecondQuestionAnswered = true
}
}
} else if !isThirdQuestionAnswered {
createButton(text: "Always be 10 minutes late β°", isAnswered: isThirdQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Always be 10 minutes late β°")
self.isThirdQuestionAnswered = true
}
}
orDivider()
createButton(text: "Always be 10 minutes early β°", isAnswered: isThirdQuestionAnswered, selectedOption: $selectedOption) {
delayAction {
selectedOptionChanges(option: "Always be 10 minutes early β°")
self.isThirdQuestionAnswered = true
}
}
} else {
createButton(text: "Say everything twice as fast π£οΈ", isAnswered: true, selectedOption: $selectedOption) {
selectedOptionChanges(option: "Have to say everything on twice the speed π£οΈ")
}
orDivider()
createButton(text: "Say everything on half the speed π£οΈ", isAnswered: true, selectedOption: $selectedOption) {
selectedOptionChanges(option: "Have to say everything on half the speed π£οΈ")
}
}
ProgressBar(value: $progress, selectedOptions: $selectedOptions)
.frame(width: 200, height: 10)
.padding()
}
.padding()
.overlay(
RoundedRectangle(cornerSize: CGSize(width: 40, height: 40))
.stroke(Color.lightGrayColor(), lineWidth: 2)
.padding(8)
)
}
}
struct ProgressBar: View {
@Binding var value: Float
@Binding var selectedOptions: [String]
@State private var isShowingResultsModal = false
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .leading) {
Capsule()
.frame(width: geometry.size.width, height: geometry.size.height)
.opacity(0.3)
.foregroundColor(Color.darkPurpleColor())
withAnimation(){
Capsule()
.frame(width: min(CGFloat(self.value)*geometry.size.width, geometry.size.width), height: geometry.size.height)
.foregroundColor(Color.darkPurpleColor())
.onReceive([self.value].publisher.first()) { _ in
if self.value >= 1.0 {
self.isShowingResultsModal = true
}
}
}
}
.sheet(isPresented: $isShowingResultsModal) {
ResultsModal(selectedOptions: $selectedOptions, isShowingModal: $isShowingResultsModal)
}
}
}
}
private func createButton(text: String, isAnswered: Bool, selectedOption: Binding<String?>, action: @escaping () -> Void) -> some View {
Button(action: {
action()
if isAnswered {
selectedOption.wrappedValue = nil
}
}) {
Text(text)
.padding()
.padding(.horizontal)
.foregroundColor(selectedOption.wrappedValue == text ? .gray : .black)
.background(Color.lighterGrayColor())
.clipShape(Capsule())
}
}
private func selectedOptionChanges(option: String) {
selectedOption = option
progress += 0.25
selectedOptions.append(option)
}
func orDivider() -> some View {
HStack {
Rectangle()
.foregroundColor(.gray)
.frame(height: 1)
Text("OR")
.padding()
.bold()
.foregroundColor(.white)
.background(Color.darkPurpleColor())
.clipShape(Capsule())
Rectangle()
.foregroundColor(.gray)
.frame(height: 1)
}
}
private func delayAction(_ action: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
action()
}
}
}
struct WouldYouRatherView_Previews: PreviewProvider {
static var previews: some View {
WouldYouRatherView()
}
}