Fix snappy behavior when favoriting flashcard
This commit is contained in:
@@ -9,7 +9,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct FlashCardListRowView: View {
|
struct FlashCardListRowView: View {
|
||||||
@State var flashcard: Flashcard
|
@State var flashcard: Flashcard
|
||||||
@State private var refresh: UUID = UUID()
|
var refresh: () -> ()
|
||||||
var body: some View {
|
var body: some View {
|
||||||
HStack {
|
HStack {
|
||||||
Group {
|
Group {
|
||||||
@@ -31,9 +31,8 @@ struct FlashCardListRowView: View {
|
|||||||
} catch {
|
} catch {
|
||||||
print("Something went wrong while saving favorite cards, please try again")
|
print("Something went wrong while saving favorite cards, please try again")
|
||||||
}
|
}
|
||||||
refresh = UUID()
|
refresh()
|
||||||
}
|
}
|
||||||
.id(refresh)
|
|
||||||
.padding(.trailing)
|
.padding(.trailing)
|
||||||
VStack {
|
VStack {
|
||||||
Text(flashcard.name ?? "Unknown")
|
Text(flashcard.name ?? "Unknown")
|
||||||
@@ -52,12 +51,14 @@ struct FlashCardListRowView: View {
|
|||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first
|
let flashcard = try? DataController.preview.viewContext.fetch(Flashcard.fetchRequest()).first
|
||||||
|
func reloadPage() {
|
||||||
|
}
|
||||||
return Group {
|
return Group {
|
||||||
FlashCardListRowView(flashcard: flashcard!)
|
FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage)
|
||||||
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
||||||
FlashCardListRowView(flashcard: flashcard!)
|
FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage)
|
||||||
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
||||||
FlashCardListRowView(flashcard: flashcard!)
|
FlashCardListRowView(flashcard: flashcard!, refresh: reloadPage)
|
||||||
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,28 +15,33 @@ struct FlashCardListView: View {
|
|||||||
var deck: Deck?
|
var deck: Deck?
|
||||||
@Environment(\.managedObjectContext) var moc
|
@Environment(\.managedObjectContext) var moc
|
||||||
|
|
||||||
|
private struct AddButton: View {
|
||||||
|
@Binding var addFlashcard: Bool
|
||||||
|
var text: String
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeometryReader { geometry in
|
|
||||||
NavigationStack {
|
|
||||||
Group {
|
|
||||||
List {
|
|
||||||
Button(action: {
|
Button(action: {
|
||||||
self.addFlashcard = true
|
self.addFlashcard = true
|
||||||
}) {
|
}) {
|
||||||
VStack {
|
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemName: "plus.circle.fill")
|
Image(systemName: "plus.circle.fill")
|
||||||
.padding(.trailing)
|
.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)
|
.frame(maxHeight: geometry.size.height / 10)
|
||||||
}
|
|
||||||
ForEach(flashcards) { flashcard in
|
ForEach(flashcards) { flashcard in
|
||||||
NavigationLink {
|
NavigationLink {
|
||||||
FlashCardView(flashcard: flashcard, showDescription: $showDescription)
|
FlashCardView(flashcard: flashcard, showDescription: $showDescription)
|
||||||
} label: {
|
} label: {
|
||||||
FlashCardListRowView(flashcard: flashcard)
|
FlashCardListRowView(flashcard: flashcard, refresh: refreshFlashcards)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onDelete(perform: { offsets in
|
.onDelete(perform: { offsets in
|
||||||
@@ -54,7 +59,6 @@ struct FlashCardListView: View {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
.environment(\.defaultMinListRowHeight, geometry.size.height / 12)
|
.environment(\.defaultMinListRowHeight, geometry.size.height / 12)
|
||||||
}
|
|
||||||
.onAppear {
|
.onAppear {
|
||||||
refreshFlashcards()
|
refreshFlashcards()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user