Integrate flashcard list into deck view
This commit is contained in:
		| @@ -6,15 +6,18 @@ | ||||
| // | ||||
|  | ||||
| import SwiftUI | ||||
| import CoreData | ||||
|  | ||||
| struct FlashCardListView: View { | ||||
|     @State var showDescription = true | ||||
|     @State var addFlashcard = false | ||||
|     @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "favorite", ascending: false), NSSortDescriptor(key: "dateAdded", ascending: false)]) var flashcards: FetchedResults<Flashcard> | ||||
|     @State var addFlashcard: Bool = false | ||||
|     @State var flashcards: [Flashcard] = [] | ||||
|     var deck: Deck? | ||||
|     @Environment(\.managedObjectContext) var moc | ||||
|      | ||||
|     var body: some View { | ||||
|         GeometryReader { geometry in | ||||
|             NavigationSplitView { | ||||
|             NavigationStack { | ||||
|                 Group { | ||||
|                     if !flashcards.isEmpty { | ||||
|                         List { | ||||
| @@ -29,6 +32,7 @@ struct FlashCardListView: View { | ||||
|                                 for index in offsets { | ||||
|                                     let flashcard = flashcards[index] | ||||
|                                     moc.delete(flashcard) | ||||
|                                     flashcards.remove(at: index) | ||||
|                                 } | ||||
|                                  | ||||
|                                 do { | ||||
| @@ -39,16 +43,27 @@ struct FlashCardListView: View { | ||||
|                             }) | ||||
|                         } | ||||
|                     } 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) | ||||
|                             .clipShape(.buttonBorder) | ||||
|                             .frame(maxWidth: geometry.size.width - 30) | ||||
|                         Group { | ||||
|                             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) | ||||
|                                 .clipShape(.buttonBorder) | ||||
|                         } | ||||
|                         .frame(maxHeight: .infinity) | ||||
|                         .padding(.horizontal) | ||||
|                     } | ||||
|                 } | ||||
|                 .navigationTitle("All Flashcards") | ||||
|                 .onAppear { | ||||
|                     refreshFlashcards() | ||||
|                 } | ||||
|                 .onChange(of: addFlashcard, { oldValue, newValue in | ||||
|                     if oldValue { | ||||
|                         refreshFlashcards() | ||||
|                     } | ||||
|                 }) | ||||
|                 .navigationBarTitle(deck?.name ?? "All Flashcards", displayMode: deck != nil ? .inline : .automatic) | ||||
|                 .toolbar { | ||||
|                     ToolbarItemGroup(placement: .topBarLeading) { | ||||
|                     ToolbarItemGroup(placement: .topBarTrailing) { | ||||
|                         EditButton() | ||||
|                     } | ||||
|                     ToolbarItemGroup(placement: .topBarTrailing) { | ||||
| @@ -59,14 +74,29 @@ struct FlashCardListView: View { | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } detail: { | ||||
|                 Text("Select flashcard to get details about") | ||||
|             } | ||||
|             .sheet(isPresented: $addFlashcard, content: { | ||||
|                 AddFlashCardView(isShowing: $addFlashcard) | ||||
|                 AddFlashCardView(isShowing: $addFlashcard, selectedDeck: deck) | ||||
|             }) | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     private func refreshFlashcards() { | ||||
|         let request = NSFetchRequest<Flashcard>(entityName: "Flashcard") | ||||
|         request.sortDescriptors = [NSSortDescriptor(key: "favorite", ascending: false), NSSortDescriptor(key: "dateAdded", ascending: false)] | ||||
|         if deck != nil { | ||||
|             request.predicate = NSPredicate(format: "deck == %@", deck!) | ||||
|             //                        flashcards = deck?.flashcards?.allObjects as? [Flashcard] ?? [] | ||||
|         } | ||||
|         do { | ||||
|             flashcards = try moc.fetch(request) | ||||
|             print(flashcards) | ||||
|         } catch { | ||||
|             print("Something went wrong while fetching the flashcards") | ||||
|             flashcards = [] | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
| #Preview { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user