Statistic calculation for "a" word
This commit is contained in:
		@@ -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 = "<group>"; };
 | 
			
		||||
		7E943A29273211C300E7DDF4 /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
 | 
			
		||||
		7E943A2C273211C300E7DDF4 /* Toki_Trainer.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Toki_Trainer.xcdatamodel; sourceTree = "<group>"; };
 | 
			
		||||
		7EF546152737B8FA00537AE6 /* FlashCardResultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardResultView.swift; sourceTree = "<group>"; };
 | 
			
		||||
		7EF546152737B8FA00537AE6 /* FlashCardResultsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardResultsView.swift; sourceTree = "<group>"; };
 | 
			
		||||
/* 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 = "<group>";
 | 
			
		||||
@@ -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 */,
 | 
			
		||||
 
 | 
			
		||||
@@ -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">
 | 
			
		||||
         </BreakpointContent>
 | 
			
		||||
 
 | 
			
		||||
@@ -10,5 +10,13 @@
 | 
			
		||||
			<integer>0</integer>
 | 
			
		||||
		</dict>
 | 
			
		||||
	</dict>
 | 
			
		||||
	<key>SuppressBuildableAutocreation</key>
 | 
			
		||||
	<dict>
 | 
			
		||||
		<key>7E943A1C273211C200E7DDF4</key>
 | 
			
		||||
		<dict>
 | 
			
		||||
			<key>primary</key>
 | 
			
		||||
			<true/>
 | 
			
		||||
		</dict>
 | 
			
		||||
	</dict>
 | 
			
		||||
</dict>
 | 
			
		||||
</plist>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,15 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 | 
			
		||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19461" systemVersion="21A5552a" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
 | 
			
		||||
    <entity name="FlashCardAnswer" representedClassName="FlashCardAnswer" syncable="YES" codeGenerationType="class">
 | 
			
		||||
        <attribute name="correct" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
 | 
			
		||||
        <attribute name="correctCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
 | 
			
		||||
        <attribute name="triesCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
 | 
			
		||||
        <attribute name="word" optional="YES" attributeType="String"/>
 | 
			
		||||
    </entity>
 | 
			
		||||
    <entity name="Item" representedClassName="Item" syncable="YES" codeGenerationType="class">
 | 
			
		||||
        <attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
 | 
			
		||||
    </entity>
 | 
			
		||||
    <elements>
 | 
			
		||||
        <element name="FlashCardAnswer" positionX="-54" positionY="0" width="128" height="74"/>
 | 
			
		||||
        <element name="Item" positionX="-63" positionY="-18" width="128" height="44"/>
 | 
			
		||||
        <element name="FlashCardAnswer" positionX="-54" positionY="0" width="128" height="59"/>
 | 
			
		||||
    </elements>
 | 
			
		||||
</model>
 | 
			
		||||
@@ -18,7 +18,7 @@ class FlashCardsViewModel: ObservableObject {
 | 
			
		||||
        if let safeDictionary = jsonLoader.loadDictionary() {
 | 
			
		||||
            fullDictionary = safeDictionary
 | 
			
		||||
            randomDictionary = safeDictionary
 | 
			
		||||
            randomDictionary.shuffle()
 | 
			
		||||
            //randomDictionary.shuffle()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
@@ -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