2021-11-07 22:15:11 +00:00
|
|
|
//
|
|
|
|
// FlashCardResultView.swift
|
|
|
|
// Toki Trainer
|
|
|
|
//
|
|
|
|
// Created by Avery Ada Pace on 11/7/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct FlashCardResultsView: View {
|
|
|
|
@Environment(\.managedObjectContext) private var viewContext
|
|
|
|
|
2021-11-07 23:12:21 +00:00
|
|
|
@FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var answers: FetchedResults<FlashCardAnswer>
|
2021-11-07 23:51:48 +00:00
|
|
|
|
|
|
|
@ObservedObject var tokiDictionaryViewModel = TokiDictionaryViewModel()
|
2021-11-07 22:15:11 +00:00
|
|
|
|
2021-11-07 23:51:48 +00:00
|
|
|
@State private var wordStatistics: [String: Double] = [:]
|
2021-11-08 17:49:50 +00:00
|
|
|
@State private var sortedWordStatistics: [(String, Double)] = []
|
2021-11-07 22:15:11 +00:00
|
|
|
|
|
|
|
func calculateStatistics() {
|
2021-11-07 23:12:21 +00:00
|
|
|
for answer in answers {
|
2021-11-07 23:51:48 +00:00
|
|
|
if answer.triesCount > 0 {
|
|
|
|
self.wordStatistics[answer.word!] = (Double(answer.correctCount) / Double(answer.triesCount)) * 100
|
2021-11-07 22:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 17:49:50 +00:00
|
|
|
let sortedStatistics = wordStatistics.sorted {
|
|
|
|
return $0.value > $1.value
|
|
|
|
}
|
|
|
|
self.sortedWordStatistics = sortedStatistics
|
2021-11-07 23:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func calculateBackgroundColor(_ percent: Double) -> Color {
|
|
|
|
if percent >= 80 {
|
|
|
|
return Color.green
|
|
|
|
} else if percent >= 40 {
|
|
|
|
return Color.yellow
|
|
|
|
} else {
|
|
|
|
return Color.red
|
|
|
|
}
|
2021-11-07 22:15:11 +00:00
|
|
|
}
|
2021-11-07 23:12:21 +00:00
|
|
|
|
2021-11-07 22:15:11 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2021-11-08 17:49:50 +00:00
|
|
|
List(sortedWordStatistics, id: \.0) { entry in
|
2021-11-07 23:51:48 +00:00
|
|
|
HStack {
|
2021-11-08 17:49:50 +00:00
|
|
|
Text(entry.0)
|
2021-11-07 23:51:48 +00:00
|
|
|
Spacer()
|
2021-11-08 17:49:50 +00:00
|
|
|
Text("\(String(format: "%.2f", entry.1)) %")
|
2021-11-07 22:15:11 +00:00
|
|
|
}
|
2021-11-08 17:49:50 +00:00
|
|
|
.listRowBackground(calculateBackgroundColor(entry.1))
|
2021-11-07 23:51:48 +00:00
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
calculateStatistics()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 22:15:11 +00:00
|
|
|
struct FlashCardResultView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
FlashCardResultsView()
|
|
|
|
}
|
|
|
|
}
|