Fix snappy behavior when favoriting flashcard

This commit is contained in:
2024-05-03 22:37:10 +02:00
parent 5a5646648f
commit df267ebc4e
2 changed files with 45 additions and 40 deletions

View File

@@ -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)
}
}

View File

@@ -15,28 +15,33 @@ 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 {
GeometryReader { geometry in
NavigationStack {
Group {
List {
Button(action: {
self.addFlashcard = true
}) {
VStack {
HStack {
Image(systemName: "plus.circle.fill")
.padding(.trailing)
Text("Add new Flashcard")
Text(text)
}
}
}
}
var body: some View {
GeometryReader { geometry in
NavigationStack {
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)
FlashCardListRowView(flashcard: flashcard, refresh: refreshFlashcards)
}
}
.onDelete(perform: { offsets in
@@ -54,7 +59,6 @@ struct FlashCardListView: View {
})
}
.environment(\.defaultMinListRowHeight, geometry.size.height / 12)
}
.onAppear {
refreshFlashcards()
}