-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserPrompt.swift
183 lines (150 loc) · 5.31 KB
/
UserPrompt.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
//
// UserPrompt.swift
// RedCal
//
// Created by Carissa Farry Hilmi Az Zahra on 08/05/23.
//
import SwiftUI
struct Prompt {
let question: String
let answerInputType: AnswerInputType
let options: [String]?
}
struct Answer {
var promptID: Int
var answer: AnswerType
}
enum AnswerInputType {
case date
case number
}
protocol AnswerType {}
struct DateAnswer: AnswerType {
let date: Date
}
struct NumericAnswer: AnswerType {
let number: Int
}
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
struct UserPrompt: View {
@State private var currentCardIndex = 0
@State var prompts: [Prompt] = [
Prompt(
question: "When is your last period date?",
answerInputType: .date,
options: nil
),
Prompt(
question: "How much your usual period length?",
answerInputType: .number,
options: nil
),
]
@State var answers: [Answer] = []
var body: some View {
VStack {
TabView(selection: $currentCardIndex) {
ForEach((prompts.indices), id: \.self) { index in
PromptCard(prompts: $prompts, currentCardIndex: $currentCardIndex, answers: $answers)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
}
.frame(height: screenHeight * 0.7)
}
func slideCardLeft() {
withAnimation {
currentCardIndex -= 1
}
}
func slideCardRight() {
withAnimation {
currentCardIndex += 1
}
}
}
struct PromptCard: View {
@Binding var prompts: [Prompt]
@Binding var currentCardIndex: Int
@Binding var answers: [Answer]
@State var dateAnswer = Date.now
@State var numberAnswer = -1
@State private var usersAnswer = ""
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.white)
.cornerRadius(30)
.padding(.vertical, screenWidth * 0.05)
.frame(width:screenWidth * 0.8, height: screenHeight * 0.7)
.shadow(color: Color.black.opacity(0.2), radius: 7, x: 0, y: 0)
VStack {
Text(prompts[currentCardIndex].question)
.foregroundColor(.black)
.font(.title3)
.multilineTextAlignment(.center)
.padding(.horizontal, screenWidth * 0.15)
// Conditional picker and selection for data saving
switch prompts[currentCardIndex].answerInputType {
case .date:
DatePicker("", selection: $dateAnswer, displayedComponents: .date)
.datePickerStyle(.wheel)
.frame(maxHeight: 400)
.labelsHidden()
case .number:
Picker("", selection: $numberAnswer) {
ForEach(1...14, id: \.self) { number in
Text("\(number)")
}
}
.frame(maxHeight: 400)
.pickerStyle(.wheel)
.labelsHidden()
.padding(.horizontal, screenWidth * 0.1)
}
if currentCardIndex >= (prompts.count - 1) {
NavigationLink(
destination: MainView()
.navigationBarBackButtonHidden(true)
) {
Text("To Main View")
}
} else {
Button(action: {
let answer: Answer
switch prompts[currentCardIndex].answerInputType {
case .date:
answer = Answer(promptID: currentCardIndex, answer: DateAnswer(date: dateAnswer))
case .number:
answer = Answer(promptID: currentCardIndex, answer: NumericAnswer(number: numberAnswer))
}
saveOrUpdateAnswer(answer: answer)
print(answers)
currentCardIndex += 1
}, label: {
ZStack {
Rectangle ()
.frame(width: screenWidth * 0.65, height: screenHeight * 0.05)
.cornerRadius(10)
Text("Save")
.foregroundColor(.white)
}
})
}
}
}
}
private func saveOrUpdateAnswer(answer: Answer) {
if let index = answers.firstIndex(where: { $0.promptID == answer.promptID }) {
answers[index] = answer
} else {
answers.append(answer)
}
}
}
struct UserPrompt_Previews: PreviewProvider {
static var previews: some View {
UserPrompt()
}
}