implement laps, reset
This commit is contained in:
@@ -8,16 +8,25 @@
|
||||
import SwiftUI
|
||||
|
||||
struct StopwatchDetailView: View {
|
||||
let stopwatch: Stopwatch
|
||||
@ObservedObject var stopwatch: Stopwatch
|
||||
let onSave: (String) -> Void
|
||||
let onCancel: () -> Void
|
||||
let onReset: () -> Void
|
||||
|
||||
@State private var draftName: String
|
||||
@State private var isShowingResetAlert = false
|
||||
|
||||
init(stopwatch: Stopwatch, onSave: @escaping (String) -> Void, onCancel: @escaping () -> Void) {
|
||||
private let dateFormatter: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
|
||||
return formatter
|
||||
}()
|
||||
|
||||
init(stopwatch: Stopwatch, onSave: @escaping (String) -> Void, onCancel: @escaping () -> Void, onReset: @escaping () -> Void) {
|
||||
self.stopwatch = stopwatch
|
||||
self.onSave = onSave
|
||||
self.onCancel = onCancel
|
||||
self.onReset = onReset
|
||||
_draftName = State(initialValue: stopwatch.name)
|
||||
}
|
||||
|
||||
@@ -27,9 +36,91 @@ struct StopwatchDetailView: View {
|
||||
Section(header: Text("Name")) {
|
||||
TextField("Enter name...", text: $draftName)
|
||||
}
|
||||
|
||||
Section(header: Text("Laps")) {
|
||||
if stopwatch.isRunning,
|
||||
let currentStart = stopwatch.currentRunStartTime,
|
||||
let currentDuration = stopwatch.currentRunDuration {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack {
|
||||
Text("Lap \(stopwatch.laps.count + 1)")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.blue)
|
||||
Spacer()
|
||||
Text(Stopwatch.format(interval: currentDuration, format: AppSettings.shared.timeFormat))
|
||||
.font(.system(.body, design: .monospaced))
|
||||
.bold()
|
||||
.foregroundStyle(.blue)
|
||||
}
|
||||
|
||||
HStack {
|
||||
Text("Current")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.blue)
|
||||
Spacer()
|
||||
VStack(alignment: .trailing) {
|
||||
Text(dateFormatter.string(from: currentStart))
|
||||
Text("Running...")
|
||||
}
|
||||
}
|
||||
.font(.system(.caption, design: .monospaced))
|
||||
.foregroundStyle(.blue)
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
}
|
||||
|
||||
if stopwatch.laps.isEmpty && !stopwatch.isRunning {
|
||||
Text("No laps recorded yet.")
|
||||
.foregroundStyle(.secondary)
|
||||
} else {
|
||||
List {
|
||||
ForEach(stopwatch.laps) { lap in
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack {
|
||||
Text("Lap \(lap.number)")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
Text(Stopwatch.format(interval: lap.duration, format: AppSettings.shared.timeFormat))
|
||||
.font(.system(.body, design: .monospaced))
|
||||
.bold()
|
||||
}
|
||||
|
||||
HStack {
|
||||
Spacer()
|
||||
VStack(alignment: .trailing) {
|
||||
Text(dateFormatter.string(from: lap.startTime))
|
||||
Text(dateFormatter.string(from: lap.endTime))
|
||||
}
|
||||
}
|
||||
.font(.system(.caption, design: .monospaced))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(role: .destructive) {
|
||||
isShowingResetAlert = true
|
||||
} label: {
|
||||
HStack {
|
||||
Spacer()
|
||||
Text("Reset Stopwatch")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Edit Stopwatch")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.alert("Are you sure you want to reset the Stopwatch \(stopwatch.name)?", isPresented: $isShowingResetAlert) {
|
||||
Button("Reset", role: .destructive) {
|
||||
onReset()
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel") {
|
||||
@@ -51,6 +142,7 @@ struct StopwatchDetailView: View {
|
||||
StopwatchDetailView(
|
||||
stopwatch: Stopwatch(name: "Test Timer"),
|
||||
onSave: { _ in },
|
||||
onCancel: {}
|
||||
onCancel: {},
|
||||
onReset: {}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user