FlashCardResults shows colorized statistics
This commit is contained in:
parent
a3d65da241
commit
cf02530073
@ -31,4 +31,12 @@ struct K {
|
|||||||
|
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static var getFlashCardAnswersWithPredicateFetchRequest: NSFetchRequest<FlashCardAnswer> {
|
||||||
|
let request: NSFetchRequest<FlashCardAnswer> = FlashCardAnswer.fetchRequest()
|
||||||
|
request.sortDescriptors = []
|
||||||
|
request.predicate = NSPredicate(format: "word == %@", "a")
|
||||||
|
|
||||||
|
return request
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,25 +12,51 @@ struct FlashCardResultsView: View {
|
|||||||
|
|
||||||
@FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var answers: FetchedResults<FlashCardAnswer>
|
@FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var answers: FetchedResults<FlashCardAnswer>
|
||||||
|
|
||||||
|
@ObservedObject var tokiDictionaryViewModel = TokiDictionaryViewModel()
|
||||||
|
|
||||||
|
@State private var wordStatistics: [String: Double] = [:]
|
||||||
@State private var statistics = 0.0
|
@State private var statistics = 0.0
|
||||||
|
|
||||||
func calculateStatistics() {
|
func calculateStatistics() {
|
||||||
for answer in answers {
|
for answer in answers {
|
||||||
if answer.triesCount != 0 {
|
if answer.triesCount > 0 {
|
||||||
print("word: \(answer.word)")
|
self.wordStatistics[answer.word!] = (Double(answer.correctCount) / Double(answer.triesCount)) * 100
|
||||||
print("tries: \(answer.triesCount)")
|
|
||||||
print("correct: \(answer.correctCount)")
|
|
||||||
self.statistics = Double(answer.correctCount) / Double(answer.triesCount)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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 {
|
var body: some View {
|
||||||
Text("Percentage: \(statistics)")
|
List(wordStatistics.sorted(by: <), id: \.key) { entry in
|
||||||
.onAppear {
|
HStack {
|
||||||
calculateStatistics()
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,8 +103,10 @@ struct FlashCardStack: View {
|
|||||||
func setFlashCardAnswersCoreData(_ correct: Bool) {
|
func setFlashCardAnswersCoreData(_ correct: Bool) {
|
||||||
var cardInDatabase = false
|
var cardInDatabase = false
|
||||||
for answer in flashCardAnswers {
|
for answer in flashCardAnswers {
|
||||||
print(answer.word)
|
|
||||||
if answer.word == dictionary[currentFlashCard].word {
|
if answer.word == dictionary[currentFlashCard].word {
|
||||||
|
print("word in database: \(answer.word)")
|
||||||
|
print("tries: \(answer.triesCount)")
|
||||||
|
print("correct`: \(answer.correctCount)")
|
||||||
cardInDatabase = true
|
cardInDatabase = true
|
||||||
answer.setValue((answer.triesCount + 1), forKey: "triesCount")
|
answer.setValue((answer.triesCount + 1), forKey: "triesCount")
|
||||||
if correct {
|
if correct {
|
||||||
|
Loading…
Reference in New Issue
Block a user