interval-timer-ios/IntervalTimer/TimerListView.swift
William Brawner 2f5e71c304 Implement timer editing
Signed-off-by: William Brawner <me@wbrawner.com>
2020-11-04 10:21:40 -07:00

81 lines
2.9 KiB
Swift

//
// ContentView.swift
// IntervalTimer
//
// Created by William Brawner on 10/22/20.
// Copyright © 2020 William Brawner. All rights reserved.
//
import SwiftUI
struct TimerListView: View {
@State var isEditing: Bool = false
@EnvironmentObject var dataStore: TimerDataStore
var stateContent: AnyView {
switch dataStore.timers {
case .success(let timers):
if timers.count == 0 {
return Text("Create a timer to get started")
.toAnyView()
} else {
return List {
ForEach(timers) { timer in
NavigationLink(
destination: ActiveTimerView(),
isActive: .constant(self.dataStore.hasActiveTimer)
) {
HStack {
VStack(alignment: .leading) {
Text(timer.name)
.lineLimit(1)
if timer.userDescription?.count ?? 0 > 0 {
Text(timer.userDescription!)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(1)
}
}
Spacer()
Text(timer.totalDuration.toDurationString())
.font(Font.subheadline.monospacedDigit())
.foregroundColor(.secondary)
}
.frame(minHeight: 50)
.contentShape(Rectangle())
.onTapGesture {
self.dataStore.openTimer(timer)
}
}
}.onDelete { index in
self.dataStore.deleteTimer(at: index)
}
}
.listStyle(InsetGroupedListStyle())
.toAnyView()
}
default:
return Text("Loading...").toAnyView()
}
}
var body: some View {
NavigationView {
stateContent
.navigationBarTitle("Timers")
.navigationBarItems(
trailing: Button(action: {
self.isEditing = true
}, label: { Image(systemName: "plus").padding() } ))
}
.sheet(isPresented: $isEditing,
onDismiss: { self.isEditing = false },
content: { TimerFormView()})
}
}
struct TimerListView_Previews: PreviewProvider {
static var previews: some View {
TimerListView().environmentObject(TimerDataStore() {})
}
}