-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrainingView.swift
More file actions
194 lines (171 loc) · 7.45 KB
/
TrainingView.swift
File metadata and controls
194 lines (171 loc) · 7.45 KB
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
import SwiftUI
struct TrainingView: View {
@State private var currentQuestionIndex = 0
@State private var selectedChoices: Set<UUID> = []
@State private var showResult = false
@State private var startTime: Date?
@State private var isBookmarked: Bool = false
@Environment(\.presentationMode) var presentationMode
@ObservedObject var userTrainingStore = UserTrainingStore.shared
let course: Course
@StateObject private var questionLoader: QuestionLoader
init(course: Course) {
self.course = course
_questionLoader = StateObject(wrappedValue: QuestionLoader(filename: course.shortName + ".json", intelligentLearning: false))
}
var body: some View {
ZStack {
VStack {
if !questionLoader.questions.isEmpty {
let questions = Array(questionLoader.questions)
let question = questions[currentQuestionIndex]
QuestionNavbar(
currentQuestionIndex: currentQuestionIndex,
totalQuestions: questionLoader.questions.count,
question: question,
isBookmarked: $isBookmarked
)
QuestionView(
mode: .training,
question: question,
selectedChoices: selectedChoices,
isMultipleResponse: question.multipleResponse,
isResultShown: showResult,
onChoiceSelected: { choiceID in
handleChoiceSelection(choiceID, question)
}
)
.navigationBarItems(trailing: bookmarkButton)
.onAppear {
updateBookmarkState()
}
HStack(spacing: 20) {
if !showResult {
if currentQuestionIndex > 0 {
Button(action: {
showPreviousQuestion()
}) {
Text("Previous")
.padding(10)
.frame(maxWidth: .infinity)
.background(Color.customSecondary)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal, 16)
}
}
Button(action: {
showResult = true
updateUserTrainingData(for: question)
}) {
Text("Show Result")
.padding(10)
.frame(maxWidth: .infinity)
.background(Color.customPrimary)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal, 16)
}
} else {
Button(action: {
showNextQuestion()
}) {
Text("Next Question")
.padding(10)
.frame(maxWidth: .infinity)
.background(Color.customSecondary)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal, 16)
}
}
}
.padding(.top)
} else {
Text("No Questions available! Please download course")
}
Spacer()
}
.navigationBarHidden(true)
.onAppear {
startTime = Date()
loadUserTrainingData()
updateBookmarkState() // Ensure bookmark state is updated when the view appears
}
.onDisappear {
saveUserTrainingData()
}
}
}
private var bookmarkButton: some View {
Button(action: {
toggleBookmark()
}) {
Image(systemName: isBookmarked ? "bookmark.fill" : "bookmark")
.foregroundColor(isBookmarked ? .red : .blue)
}
}
private func showNextQuestion() {
if currentQuestionIndex < questionLoader.questions.count - 1 {
currentQuestionIndex += 1
selectedChoices.removeAll()
showResult = false
startTime = Date()
updateBookmarkState() // Update the bookmark state for the next question
}
}
private func showPreviousQuestion() {
if currentQuestionIndex > 0 {
currentQuestionIndex -= 1
selectedChoices.removeAll()
showResult = false
startTime = Date()
updateBookmarkState() // Update the bookmark state for the previous question
}
}
private func updateBookmarkState() {
let currentQuestion = questionLoader.questions[currentQuestionIndex]
isBookmarked = FavoritesStorage.shared.isBookmarked(currentQuestion)
}
private func toggleBookmark() {
let currentQuestion = questionLoader.questions[currentQuestionIndex]
if isBookmarked {
FavoritesStorage.shared.removeBookmarkByQuestionText(currentQuestion.question)
} else {
let newBookmark = Bookmark(id: UUID(), question: currentQuestion, answer: currentQuestion.choices)
FavoritesStorage.shared.addBookmark(newBookmark)
}
isBookmarked.toggle()
}
func handleChoiceSelection(_ choiceID: UUID, _ question: Question) {
if question.multipleResponse {
if selectedChoices.contains(choiceID) {
selectedChoices.remove(choiceID)
} else {
selectedChoices.insert(choiceID)
}
} else {
selectedChoices = [choiceID]
}
}
func updateUserTrainingData(for question: Question) {
guard var userTrainingData = userTrainingStore.trainingData[course.shortName] else { return }
if let startTime = startTime {
userTrainingData.timeSpent += Date().timeIntervalSince(startTime)
}
let correctChoices = Set(question.choices.filter { $0.correct }.map { $0.id })
let userCorrectChoices = selectedChoices.intersection(correctChoices)
userTrainingData.correctAnswers += userCorrectChoices.count
userTrainingData.wrongAnswers += selectedChoices.subtracting(correctChoices).count
userTrainingData.updateStats(for: question.id, correctChoices: correctChoices, selectedChoices: selectedChoices)
userTrainingStore.saveTrainingData(userTrainingData)
}
func loadUserTrainingData() {
_ = userTrainingStore.loadTrainingData(forCourse: course.shortName)
}
func saveUserTrainingData() {
if let userTrainingData = userTrainingStore.trainingData[course.shortName] {
userTrainingStore.saveTrainingData(userTrainingData)
}
}
}