From 42fbfbeebb89a85a8ef489a23e7c5e7a16ab8380 Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Tue, 30 Apr 2024 10:12:10 +0200 Subject: [PATCH] Save which cards are favorite --- WordAX.xcodeproj/project.pbxproj | 4 +++- .../Model/Flashcard+CoreDataProperties.swift | 1 + .../WordAXCD.xcdatamodeld/.xccurrentversion | 2 +- .../WordAX0.0.4.xcdatamodel/contents | 14 ++++++++++++++ WordAX/Views/AnkiView.swift | 1 - WordAX/Views/FlashCardListRowView.swift | 19 ++++++++++++------- WordAX/Views/FlashCardListView.swift | 4 +--- 7 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 WordAX/Model/WordAXCD.xcdatamodeld/WordAX0.0.4.xcdatamodel/contents diff --git a/WordAX.xcodeproj/project.pbxproj b/WordAX.xcodeproj/project.pbxproj index 6df76dc..d33c866 100644 --- a/WordAX.xcodeproj/project.pbxproj +++ b/WordAX.xcodeproj/project.pbxproj @@ -45,6 +45,7 @@ 6CEF7FA12BC88F6000E205F6 /* Flashcard+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Flashcard+CoreDataProperties.swift"; sourceTree = ""; }; 6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = WordAX0.0.3.xcdatamodel; sourceTree = ""; }; 6CEF7FA52BC96F2B00E205F6 /* ButtonHStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonHStackView.swift; sourceTree = ""; }; + 6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = WordAX0.0.4.xcdatamodel; sourceTree = ""; }; 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 = ""; }; 6CF439532B83541D004C3543 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; @@ -424,12 +425,13 @@ 6CEF7F7F2BC4694900E205F6 /* WordAXCD.xcdatamodeld */ = { isa = XCVersionGroup; children = ( + 6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */, 6CEF7FA42BC95F0300E205F6 /* WordAX0.0.3.xcdatamodel */, 6CEF7F9F2BC88F3900E205F6 /* WordAX0.0.2.xcdatamodel */, 6CEF7F962BC6B45F00E205F6 /* WordAX0.0.1.xcdatamodel */, 6CEF7F802BC4694900E205F6 /* WordAX.xcdatamodel */, ); - currentVersion = 6CEF7F802BC4694900E205F6 /* WordAX.xcdatamodel */; + currentVersion = 6CF12F1A2BDFF888000762CC /* WordAX0.0.4.xcdatamodel */; path = WordAXCD.xcdatamodeld; sourceTree = ""; versionGroupType = wrapper.xcdatamodel; diff --git a/WordAX/Model/Flashcard+CoreDataProperties.swift b/WordAX/Model/Flashcard+CoreDataProperties.swift index 758e022..d5f4150 100644 --- a/WordAX/Model/Flashcard+CoreDataProperties.swift +++ b/WordAX/Model/Flashcard+CoreDataProperties.swift @@ -24,6 +24,7 @@ extension Flashcard { @NSManaged public var nextSpacedRepetitionMilestone: Int64 @NSManaged public var shown: Bool @NSManaged public var shownCount: Int64 + @NSManaged public var favorite: Bool } extension Flashcard : Identifiable { diff --git a/WordAX/Model/WordAXCD.xcdatamodeld/.xccurrentversion b/WordAX/Model/WordAXCD.xcdatamodeld/.xccurrentversion index bebddd6..8c0cdd4 100644 --- a/WordAX/Model/WordAXCD.xcdatamodeld/.xccurrentversion +++ b/WordAX/Model/WordAXCD.xcdatamodeld/.xccurrentversion @@ -3,6 +3,6 @@ _XCCurrentVersionName - WordAX.xcdatamodel + WordAX0.0.4.xcdatamodel diff --git a/WordAX/Model/WordAXCD.xcdatamodeld/WordAX0.0.4.xcdatamodel/contents b/WordAX/Model/WordAXCD.xcdatamodeld/WordAX0.0.4.xcdatamodel/contents new file mode 100644 index 0000000..0d69bce --- /dev/null +++ b/WordAX/Model/WordAXCD.xcdatamodeld/WordAX0.0.4.xcdatamodel/contents @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WordAX/Views/AnkiView.swift b/WordAX/Views/AnkiView.swift index e305c0a..6666cbe 100644 --- a/WordAX/Views/AnkiView.swift +++ b/WordAX/Views/AnkiView.swift @@ -126,6 +126,5 @@ struct AnkiView: View { #Preview { AnkiView() - .environmentObject(WordAXModelView()) .environment(\.managedObjectContext, DataController.preview.container.viewContext) } diff --git a/WordAX/Views/FlashCardListRowView.swift b/WordAX/Views/FlashCardListRowView.swift index eb0c94b..a34b881 100644 --- a/WordAX/Views/FlashCardListRowView.swift +++ b/WordAX/Views/FlashCardListRowView.swift @@ -8,26 +8,32 @@ import SwiftUI struct FlashCardListRowView: View { - @EnvironmentObject var model: WordAXModelView - var flashcard: Flashcard - @State var favorite = true + @State var flashcard: Flashcard + @State private var refresh: UUID = UUID() var body: some View { HStack { Group { - if favorite { + if !flashcard.favorite { Image(systemName: "star") } else { ZStack { Image(systemName: "star.fill") .foregroundStyle(.yellow) Image(systemName: "star") - .opacity(0.4) + .opacity(0) } } } .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) VStack { Text(flashcard.name ?? "Unknown") @@ -41,7 +47,6 @@ struct FlashCardListRowView: View { .lineLimit(1) } } - } } diff --git a/WordAX/Views/FlashCardListView.swift b/WordAX/Views/FlashCardListView.swift index 6b26122..3a3c69f 100644 --- a/WordAX/Views/FlashCardListView.swift +++ b/WordAX/Views/FlashCardListView.swift @@ -8,7 +8,6 @@ import SwiftUI struct FlashCardListView: View { - @EnvironmentObject var model: WordAXModelView @State var showDescription = true @State var addFlashcard = false @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults @@ -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)") .padding() .background(.purple)