2020-11-01 18:54:19 +00:00
|
|
|
//
|
|
|
|
// 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 {
|
2020-11-02 17:14:49 +00:00
|
|
|
return List {
|
|
|
|
ForEach(timers) { timer in
|
|
|
|
NavigationLink(
|
2020-11-04 17:21:40 +00:00
|
|
|
destination: ActiveTimerView(),
|
2020-11-02 17:14:49 +00:00
|
|
|
isActive: .constant(self.dataStore.hasActiveTimer)
|
|
|
|
) {
|
|
|
|
HStack {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text(timer.name)
|
2020-11-01 18:54:19 +00:00
|
|
|
.lineLimit(1)
|
2020-11-06 01:51:51 +00:00
|
|
|
if timer.description?.count ?? 0 > 0 {
|
|
|
|
Text(timer.description!)
|
2020-11-02 17:14:49 +00:00
|
|
|
.font(.subheadline)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.lineLimit(1)
|
|
|
|
}
|
2020-11-01 18:54:19 +00:00
|
|
|
}
|
2020-11-02 17:14:49 +00:00
|
|
|
Spacer()
|
|
|
|
Text(timer.totalDuration.toDurationString())
|
|
|
|
.font(Font.subheadline.monospacedDigit())
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
.frame(minHeight: 50)
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
.onTapGesture {
|
|
|
|
self.dataStore.openTimer(timer)
|
2020-11-01 18:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 17:14:49 +00:00
|
|
|
}.onDelete { index in
|
|
|
|
self.dataStore.deleteTimer(at: index)
|
2020-11-01 18:54:19 +00:00
|
|
|
}
|
2020-11-02 17:14:49 +00:00
|
|
|
}
|
2020-11-04 17:21:40 +00:00
|
|
|
.listStyle(InsetGroupedListStyle())
|
2020-11-02 17:14:49 +00:00
|
|
|
.toAnyView()
|
2020-11-01 18:54:19 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Text("Loading...").toAnyView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
|
|
|
stateContent
|
|
|
|
.navigationBarTitle("Timers")
|
2020-11-03 14:59:59 +00:00
|
|
|
.navigationBarItems(
|
|
|
|
trailing: Button(action: {
|
|
|
|
self.isEditing = true
|
|
|
|
}, label: { Image(systemName: "plus").padding() } ))
|
2020-11-01 18:54:19 +00:00
|
|
|
}
|
2020-11-06 18:02:02 +00:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
2020-11-01 18:54:19 +00:00
|
|
|
.sheet(isPresented: $isEditing,
|
|
|
|
onDismiss: { self.isEditing = false },
|
|
|
|
content: { TimerFormView()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TimerListView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
TimerListView().environmentObject(TimerDataStore() {})
|
|
|
|
}
|
|
|
|
}
|