Skip to content

Commit c3f98bc

Browse files
committed
Cancels clear notice task, after a new notice is presented
1 parent a6b00fc commit c3f98bc

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

WooCommerce/Classes/View Modifiers/View+NoticesModifier.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SwiftUI
22
import UIKit
3+
import Hardware
34

45
/// View Modifier that shows a notice in front of a view.
56
/// NOTE: This currently does not support enqueuing multiple notices like `DefaultNoticePresenter` does.
@@ -10,6 +11,14 @@ struct NoticeModifier: ViewModifier {
1011
///
1112
@Binding var notice: Notice?
1213

14+
/// Cancelable task that clears a notice.
15+
///
16+
@State var clearNoticeTask = DispatchWorkItem(block: {})
17+
18+
/// Time the notice will remain on screen.
19+
///
20+
private let onScreenNoticeTime = 5.0
21+
1322
func body(content: Content) -> some View {
1423
content
1524
.overlay(buildNoticeStack())
@@ -30,13 +39,12 @@ struct NoticeModifier: ViewModifier {
3039
// NoticeView wrapper
3140
NoticeAlert(notice: notice, width: geometry.size.width)
3241
.onDismiss {
33-
$notice.wrappedValue = nil
42+
clearNoticeTask.perform()
43+
clearNoticeTask.cancel()
3444
}
3545
.onAppear {
36-
// TODO: Move this to a proper state management class
37-
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
38-
//$notice.wrappedValue = nil
39-
}
46+
clearNoticeTask = .init { $notice.wrappedValue = nil }
47+
DispatchQueue.main.asyncAfter(deadline: .now() + onScreenNoticeTime, execute: clearNoticeTask)
4048
}
4149

4250
.fixedSize(horizontal: false, vertical: true)

0 commit comments

Comments
 (0)