Implement timer deletion

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-11-02 10:14:49 -07:00
parent ed57f54cf9
commit 4443293cd0
2 changed files with 41 additions and 34 deletions

View file

@ -110,7 +110,7 @@ class TimerDataStore: ObservableObject {
UIApplication.shared.isIdleTimerDisabled = self.activeTimer?.isRunning ?? false
}
func updateTimer() {
private func updateTimer() {
guard let state = self.activeTimer else {
return
}
@ -223,7 +223,8 @@ class TimerDataStore: ObservableObject {
loadTimers()
}
func deleteTimer(_ timer: IntervalTimer) {
func deleteTimer(at: IndexSet) {
let timer = try! self.timers.get()[at.first!]
let viewContext = persistentContainer.viewContext
viewContext.delete(timer)
try! viewContext.save()

View file

@ -19,41 +19,50 @@ struct TimerListView: View {
return Text("Create a timer to get started")
.toAnyView()
} else {
return List(timers) { timer in
NavigationLink(
destination: ActiveTimerView()
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action: {
// TODO: Confirm before exiting if the timer isn't complete
self.dataStore.closeTimer()
}, label: { Image(systemName: "xmark") })
return List {
ForEach(timers) { timer in
NavigationLink(
destination: ActiveTimerView()
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action: {
// TODO: Confirm before exiting if the timer isn't complete
self.dataStore.closeTimer()
}, label: { Image(systemName: "xmark") })
.foregroundColor(.primary)
),
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)
),
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)
}
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)
}
}.toAnyView()
}
.navigationBarItems(
trailing: Button(action: {
self.isEditing = true
}, label: { Image(systemName: "plus").padding() } ))
.toAnyView()
}
default:
return Text("Loading...").toAnyView()
@ -64,9 +73,6 @@ struct TimerListView: View {
NavigationView {
stateContent
.navigationBarTitle("Timers")
.navigationBarItems(trailing: Button(action: {
self.isEditing = true
}, label: { Image(systemName: "plus").padding() } ))
}
.sheet(isPresented: $isEditing,
onDismiss: { self.isEditing = false },