From e4c5c2df05c923ab48acd3ad90f5f0904ab7b7d8 Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Sun, 7 Nov 2021 17:15:11 -0500 Subject: [PATCH] Statistic calculation for "a" word --- Toki Trainer.xcodeproj/project.pbxproj | 8 +-- .../xcdebugger/Breakpoints_v2.xcbkptlist | 4 +- .../xcschemes/xcschememanagement.plist | 8 +++ .../Toki_Trainer.xcdatamodel/contents | 5 +- .../ViewModels/FlashCardsViewModel.swift | 2 +- Toki Trainer/Views/ContentView.swift | 2 +- Toki Trainer/Views/FlashCardResultView.swift | 30 ----------- Toki Trainer/Views/FlashCardResultsView.swift | 51 +++++++++++++++++++ Toki Trainer/Views/FlashCardView.swift | 47 ++++++++++++++--- 9 files changed, 110 insertions(+), 47 deletions(-) delete mode 100644 Toki Trainer/Views/FlashCardResultView.swift create mode 100644 Toki Trainer/Views/FlashCardResultsView.swift diff --git a/Toki Trainer.xcodeproj/project.pbxproj b/Toki Trainer.xcodeproj/project.pbxproj index 4c75fc5..cca1bb0 100644 --- a/Toki Trainer.xcodeproj/project.pbxproj +++ b/Toki Trainer.xcodeproj/project.pbxproj @@ -23,7 +23,7 @@ 7E943A28273211C300E7DDF4 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E943A27273211C300E7DDF4 /* Preview Assets.xcassets */; }; 7E943A2A273211C300E7DDF4 /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A29273211C300E7DDF4 /* Persistence.swift */; }; 7E943A2D273211C300E7DDF4 /* Toki_Trainer.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A2B273211C300E7DDF4 /* Toki_Trainer.xcdatamodeld */; }; - 7EF546162737B8FB00537AE6 /* FlashCardResultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EF546152737B8FA00537AE6 /* FlashCardResultView.swift */; }; + 7EF546162737B8FB00537AE6 /* FlashCardResultsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EF546152737B8FA00537AE6 /* FlashCardResultsView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -44,7 +44,7 @@ 7E943A27273211C300E7DDF4 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 7E943A29273211C300E7DDF4 /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = ""; }; 7E943A2C273211C300E7DDF4 /* Toki_Trainer.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Toki_Trainer.xcdatamodel; sourceTree = ""; }; - 7EF546152737B8FA00537AE6 /* FlashCardResultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardResultView.swift; sourceTree = ""; }; + 7EF546152737B8FA00537AE6 /* FlashCardResultsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardResultsView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -75,7 +75,7 @@ 7E943A22273211C200E7DDF4 /* ContentView.swift */, 7E20D5FE2733AFE700D75B9A /* PartsOfSpeechView.swift */, 7E71E6EC2735D70C007CFF72 /* FlashCardView.swift */, - 7EF546152737B8FA00537AE6 /* FlashCardResultView.swift */, + 7EF546152737B8FA00537AE6 /* FlashCardResultsView.swift */, ); path = Views; sourceTree = ""; @@ -217,7 +217,7 @@ 7E20D6012734466800D75B9A /* TokiDictionaryViewModel.swift in Sources */, 7E2811192733027F0063DC78 /* TokiPartOfSpeech.swift in Sources */, 7E943A23273211C200E7DDF4 /* ContentView.swift in Sources */, - 7EF546162737B8FB00537AE6 /* FlashCardResultView.swift in Sources */, + 7EF546162737B8FB00537AE6 /* FlashCardResultsView.swift in Sources */, 7E2811172733027F0063DC78 /* TokiDictionary.swift in Sources */, 7E71E6ED2735D70C007CFF72 /* FlashCardView.swift in Sources */, 7E943A21273211C200E7DDF4 /* Toki_TrainerApp.swift in Sources */, diff --git a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index e1bd7fc..5a7d147 100644 --- a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -14,8 +14,8 @@ filePath = "Toki Trainer/Views/FlashCardView.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "223" - endingLineNumber = "223" + startingLineNumber = "256" + endingLineNumber = "256" landmarkName = "init(isFaceDown:frontText:backText:)" landmarkType = "7"> diff --git a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist index a3b04aa..611d488 100644 --- a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist @@ -10,5 +10,13 @@ 0 + SuppressBuildableAutocreation + + 7E943A1C273211C200E7DDF4 + + primary + + + diff --git a/Toki Trainer/Toki_Trainer.xcdatamodeld/Toki_Trainer.xcdatamodel/contents b/Toki Trainer/Toki_Trainer.xcdatamodeld/Toki_Trainer.xcdatamodel/contents index 2b031dd..c6b2f32 100644 --- a/Toki Trainer/Toki_Trainer.xcdatamodeld/Toki_Trainer.xcdatamodel/contents +++ b/Toki Trainer/Toki_Trainer.xcdatamodeld/Toki_Trainer.xcdatamodel/contents @@ -1,14 +1,15 @@ - + + + - \ No newline at end of file diff --git a/Toki Trainer/ViewModels/FlashCardsViewModel.swift b/Toki Trainer/ViewModels/FlashCardsViewModel.swift index 6cee3ed..3c93748 100644 --- a/Toki Trainer/ViewModels/FlashCardsViewModel.swift +++ b/Toki Trainer/ViewModels/FlashCardsViewModel.swift @@ -18,7 +18,7 @@ class FlashCardsViewModel: ObservableObject { if let safeDictionary = jsonLoader.loadDictionary() { fullDictionary = safeDictionary randomDictionary = safeDictionary - randomDictionary.shuffle() + //randomDictionary.shuffle() } } diff --git a/Toki Trainer/Views/ContentView.swift b/Toki Trainer/Views/ContentView.swift index a088fa3..b36a55a 100644 --- a/Toki Trainer/Views/ContentView.swift +++ b/Toki Trainer/Views/ContentView.swift @@ -29,7 +29,7 @@ struct ContentView: View { Image(systemName: "character.textbox") Text("Flash Cards") } - FlashCardResultView() + FlashCardResultsView() .tabItem { Image(systemName: "phone.fill") Text("Flash Card Results") diff --git a/Toki Trainer/Views/FlashCardResultView.swift b/Toki Trainer/Views/FlashCardResultView.swift deleted file mode 100644 index 471e16a..0000000 --- a/Toki Trainer/Views/FlashCardResultView.swift +++ /dev/null @@ -1,30 +0,0 @@ -// -// FlashCardResultView.swift -// Toki Trainer -// -// Created by Avery Ada Pace on 11/7/21. -// - -import SwiftUI - -struct FlashCardResultView: View { - @Environment(\.managedObjectContext) private var viewContext - - @FetchRequest var flashCardAnswers: FetchedResults - - init() { - self._flashCardAnswers = FetchRequest(entity: FlashCardAnswer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FlashCardAnswer.word, ascending: true)], predicate: nil, animation: .none) - } - - var body: some View { - List(flashCardAnswers, id: \.self) { answer in - Text(answer.word ?? "Unknown") - } - } -} - -struct FlashCardResultView_Previews: PreviewProvider { - static var previews: some View { - FlashCardResultView() - } -} diff --git a/Toki Trainer/Views/FlashCardResultsView.swift b/Toki Trainer/Views/FlashCardResultsView.swift new file mode 100644 index 0000000..9f10aab --- /dev/null +++ b/Toki Trainer/Views/FlashCardResultsView.swift @@ -0,0 +1,51 @@ +// +// FlashCardResultView.swift +// Toki Trainer +// +// Created by Avery Ada Pace on 11/7/21. +// + +import SwiftUI + +struct FlashCardResultsView: View { + @Environment(\.managedObjectContext) private var viewContext + + @FetchRequest(entity:FlashCardAnswer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FlashCardAnswer.word, ascending: false)], predicate: NSPredicate(format: "word == %@", "a")) var flashCardAnswers: FetchedResults + + @State private var statistics = 0.0 + + func calculateStatistics() { + print("count: \(flashCardAnswers.count)") + for answer in flashCardAnswers { + if answer.triesCount != 0 { + print("word: \(answer.word)") + print("tries: \(answer.triesCount)") + print("correct: \(answer.correctCount)") + self.statistics = Double(answer.correctCount) / Double(answer.triesCount) + } + } + } + +// func calculateStatistics() { +// var correctAnswers = 0 +// for answer in flashCardAnswers { +// if answer.correct { +// correctAnswers += 1 +// } +// self.statistics = Double(correctAnswers) / Double(flashCardAnswers.count) +// } +// } + + var body: some View { + Text("Percentage: \(statistics)") + .onAppear { + calculateStatistics() + } + } +} + +struct FlashCardResultView_Previews: PreviewProvider { + static var previews: some View { + FlashCardResultsView() + } +} diff --git a/Toki Trainer/Views/FlashCardView.swift b/Toki Trainer/Views/FlashCardView.swift index ee5877b..96f39f1 100644 --- a/Toki Trainer/Views/FlashCardView.swift +++ b/Toki Trainer/Views/FlashCardView.swift @@ -39,6 +39,8 @@ extension Binding { struct FlashCardStack: View { @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(entity:FlashCardAnswer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FlashCardAnswer.word, ascending: false)]) var flashCardAnswers: FetchedResults + var dictionary: [TokiDictEntry] @State private var flashCards: [FlashCard] = [] @State private var topFlashCard: FlashCard? = nil @@ -72,7 +74,6 @@ struct FlashCardStack: View { Spacer() .onAppear { initFlashCards() - print(currentFlashCard) } } @@ -99,18 +100,50 @@ struct FlashCardStack: View { flashCardsAreInteractive[currentFlashCard] = true } + func setFlashCardAnswersCoreData(_ correct: Bool) { + var cardInDatabase = false + for answer in flashCardAnswers { + print(answer.word) + if answer.word == dictionary[currentFlashCard].word { + cardInDatabase = true + answer.setValue((answer.triesCount + 1), forKey: "triesCount") + if correct { + answer.setValue((answer.correctCount + 1), forKey: "correctCount") + } + print("answer found in database") + } + } + + if cardInDatabase == false { + let answer = FlashCardAnswer(context: viewContext) + answer.word = dictionary[currentFlashCard].word + answer.triesCount = 1 + if correct { + answer.correctCount = 1 + } + print("answer not found in database") + } + +// for answer in flashCardAnswers { +// if answer.word == dictionary[currentFlashCard].word { +// flashCardAnswer.word = answer.word +// flashCardAnswer.triesCount = answer.triesCount + 1 +// if correct { +// flashCardAnswer.correctCount = answer.correctCount + 1 +// } +// } +// } + try? viewContext.save() + } + func cardAnswerReceived() { - let flashCardAnswer = FlashCardAnswer(context: viewContext) - flashCardAnswer.word = dictionary[currentFlashCard].word if flashCardsResults[currentFlashCard] == FlashCardResult.Correct { - flashCardAnswer.correct = true + setFlashCardAnswersCoreData(true) } else if flashCardsResults[currentFlashCard] == FlashCardResult.Incorrect { - flashCardAnswer.correct = false + setFlashCardAnswersCoreData(false) } else { return } - try? viewContext.save() - nextFlashCard() }