From 77e037ca18dd20858e83f07a2834559563cfafcb Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 21 Feb 2023 09:15:04 +0100 Subject: [PATCH] Fixes live timers states --- LeCountdown/Conductor.swift | 30 +++++++------------ .../Views/Components/GreenCheckmarkView.swift | 7 +++++ LeCountdown/Views/ContentView.swift | 5 ---- LeCountdown/Views/HomeView.swift | 4 +-- LeCountdown/Views/LiveTimerListView.swift | 23 +++++++++----- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/LeCountdown/Conductor.swift b/LeCountdown/Conductor.swift index 3f2235c..d4fd04d 100644 --- a/LeCountdown/Conductor.swift +++ b/LeCountdown/Conductor.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) } diff --git a/LeCountdown/Views/Components/GreenCheckmarkView.swift b/LeCountdown/Views/Components/GreenCheckmarkView.swift index 6f41f92..5f6773a 100644 --- a/LeCountdown/Views/Components/GreenCheckmarkView.swift +++ b/LeCountdown/Views/Components/GreenCheckmarkView.swift @@ -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() diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index 7d70ea5..281eec3 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -109,11 +109,6 @@ struct ContentView: View { .foregroundColor(.white) .background(Color(white: 0.1)) .cornerRadius(32.0, corners: [.topRight, .topLeft]) - - - -// .padding(.bottom, 40.0) -// .cornerRadius(16.0, corners: [.topRight, .topLeft]) } } } diff --git a/LeCountdown/Views/HomeView.swift b/LeCountdown/Views/HomeView.swift index 733496c..f261df8 100644 --- a/LeCountdown/Views/HomeView.swift +++ b/LeCountdown/Views/HomeView.swift @@ -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 } diff --git a/LeCountdown/Views/LiveTimerListView.swift b/LeCountdown/Views/LiveTimerListView.swift index 01f04fa..b6d7c8b 100644 --- a/LeCountdown/Views/LiveTimerListView.swift +++ b/LeCountdown/Views/LiveTimerListView.swift @@ -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() }