Save which cards are favorite

This commit is contained in:
2024-04-30 10:12:10 +02:00
parent 38b01b4a7e
commit 42fbfbeebb
7 changed files with 32 additions and 13 deletions

View File

@@ -45,6 +45,7 @@
6CEF7FA12BC88F6000E205F6 /* Flashcard+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Flashcard+CoreDataProperties.swift"; sourceTree = "<group>"; }; 6CEF7FA12BC88F6000E205F6 /* Flashcard+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Flashcard+CoreDataProperties.swift"; sourceTree = "<group>"; };
6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = WordAX0.0.3.xcdatamodel; sourceTree = "<group>"; }; 6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = WordAX0.0.3.xcdatamodel; sourceTree = "<group>"; };
6CEF7FA52BC96F2B00E205F6 /* ButtonHStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonHStackView.swift; sourceTree = "<group>"; }; 6CEF7FA52BC96F2B00E205F6 /* ButtonHStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonHStackView.swift; sourceTree = "<group>"; };
6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = WordAX0.0.4.xcdatamodel; sourceTree = "<group>"; };
6CF4394E2B83541D004C3543 /* WordAX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WordAX.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6CF4394E2B83541D004C3543 /* WordAX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WordAX.app; sourceTree = BUILT_PRODUCTS_DIR; };
6CF439512B83541D004C3543 /* WordAXApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordAXApp.swift; sourceTree = "<group>"; }; 6CF439512B83541D004C3543 /* WordAXApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordAXApp.swift; sourceTree = "<group>"; };
6CF439532B83541D004C3543 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = "<group>"; }; 6CF439532B83541D004C3543 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = "<group>"; };
@@ -424,12 +425,13 @@
6CEF7F7F2BC4694900E205F6 /* WordAXCD.xcdatamodeld */ = { 6CEF7F7F2BC4694900E205F6 /* WordAXCD.xcdatamodeld */ = {
isa = XCVersionGroup; isa = XCVersionGroup;
children = ( children = (
6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */,
6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */, 6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */,
6CEF7F9F2BC88F3900E205F6 /* WordAX0.0.2.xcdatamodel */, 6CEF7F9F2BC88F3900E205F6 /* WordAX0.0.2.xcdatamodel */,
6CEF7F962BC6B45F00E205F6 /* WordAX0.0.1.xcdatamodel */, 6CEF7F962BC6B45F00E205F6 /* WordAX0.0.1.xcdatamodel */,
6CEF7F802BC4694900E205F6 /* WordAX.xcdatamodel */, 6CEF7F802BC4694900E205F6 /* WordAX.xcdatamodel */,
); );
currentVersion = 6CEF7F802BC4694900E205F6 /* WordAX.xcdatamodel */; currentVersion = 6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */;
path = WordAXCD.xcdatamodeld; path = WordAXCD.xcdatamodeld;
sourceTree = "<group>"; sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel; versionGroupType = wrapper.xcdatamodel;

View File

@@ -24,6 +24,7 @@ extension Flashcard {
@NSManaged public var nextSpacedRepetitionMilestone: Int64 @NSManaged public var nextSpacedRepetitionMilestone: Int64
@NSManaged public var shown: Bool @NSManaged public var shown: Bool
@NSManaged public var shownCount: Int64 @NSManaged public var shownCount: Int64
@NSManaged public var favorite: Bool
} }
extension Flashcard : Identifiable { extension Flashcard : Identifiable {

View File

@@ -3,6 +3,6 @@
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>_XCCurrentVersionName</key> <key>_XCCurrentVersionName</key>
<string>WordAX.xcdatamodel</string> <string>WordAX0.0.4.xcdatamodel</string>
</dict> </dict>
</plist> </plist>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22757" systemVersion="23D60" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<entity name="Flashcard" representedClassName="Flashcard" syncable="YES">
<attribute name="dateAdded" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="desc" optional="YES" attributeType="String"/>
<attribute name="favorite" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="lastSeenOn" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="name" optional="YES" attributeType="String"/>
<attribute name="nextSpacedRepetitionMilestone" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="shown" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="shownCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
</entity>
</model>

View File

@@ -126,6 +126,5 @@ struct AnkiView: View {
#Preview { #Preview {
AnkiView() AnkiView()
.environmentObject(WordAXModelView())
.environment(\.managedObjectContext, DataController.preview.container.viewContext) .environment(\.managedObjectContext, DataController.preview.container.viewContext)
} }

View File

@@ -8,26 +8,32 @@
import SwiftUI import SwiftUI
struct FlashCardListRowView: View { struct FlashCardListRowView: View {
@EnvironmentObject var model: WordAXModelView @State var flashcard: Flashcard
var flashcard: Flashcard @State private var refresh: UUID = UUID()
@State var favorite = true
var body: some View { var body: some View {
HStack { HStack {
Group { Group {
if favorite { if !flashcard.favorite {
Image(systemName: "star") Image(systemName: "star")
} else { } else {
ZStack { ZStack {
Image(systemName: "star.fill") Image(systemName: "star.fill")
.foregroundStyle(.yellow) .foregroundStyle(.yellow)
Image(systemName: "star") Image(systemName: "star")
.opacity(0.4) .opacity(0)
} }
} }
} }
.onTapGesture { .onTapGesture {
self.favorite = !self.favorite flashcard.favorite.toggle()
do {
try flashcard.managedObjectContext?.save()
} catch {
print("Something went wrong while saving favorite cards, please try again")
}
refresh = UUID()
} }
.id(refresh)
.padding(.trailing) .padding(.trailing)
VStack { VStack {
Text(flashcard.name ?? "Unknown") Text(flashcard.name ?? "Unknown")
@@ -41,7 +47,6 @@ struct FlashCardListRowView: View {
.lineLimit(1) .lineLimit(1)
} }
} }
} }
} }

View File

@@ -8,7 +8,6 @@
import SwiftUI import SwiftUI
struct FlashCardListView: View { struct FlashCardListView: View {
@EnvironmentObject var model: WordAXModelView
@State var showDescription = true @State var showDescription = true
@State var addFlashcard = false @State var addFlashcard = false
@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard> @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard>
@@ -34,8 +33,7 @@ struct FlashCardListView: View {
}) })
} }
} } else {
else {
Text("You currently don't have any flashcards. To add flashcards, either click at the '+' button at the top or you can download them from the store (coming soon)") Text("You currently don't have any flashcards. To add flashcards, either click at the '+' button at the top or you can download them from the store (coming soon)")
.padding() .padding()
.background(.purple) .background(.purple)