@@ -23,6 +23,8 @@ class NotificationInstance {
2323 var meetingStartTime : Date ?
2424 weak var timerLabel : NSTextField ?
2525 weak var compactMessageLabel : NSTextField ?
26+ weak var stopCountdownLabel : NSTextField ?
27+ var stopCountdownTimer : Timer ?
2628
2729 init (
2830 payload: NotificationPayload , panel: NSPanel , clickableView: ClickableView , creationIndex: Int
@@ -60,6 +62,11 @@ class NotificationInstance {
6062 timerLabel = nil
6163 }
6264
65+ func bindStopCountdownLabel( _ label: NSTextField ) {
66+ stopCountdownLabel = label
67+ updateStopCountdownLabel ( remainingSeconds: payload. timeoutSeconds)
68+ }
69+
6370 func startScheduleUpdates( ) {
6471 guard let meetingStartTime else { return }
6572 updateScheduleLabels ( )
@@ -75,8 +82,11 @@ class NotificationInstance {
7582 func stopScheduleUpdates( ) {
7683 countdownTimer? . invalidate ( )
7784 countdownTimer = nil
85+ stopCountdownTimer? . invalidate ( )
86+ stopCountdownTimer = nil
7887 timerLabel = nil
7988 compactMessageLabel = nil
89+ stopCountdownLabel = nil
8090 }
8191
8292 private func updateScheduleLabels( ) {
@@ -110,6 +120,7 @@ class NotificationInstance {
110120 remainingDismissSeconds = timeoutSeconds
111121 dismissStartTime = Date ( )
112122 scheduleDismissTimer ( after: timeoutSeconds)
123+ startStopCountdownUpdates ( )
113124
114125 if let compactActionButton {
115126 compactActionButton. startProgress ( duration: timeoutSeconds)
@@ -125,6 +136,9 @@ class NotificationInstance {
125136 }
126137 dismissTimer? . invalidate ( )
127138 dismissTimer = nil
139+ stopCountdownTimer? . invalidate ( )
140+ stopCountdownTimer = nil
141+ updateStopCountdownLabel ( remainingSeconds: remainingDismissSeconds)
128142
129143 if let compactActionButton {
130144 compactActionButton. pauseProgress ( )
@@ -135,6 +149,7 @@ class NotificationInstance {
135149 guard timeoutSeconds > 0 , remainingDismissSeconds > 0 else { return }
136150 dismissStartTime = Date ( )
137151 scheduleDismissTimer ( after: remainingDismissSeconds)
152+ startStopCountdownUpdates ( )
138153
139154 if let compactActionButton {
140155 compactActionButton. resumeProgress ( )
@@ -148,6 +163,7 @@ class NotificationInstance {
148163 remainingDismissSeconds = timeoutSeconds
149164 dismissStartTime = Date ( )
150165 scheduleDismissTimer ( after: timeoutSeconds)
166+ startStopCountdownUpdates ( )
151167
152168 if let compactActionButton {
153169 compactActionButton. startProgress ( duration: timeoutSeconds)
@@ -157,6 +173,8 @@ class NotificationInstance {
157173 func dismiss( ) {
158174 dismissTimer? . invalidate ( )
159175 dismissTimer = nil
176+ stopCountdownTimer? . invalidate ( )
177+ stopCountdownTimer = nil
160178 dismissStartTime = nil
161179 remainingDismissSeconds = 0
162180 compactActionButton? . resetProgress ( )
@@ -191,8 +209,40 @@ class NotificationInstance {
191209 }
192210 }
193211
212+ func stopCountdownText( _ remainingSeconds: Double ) -> String {
213+ let seconds = max ( 0 , Int ( ceil ( remainingSeconds) ) )
214+ return " Anarlog will stop listening in \( seconds) seconds. "
215+ }
216+
217+ private func startStopCountdownUpdates( ) {
218+ guard stopCountdownLabel != nil else { return }
219+ updateStopCountdownLabel ( )
220+
221+ stopCountdownTimer? . invalidate ( )
222+ stopCountdownTimer = Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) {
223+ [ weak self] _ in
224+ self ? . updateStopCountdownLabel ( )
225+ }
226+ }
227+
228+ private func updateStopCountdownLabel( remainingSeconds: Double ? = nil ) {
229+ guard stopCountdownLabel != nil else { return }
230+
231+ let remaining : Double
232+ if let remainingSeconds {
233+ remaining = remainingSeconds
234+ } else if let dismissStartTime {
235+ remaining = max ( 0 , remainingDismissSeconds - Date( ) . timeIntervalSince ( dismissStartTime) )
236+ } else {
237+ remaining = remainingDismissSeconds
238+ }
239+
240+ stopCountdownLabel? . stringValue = stopCountdownText ( remaining)
241+ }
242+
194243 deinit {
195244 countdownTimer? . invalidate ( )
196245 dismissTimer? . invalidate ( )
246+ stopCountdownTimer? . invalidate ( )
197247 }
198248}
0 commit comments