implement multi stopwatch

This commit is contained in:
2026-01-27 01:44:33 +01:00
parent 4291099fba
commit a3b73e101b
5 changed files with 243 additions and 6 deletions

View File

@@ -6,16 +6,73 @@
//
import SwiftUI
import Combine
class ContentViewModel: ObservableObject {
@Published var stopwatches: [Stopwatch] = []
func addStopwatch(name: String) {
let newStopwatch = Stopwatch(name: name)
stopwatches.append(newStopwatch)
}
func deleteStopwatch(at index: Int) {
stopwatches[index].pause() // Ensure timer is stopped
stopwatches.remove(at: index)
}
func deleteStopwatch(id: UUID) {
if let index = stopwatches.firstIndex(where: { $0.id == id }) {
deleteStopwatch(at: index)
}
}
}
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
@State private var isShowingAddSheet = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
NavigationStack {
List {
ForEach(viewModel.stopwatches) { stopwatch in
StopwatchRow(stopwatch: stopwatch, onDelete: {
viewModel.deleteStopwatch(id: stopwatch.id)
})
}
.onDelete { indexSet in
for index in indexSet {
viewModel.deleteStopwatch(at: index)
}
}
}
.listStyle(.plain)
.navigationTitle("MultiChrono")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
isShowingAddSheet = true
}) {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $isShowingAddSheet) {
AddStopwatchView { name in
viewModel.addStopwatch(name: name)
isShowingAddSheet = false
}
}
.overlay {
if viewModel.stopwatches.isEmpty {
ContentUnavailableView(
"No Stopwatches",
systemImage: "timer",
description: Text("Tap the + button to create a stopwatch.")
)
}
}
}
.padding()
}
}