Fixes live timers states

release
Laurent 3 years ago
parent 426f54cf08
commit 77e037ca18
  1. 30
      LeCountdown/Conductor.swift
  2. 7
      LeCountdown/Views/Components/GreenCheckmarkView.swift
  3. 5
      LeCountdown/Views/ContentView.swift
  4. 4
      LeCountdown/Views/HomeView.swift
  5. 23
      LeCountdown/Views/LiveTimerListView.swift

@ -26,6 +26,8 @@ class Conductor: ObservableObject {
self.currentStopwatches = Conductor.savedStopwatches
}
@Published var cancelledCountdowns: [String] = []
@Published var currentCountdowns: [String : DateInterval] = [:] {
didSet {
Conductor.savedCountdowns = currentCountdowns
@ -48,6 +50,7 @@ class Conductor: ObservableObject {
func removeLiveTimer(id: String) {
self.liveTimers.removeAll(where: { $0.id == id })
self.cancelledCountdowns.removeAll(where: { $0 == id })
}
fileprivate func _buildLiveTimers() {
@ -58,38 +61,24 @@ class Conductor: ObservableObject {
// add countdown if not present
for liveCountdown in liveCountdowns {
if let livetimer = self.liveTimers.first(where: { $0.id == liveCountdown.id }) {
self.liveTimers.replace([livetimer], with: [liveCountdown])
if let index = self.liveTimers.firstIndex(where: { $0.id == liveCountdown.id }) {
self.liveTimers.remove(at: index)
self.liveTimers.insert(liveCountdown, at: index)
} else {
self.liveTimers.append(liveCountdown)
}
}
// remove after
// for liveTimer in self.liveTimers {
// let id: String = liveTimer.id
// if liveCountdowns.first(where: { $0.id == id }) == nil {
// let timer = Timer.scheduledTimer(withTimeInterval: 8.0, repeats: false) { _ in
// self.removeLiveTimer(id: id)
// }
// self._cleanupTimers[id] = timer
// }
// }
let liveStopwatches = self.currentStopwatches.map {
return LiveTimer(id: $0, date: $1)
}
for liveStopwatch in liveStopwatches {
if let livetimer = self.liveTimers.first(where: { $0.id == liveStopwatch.id }) {
self.liveTimers.replace([livetimer], with: [liveStopwatch])
if let index = self.liveTimers.firstIndex(where: { $0.id == liveStopwatch.id }) {
self.liveTimers.remove(at: index)
self.liveTimers.insert(liveStopwatch, at: index)
} else {
self.liveTimers.append(liveStopwatch)
}
//
//
// if self.liveTimers.first(where: { $0.id == liveStopwatch.id }) == nil {
// self.liveTimers.append(liveStopwatch)
// }
}
}
@ -127,6 +116,7 @@ class Conductor: ObservableObject {
func cancelCountdown(id: String) {
CountdownScheduler.master.cancelCurrentNotifications(countdownId: id)
self.stopSoundIfPossible()
self.cancelledCountdowns.append(id)
self._endCountdown(countdownId: id, cancel: true)
}

@ -14,6 +14,13 @@ struct GreenCheckmarkView: View {
}
}
struct XMarkView: View {
var body: some View {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.red)
}
}
struct GreenCheckmarkView_Previews: PreviewProvider {
static var previews: some View {
GreenCheckmarkView()

@ -109,11 +109,6 @@ struct ContentView<T : AbstractTimer>: View {
.foregroundColor(.white)
.background(Color(white: 0.1))
.cornerRadius(32.0, corners: [.topRight, .topLeft])
// .padding(.bottom, 40.0)
// .cornerRadius(16.0, corners: [.topRight, .topLeft])
}
}
}

@ -41,7 +41,7 @@ struct CompactHomeView: View {
.tag(2)
}
}
.tabViewStyle(.page)
.tabViewStyle(.page(indexDisplayMode: .never))
.onAppear {
if self.timers.count > 0 {
self.tabSelection = 1
@ -79,7 +79,7 @@ struct RegularHomeView: View {
.tag(2)
}
}
.tabViewStyle(.page)
.tabViewStyle(.page(indexDisplayMode: .never))
.onOpenURL { _ in
self.tabSelection = 1
}

@ -136,15 +136,20 @@ struct LiveCountdownView: View {
VStack(alignment: .trailing) {
let running = self.date > context.date
let cancelled = conductor.cancelledCountdowns.contains(where: { $0 == self.countdown.stringId })
if running {
if cancelled {
TimeView(text: NSLocalizedString("Cancelled", comment: ""))
} else if running {
TimeView(text: self._formattedDuration(date: context.date))
} else {
TimeView(text: self.date.formatted(date: .omitted, time: .shortened))
}
// SeparatorView()
HStack {
if !running {
if cancelled {
XMarkView()
} else if !running {
GreenCheckmarkView()
}
Text(self.countdown.displayName.uppercased())
@ -156,8 +161,11 @@ struct LiveCountdownView: View {
.contentShape(Rectangle()) // make the onTap react everywhere
.onTapGesture {
withAnimation {
self._cancelCountdown()
// self._dismiss()
if conductor.currentCountdowns[self.countdown.stringId] != nil {
self._cancelCountdown()
} else {
self._dismiss()
}
}
}
.frame(height: liveViewSize)
@ -168,7 +176,7 @@ struct LiveCountdownView: View {
}
fileprivate func _dismiss() {
conductor.cancelCountdown(id: self.countdown.stringId)
// conductor.cancelCountdown(id: self.countdown.stringId)
conductor.removeLiveTimer(id: self.countdown.stringId)
}
@ -194,10 +202,11 @@ struct LiveTimerListView: View {
var body: some View {
ScrollView {
// ScrollView {
LazyVGrid(
columns: self._columns(),
alignment: .trailing,
spacing: 0.0
) {
@ -217,7 +226,7 @@ struct LiveTimerListView: View {
}
}
}
// }
}.padding()
}

Loading…
Cancel
Save