From cf025300734c54fc48a7f8c3558b8463f852009e Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Sun, 7 Nov 2021 18:51:48 -0500 Subject: [PATCH] FlashCardResults shows colorized statistics --- Toki Trainer/Constants.swift | 8 ++++ Toki Trainer/Views/FlashCardResultsView.swift | 42 +++++++++++++++---- Toki Trainer/Views/FlashCardView.swift | 4 +- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/Toki Trainer/Constants.swift b/Toki Trainer/Constants.swift index 99606dc..452977d 100644 --- a/Toki Trainer/Constants.swift +++ b/Toki Trainer/Constants.swift @@ -31,4 +31,12 @@ struct K { return request } + + static var getFlashCardAnswersWithPredicateFetchRequest: NSFetchRequest { + let request: NSFetchRequest = FlashCardAnswer.fetchRequest() + request.sortDescriptors = [] + request.predicate = NSPredicate(format: "word == %@", "a") + + return request + } } diff --git a/Toki Trainer/Views/FlashCardResultsView.swift b/Toki Trainer/Views/FlashCardResultsView.swift index 92ca657..fd18999 100644 --- a/Toki Trainer/Views/FlashCardResultsView.swift +++ b/Toki Trainer/Views/FlashCardResultsView.swift @@ -11,26 +11,52 @@ struct FlashCardResultsView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var answers: FetchedResults + + @ObservedObject var tokiDictionaryViewModel = TokiDictionaryViewModel() + @State private var wordStatistics: [String: Double] = [:] @State private var statistics = 0.0 func calculateStatistics() { for answer in answers { - if answer.triesCount != 0 { - print("word: \(answer.word)") - print("tries: \(answer.triesCount)") - print("correct: \(answer.correctCount)") - self.statistics = Double(answer.correctCount) / Double(answer.triesCount) + if answer.triesCount > 0 { + self.wordStatistics[answer.word!] = (Double(answer.correctCount) / Double(answer.triesCount)) * 100 } } + print(wordStatistics) + } + + func calculateBackgroundColor(_ percent: Double) -> Color { + if percent >= 80 { + return Color.green + } else if percent >= 40 { + return Color.yellow + } else { + return Color.red + } + } var body: some View { - Text("Percentage: \(statistics)") - .onAppear { - calculateStatistics() + List(wordStatistics.sorted(by: <), id: \.key) { entry in + HStack { + Text(entry.key) + Spacer() + Text("\(String(format: "%.2f", entry.value)) %") } + .listRowBackground(calculateBackgroundColor(entry.value)) + } + .onAppear { + calculateStatistics() + } + } +} + +extension Double { + func formatAsPercent(places: Int) -> String { + let formattedValue = String(format: "%.2f", self) + return formattedValue } } diff --git a/Toki Trainer/Views/FlashCardView.swift b/Toki Trainer/Views/FlashCardView.swift index 27c889a..6cf6202 100644 --- a/Toki Trainer/Views/FlashCardView.swift +++ b/Toki Trainer/Views/FlashCardView.swift @@ -103,8 +103,10 @@ struct FlashCardStack: View { func setFlashCardAnswersCoreData(_ correct: Bool) { var cardInDatabase = false for answer in flashCardAnswers { - print(answer.word) if answer.word == dictionary[currentFlashCard].word { + print("word in database: \(answer.word)") + print("tries: \(answer.triesCount)") + print("correct`: \(answer.correctCount)") cardInDatabase = true answer.setValue((answer.triesCount + 1), forKey: "triesCount") if correct {