From f45f7db10ba980a7d64a98988556bdb67bcdbe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 27 Jan 2026 02:05:41 +0100 Subject: [PATCH] implement editing --- MultiChrono/ContentView.swift | 25 ++++++++++-- MultiChrono/StopwatchDetailView.swift | 56 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 MultiChrono/StopwatchDetailView.swift diff --git a/MultiChrono/ContentView.swift b/MultiChrono/ContentView.swift index cff5da1..569554f 100644 --- a/MultiChrono/ContentView.swift +++ b/MultiChrono/ContentView.swift @@ -53,15 +53,21 @@ class ContentViewModel: ObservableObject { struct ContentView: View { @StateObject private var viewModel = ContentViewModel() @State private var isShowingAddSheet = false + @State private var selectedStopwatch: Stopwatch? @Environment(\.scenePhase) private var scenePhase var body: some View { NavigationStack { List { ForEach(viewModel.stopwatches) { stopwatch in - StopwatchRow(stopwatch: stopwatch, onDelete: { - viewModel.deleteStopwatch(id: stopwatch.id) - }) + Button { + selectedStopwatch = stopwatch + } label: { + StopwatchRow(stopwatch: stopwatch, onDelete: { + viewModel.deleteStopwatch(id: stopwatch.id) + }) + } + .buttonStyle(.plain) // Preserves the row layout and interactions } .onDelete { indexSet in for index in indexSet { @@ -86,6 +92,19 @@ struct ContentView: View { isShowingAddSheet = false } } + .sheet(item: $selectedStopwatch) { stopwatch in + StopwatchDetailView( + stopwatch: stopwatch, + onSave: { newName in + stopwatch.name = newName + viewModel.save() + selectedStopwatch = nil + }, + onCancel: { + selectedStopwatch = nil + } + ) + } .overlay { if viewModel.stopwatches.isEmpty { ContentUnavailableView( diff --git a/MultiChrono/StopwatchDetailView.swift b/MultiChrono/StopwatchDetailView.swift new file mode 100644 index 0000000..c22661f --- /dev/null +++ b/MultiChrono/StopwatchDetailView.swift @@ -0,0 +1,56 @@ +// +// StopwatchDetailView.swift +// MultiChrono +// +// Created by Beatrice DellacĂ  on 27/01/26. +// + +import SwiftUI + +struct StopwatchDetailView: View { + let stopwatch: Stopwatch + let onSave: (String) -> Void + let onCancel: () -> Void + + @State private var draftName: String + + init(stopwatch: Stopwatch, onSave: @escaping (String) -> Void, onCancel: @escaping () -> Void) { + self.stopwatch = stopwatch + self.onSave = onSave + self.onCancel = onCancel + _draftName = State(initialValue: stopwatch.name) + } + + var body: some View { + NavigationStack { + Form { + Section(header: Text("Name")) { + TextField("Enter name...", text: $draftName) + } + } + .navigationTitle("Edit Stopwatch") + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .cancellationAction) { + Button("Cancel") { + onCancel() + } + } + ToolbarItem(placement: .confirmationAction) { + Button("Save") { + onSave(draftName) + } + .disabled(draftName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) + } + } + } + } +} + +#Preview { + StopwatchDetailView( + stopwatch: Stopwatch(name: "Test Timer"), + onSave: { _ in }, + onCancel: {} + ) +}