From 64a98495173c52ed5fc088e8cfa7bea4e741a074 Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Tue, 9 Nov 2021 14:34:29 -0500 Subject: [PATCH] Flash Cards now calculate percentage from last attempt --- Toki Trainer/Views/FlashCardLessonsView.swift | 2 +- Toki Trainer/Views/FlashCardView.swift | 40 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Toki Trainer/Views/FlashCardLessonsView.swift b/Toki Trainer/Views/FlashCardLessonsView.swift index b764a7c..e0c4874 100644 --- a/Toki Trainer/Views/FlashCardLessonsView.swift +++ b/Toki Trainer/Views/FlashCardLessonsView.swift @@ -74,7 +74,7 @@ struct FlashCardLessonsView: View { calculateStatistics(lesson) } Spacer() - if statisticsCalculated && lessonStatistics[lesson.lesson] != 0.0 && lessonStatistics[lesson.lesson] != nil { + if statisticsCalculated && lessonStatistics[lesson.lesson] != nil { Text("\(String(format: "%.0f", lessonStatistics[lesson.lesson]!)) %") .multilineTextAlignment(.leading) } diff --git a/Toki Trainer/Views/FlashCardView.swift b/Toki Trainer/Views/FlashCardView.swift index 610b37d..e6ea3f0 100644 --- a/Toki Trainer/Views/FlashCardView.swift +++ b/Toki Trainer/Views/FlashCardView.swift @@ -66,7 +66,8 @@ struct FlashCardStack: View { @State private var flashCardsAreInteractive: [Bool] = [] @State private var flashCardsVertOffset: [CGFloat] = [] @State private var flashCardsResults: [FlashCardResult] = [] - @State private var fadeOutOverlay = false + @State private var helperFadeOutOverlay = false + @State private var deckCompleteFadeInOverlay = false @State private var currentFlashCard = 0 @@ -82,18 +83,18 @@ struct FlashCardStack: View { } } } - .overlay(HStack { - Image(systemName: "arrow.backward") - Text("Incorrect") - Spacer() - Text("Correct") - Image(systemName: "arrow.right") - }.opacity(fadeOutOverlay ? 0.0 : 1.0), alignment: .top) + } + .overlay(HStack { + Image(systemName: "arrow.backward") + Text("Incorrect") + Spacer() + Text("Correct") + Image(systemName: "arrow.right") + }.opacity(helperFadeOutOverlay ? 0.0 : 1.0), alignment: .top) + .onAppear { + initFlashCards() } Spacer() - .onAppear { - initFlashCards() - } } func initFlashCards() { @@ -117,6 +118,17 @@ struct FlashCardStack: View { flashCardsVertOffset[currentFlashCard] = 50 flashCardsAreInteractive[currentFlashCard] = true + + resetLessonAnswersCoreData() + } + + func resetLessonAnswersCoreData() { + for lessonAnswer in lessonAnswers { + if lessonAnswer.lesson == currentLesson { + viewContext.delete(lessonAnswer) + } + } + try? viewContext.save() } func setFlashCardAnswersCoreData(_ correct: Bool) { @@ -191,7 +203,7 @@ struct FlashCardStack: View { flashCardsAreInteractive[currentFlashCard] = true - self.fadeOutOverlay = true + self.helperFadeOutOverlay = true if ((flashCards.count - 1) - currentFlashCard) > 3 { print("flashCards: \(flashCards.count)") @@ -334,9 +346,9 @@ struct CardFlipModifier: AnimatableModifier { } struct FlashCardView_Previews: PreviewProvider { - static var viewModel = TokiDictionaryViewModel() + static var lessonVM = FlashCardLessonsViewModel() static var previews: some View { - FlashCardView(lesson: "Preview", passedDictionary: viewModel.dictionary).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + FlashCardView(lesson: lessonVM.lessons[0].lesson, passedDictionary: lessonVM.lessons[0].words).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }