Add add button to list view for flashcards and edit to toolbar

This commit is contained in:
2024-05-03 22:20:41 +02:00
parent ecdea1de2f
commit 5a5646648f

View File

@@ -19,39 +19,41 @@ struct FlashCardListView: View {
GeometryReader { geometry in GeometryReader { geometry in
NavigationStack { NavigationStack {
Group { Group {
if !flashcards.isEmpty { List {
List { Button(action: {
ForEach(flashcards) { flashcard in self.addFlashcard = true
NavigationLink { }) {
FlashCardView(flashcard: flashcard, showDescription: $showDescription) VStack {
} label: { HStack {
FlashCardListRowView(flashcard: flashcard) Image(systemName: "plus.circle.fill")
.padding(.trailing)
Text("Add new Flashcard")
} }
} }
.onDelete(perform: { offsets in .frame(maxHeight: geometry.size.height / 10)
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")
}
})
} }
} else { ForEach(flashcards) { flashcard in
Group { NavigationLink {
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)") FlashCardView(flashcard: flashcard, showDescription: $showDescription)
.padding() } label: {
.background(.purple) FlashCardListRowView(flashcard: flashcard)
.clipShape(.buttonBorder) }
} }
.frame(maxHeight: .infinity) .onDelete(perform: { offsets in
.padding(.horizontal) 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 { .onAppear {
refreshFlashcards() refreshFlashcards()
@@ -64,11 +66,7 @@ struct FlashCardListView: View {
.navigationBarTitle(deck?.name ?? "All Flashcards", displayMode: deck != nil ? .inline : .automatic) .navigationBarTitle(deck?.name ?? "All Flashcards", displayMode: deck != nil ? .inline : .automatic)
.toolbar { .toolbar {
ToolbarItemGroup(placement: .topBarTrailing) { ToolbarItemGroup(placement: .topBarTrailing) {
Button(action: { EditButton()
self.addFlashcard = true
}) {
Image(systemName: "plus")
}
} }
} }
} }
@@ -93,7 +91,7 @@ struct FlashCardListView: View {
flashcards = [] flashcards = []
} }
} }
} }
#Preview { #Preview {