Statistic calculation for "a" word
This commit is contained in:
@@ -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")
|
||||
|
@@ -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<FlashCardAnswer>
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
51
Toki Trainer/Views/FlashCardResultsView.swift
Normal file
51
Toki Trainer/Views/FlashCardResultsView.swift
Normal file
@@ -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<FlashCardAnswer>
|
||||
|
||||
@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()
|
||||
}
|
||||
}
|
@@ -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<FlashCardAnswer>
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user