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] ForEach(flashcards) { flashcard in
moc.delete(flashcard) NavigationLink {
flashcards.remove(at: index) 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 { do {
try moc.save() try moc.save()
} catch { } catch {
print("Something went wrong while deleting an object") print("Something went wrong while deleting an object")
} }
}) })
}
} else {
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)
} }
.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")
}
} }
} }
} }