implement alerts, delete

This commit is contained in:
2026-01-28 23:43:50 +01:00
parent a607e33ce9
commit 93cf6f8483
3 changed files with 51 additions and 9 deletions

View File

@@ -62,6 +62,7 @@ struct ContentView: View {
@State private var isShowingSettings = false
@State private var selectedStopwatch: Stopwatch?
@State private var draggingStopwatch: Stopwatch?
@State private var stopwatchToDelete: Stopwatch?
@Environment(\.scenePhase) private var scenePhase
var body: some View {
@@ -73,7 +74,8 @@ struct ContentView: View {
stopwatch: stopwatch,
viewModel: viewModel,
selectedStopwatch: $selectedStopwatch,
draggingStopwatch: $draggingStopwatch
draggingStopwatch: $draggingStopwatch,
stopwatchToDelete: $stopwatchToDelete
)
}
}
@@ -119,9 +121,26 @@ struct ContentView: View {
stopwatch.reset()
viewModel.save()
selectedStopwatch = nil
},
onDelete: {
viewModel.deleteStopwatch(id: stopwatch.id)
selectedStopwatch = nil
}
)
}
.alert("Are you sure you want to delete the Stopwatch \(stopwatchToDelete?.name ?? "")?", isPresented: Binding(
get: { stopwatchToDelete != nil },
set: { if !$0 { stopwatchToDelete = nil } }
)) {
if let stopwatch = stopwatchToDelete {
Button("Delete", role: .destructive) {
withAnimation {
viewModel.deleteStopwatch(id: stopwatch.id)
}
}
}
Button("Cancel", role: .cancel) {}
}
.overlay {
if viewModel.stopwatches.isEmpty {
ContentUnavailableView(
@@ -145,15 +164,14 @@ struct StopwatchListItem: View {
@ObservedObject var viewModel: ContentViewModel
@Binding var selectedStopwatch: Stopwatch?
@Binding var draggingStopwatch: Stopwatch?
@Binding var stopwatchToDelete: Stopwatch?
var body: some View {
Button {
selectedStopwatch = stopwatch
} label: {
StopwatchRow(stopwatch: stopwatch, onDelete: {
withAnimation {
viewModel.deleteStopwatch(id: stopwatch.id)
}
stopwatchToDelete = stopwatch
})
}
.buttonStyle(.plain)
@@ -163,9 +181,7 @@ struct StopwatchListItem: View {
.contentShape(Rectangle())
.contextMenu {
Button(role: .destructive) {
withAnimation {
viewModel.deleteStopwatch(id: stopwatch.id)
}
stopwatchToDelete = stopwatch
} label: {
Label("Delete", systemImage: "trash")
}