From df267ebc4e951514c74a953565a40681f4a67a1e Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Fri, 3 May 2024 22:37:10 +0200 Subject: [PATCH] Fix snappy behavior when favoriting flashcard --- .../Flashcards/FlashCardListRowView.swift | 13 ++-- .../Views/Flashcards/FlashCardListView.swift | 72 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/WordAX/Views/Flashcards/FlashCardListRowView.swift b/WordAX/Views/Flashcards/FlashCardListRowView.swift index 024528a..d28f96c 100644 --- a/WordAX/Views/Flashcards/FlashCardListRowView.swift +++ b/WordAX/Views/Flashcards/FlashCardListRowView.swift @@ -9,7 +9,7 @@ import SwiftUI struct FlashCardListRowView: View { @State var flashcard: Flashcard - @State private var refresh: UUID = UUID() + var refresh: () -> () var body: some View { HStack { Group { @@ -31,9 +31,8 @@ struct FlashCardListRowView: View { } catch { print("Something went wrong while saving favorite cards, please try again") } - refresh = UUID() + refresh() } - .id(refresh) .padding(.trailing) VStack { Text(flashcard.name ?? "Unknown") @@ -52,12 +51,14 @@ struct FlashCardListRowView: View { #Preview { let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first + func reloadPage() { + } return Group { - FlashCardListRowView(flashcard: flashcard!) + FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage) .environment(\.managedObjectContext, DataController.preview.container.viewContext) - FlashCardListRowView(flashcard: flashcard!) + FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage) .environment(\.managedObjectContext, DataController.preview.container.viewContext) - FlashCardListRowView(flashcard: flashcard!) + FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage) .environment(\.managedObjectContext, DataController.preview.container.viewContext) } } diff --git a/WordAX/Views/Flashcards/FlashCardListView.swift b/WordAX/Views/Flashcards/FlashCardListView.swift index 3636ad2..e8a1d8e 100644 --- a/WordAX/Views/Flashcards/FlashCardListView.swift +++ b/WordAX/Views/Flashcards/FlashCardListView.swift @@ -15,46 +15,50 @@ struct FlashCardListView: View { var deck: Deck? @Environment(\.managedObjectContext) var moc + private struct AddButton: View { + @Binding var addFlashcard: Bool + var text: String + var body: some View { + Button(action: { + self.addFlashcard = true + }) { + HStack { + Image(systemName: "plus.circle.fill") + .padding(.trailing) + Text(text) + } + } + } + } + var body: some View { GeometryReader { geometry in NavigationStack { - Group { - List { - Button(action: { - self.addFlashcard = true - }) { - VStack { - HStack { - Image(systemName: "plus.circle.fill") - .padding(.trailing) - Text("Add new Flashcard") - } - } - .frame(maxHeight: geometry.size.height / 10) + List { + AddButton(addFlashcard: $addFlashcard, text: "Add new Flashcard") + .frame(maxHeight: geometry.size.height / 10) + ForEach(flashcards) { flashcard in + NavigationLink { + FlashCardView(flashcard: flashcard, showDescription: $showDescription) + } label: { + FlashCardListRowView(flashcard: flashcard, refresh: refreshFlashcards) } - ForEach(flashcards) { flashcard in - NavigationLink { - FlashCardView(flashcard: flashcard, showDescription: $showDescription) - } label: { - FlashCardListRowView(flashcard: flashcard) - } - } - .onDelete(perform: { offsets in - for index in offsets { - let flashcard = flashcards[index] - moc.delete(flashcard) - flashcards.remove(at: index) - } - - do { - try moc.save() - } catch { - print("Something went wrong while deleting an object") - } - }) } - .environment(\.defaultMinListRowHeight, geometry.size.height / 12) + .onDelete(perform: { offsets in + for index in offsets { + let flashcard = flashcards[index] + moc.delete(flashcard) + flashcards.remove(at: index) + } + + do { + try moc.save() + } catch { + print("Something went wrong while deleting an object") + } + }) } + .environment(\.defaultMinListRowHeight, geometry.size.height / 12) .onAppear { refreshFlashcards() }