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. 21
      LeCountdown/Views/LiveTimerListView.swift

@ -26,6 +26,8 @@ class Conductor: ObservableObject {
self.currentStopwatches = Conductor.savedStopwatches self.currentStopwatches = Conductor.savedStopwatches
} }
@Published var cancelledCountdowns: [String] = []
@Published var currentCountdowns: [String : DateInterval] = [:] { @Published var currentCountdowns: [String : DateInterval] = [:] {
didSet { didSet {
Conductor.savedCountdowns = currentCountdowns Conductor.savedCountdowns = currentCountdowns
@ -48,6 +50,7 @@ class Conductor: ObservableObject {
func removeLiveTimer(id: String) { func removeLiveTimer(id: String) {
self.liveTimers.removeAll(where: { $0.id == id }) self.liveTimers.removeAll(where: { $0.id == id })
self.cancelledCountdowns.removeAll(where: { $0 == id })
} }
fileprivate func _buildLiveTimers() { fileprivate func _buildLiveTimers() {
@ -58,38 +61,24 @@ class Conductor: ObservableObject {
// add countdown if not present // add countdown if not present
for liveCountdown in liveCountdowns { for liveCountdown in liveCountdowns {
if let livetimer = self.liveTimers.first(where: { $0.id == liveCountdown.id }) { if let index = self.liveTimers.firstIndex(where: { $0.id == liveCountdown.id }) {
self.liveTimers.replace([livetimer], with: [liveCountdown]) self.liveTimers.remove(at: index)
self.liveTimers.insert(liveCountdown, at: index)
} else { } else {
self.liveTimers.append(liveCountdown) 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 { let liveStopwatches = self.currentStopwatches.map {
return LiveTimer(id: $0, date: $1) return LiveTimer(id: $0, date: $1)
} }
for liveStopwatch in liveStopwatches { for liveStopwatch in liveStopwatches {
if let livetimer = self.liveTimers.first(where: { $0.id == liveStopwatch.id }) { if let index = self.liveTimers.firstIndex(where: { $0.id == liveStopwatch.id }) {
self.liveTimers.replace([livetimer], with: [liveStopwatch]) self.liveTimers.remove(at: index)
self.liveTimers.insert(liveStopwatch, at: index)
} else { } else {
self.liveTimers.append(liveStopwatch) 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) { func cancelCountdown(id: String) {
CountdownScheduler.master.cancelCurrentNotifications(countdownId: id) CountdownScheduler.master.cancelCurrentNotifications(countdownId: id)
self.stopSoundIfPossible() self.stopSoundIfPossible()
self.cancelledCountdowns.append(id)
self._endCountdown(countdownId: id, cancel: true) 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 { struct GreenCheckmarkView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
GreenCheckmarkView() GreenCheckmarkView()

@ -109,11 +109,6 @@ struct ContentView<T : AbstractTimer>: View {
.foregroundColor(.white) .foregroundColor(.white)
.background(Color(white: 0.1)) .background(Color(white: 0.1))
.cornerRadius(32.0, corners: [.topRight, .topLeft]) .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) .tag(2)
} }
} }
.tabViewStyle(.page) .tabViewStyle(.page(indexDisplayMode: .never))
.onAppear { .onAppear {
if self.timers.count > 0 { if self.timers.count > 0 {
self.tabSelection = 1 self.tabSelection = 1
@ -79,7 +79,7 @@ struct RegularHomeView: View {
.tag(2) .tag(2)
} }
} }
.tabViewStyle(.page) .tabViewStyle(.page(indexDisplayMode: .never))
.onOpenURL { _ in .onOpenURL { _ in
self.tabSelection = 1 self.tabSelection = 1
} }

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

Loading…
Cancel
Save