implement lap edit

This commit is contained in:
2026-02-05 00:54:58 +01:00
parent 93cf6f8483
commit 190cfc4071
3 changed files with 234 additions and 33 deletions

View File

@@ -0,0 +1,165 @@
//
// EditLapView.swift
// MultiChrono
//
// Created by Beatrice Dellacà on 27/01/26.
//
import SwiftUI
struct EditLapView: View {
@Environment(\.dismiss) var dismiss
@State var lap: Stopwatch.Lap
var onSave: (Stopwatch.Lap) -> Void
var body: some View {
NavigationStack {
Form {
Section(header: Text("Start Time")) {
PreciseTimePicker(date: $lap.startTime)
}
Section(header: Text("End Time")) {
PreciseTimePicker(date: $lap.endTime)
}
Section(header: Text("Duration")) {
HStack {
Text("Duration")
Spacer()
Text(Stopwatch.format(interval: lap.endTime.timeIntervalSince(lap.startTime), format: AppSettings.shared.timeFormat))
.foregroundStyle(.secondary)
}
}
}
.navigationTitle("Edit Lap \(lap.number)")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
onSave(lap)
dismiss()
}
}
}
}
}
}
struct PreciseTimePicker: View {
@Binding var date: Date
var body: some View {
VStack(spacing: 12) {
DatePicker("Date & Time", selection: $date, displayedComponents: [.hourAndMinute, .date])
HStack(spacing: 0) {
Text("Sec")
.font(.caption)
.frame(width: 40)
Picker("Seconds", selection: Binding(
get: { Calendar.current.component(.second, from: date) },
set: { newSecond in
if let newDate = Calendar.current.date(bySetting: .second, value: newSecond, of: date) {
date = newDate
}
}
)) {
ForEach(0..<60) {
Text(String(format: "%02d", $0)).tag($0)
}
}
.pickerStyle(.wheel)
.frame(height: 100)
.clipped()
Spacer()
.frame(width: 20)
Text("ms")
.font(.caption)
.frame(width: 30)
// Milliseconds - Hundreds
Picker("Hundreds", selection: Binding(
get: { (Calendar.current.component(.nanosecond, from: date) / 1_000_000) / 100 },
set: { newHundreds in
let currentMillis = Calendar.current.component(.nanosecond, from: date) / 1_000_000
let tensAndUnits = currentMillis % 100
let totalMillis = (newHundreds * 100) + tensAndUnits
if let newDate = Calendar.current.date(bySetting: .nanosecond, value: totalMillis * 1_000_000, of: date) {
date = newDate
}
}
)) {
ForEach(0..<10) {
Text("\($0)").tag($0)
}
}
.pickerStyle(.wheel)
.frame(width: 40, height: 100)
.clipped()
// Milliseconds - Tens
Picker("Tens", selection: Binding(
get: { ((Calendar.current.component(.nanosecond, from: date) / 1_000_000) % 100) / 10 },
set: { newTens in
let currentMillis = Calendar.current.component(.nanosecond, from: date) / 1_000_000
let hundreds = currentMillis / 100
let units = currentMillis % 10
let totalMillis = (hundreds * 100) + (newTens * 10) + units
if let newDate = Calendar.current.date(bySetting: .nanosecond, value: totalMillis * 1_000_000, of: date) {
date = newDate
}
}
)) {
ForEach(0..<10) {
Text("\($0)").tag($0)
}
}
.pickerStyle(.wheel)
.frame(width: 40, height: 100)
.clipped()
// Milliseconds - Units
Picker("Units", selection: Binding(
get: { (Calendar.current.component(.nanosecond, from: date) / 1_000_000) % 10 },
set: { newUnits in
let currentMillis = Calendar.current.component(.nanosecond, from: date) / 1_000_000
let hundredsAndTens = (currentMillis / 10) * 10
let totalMillis = hundredsAndTens + newUnits
if let newDate = Calendar.current.date(bySetting: .nanosecond, value: totalMillis * 1_000_000, of: date) {
date = newDate
}
}
)) {
ForEach(0..<10) {
Text("\($0)").tag($0)
}
}
.pickerStyle(.wheel)
.frame(width: 40, height: 100)
.clipped()
}
}
}
}
#Preview {
EditLapView(
lap: Stopwatch.Lap(
id: UUID(),
number: 1,
startTime: Date(),
endTime: Date().addingTimeInterval(65),
duration: 65
),
onSave: { _ in }
)
}