Skip to content

Commit 7048a0e

Browse files
committed
Update NoticeWrapper so the underlying notice view can be updated
1 parent c3f98bc commit 7048a0e

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,42 @@ struct NoticeModifier: ViewModifier {
3939
// NoticeView wrapper
4040
NoticeAlert(notice: notice, width: geometry.size.width)
4141
.onDismiss {
42-
clearNoticeTask.perform()
43-
clearNoticeTask.cancel()
42+
print("on dismiss")
43+
performClearNoticeTask()
44+
}
45+
.onChange(of: notice) { _ in
46+
print("on change")
47+
dispatchClearNoticeTask()
4448
}
4549
.onAppear {
46-
clearNoticeTask = .init { $notice.wrappedValue = nil }
47-
DispatchQueue.main.asyncAfter(deadline: .now() + onScreenNoticeTime, execute: clearNoticeTask)
50+
print("on appear")
51+
dispatchClearNoticeTask()
4852
}
4953

5054
.fixedSize(horizontal: false, vertical: true)
5155
}
5256
}
5357
}
5458
}
59+
60+
/// Cancels any ongoing clear notice task and dispatches it again.
61+
///
62+
private func dispatchClearNoticeTask() {
63+
clearNoticeTask.cancel()
64+
clearNoticeTask = .init {
65+
$notice.wrappedValue = nil
66+
print("clear was performed")
67+
}
68+
print("on Dispatch")
69+
DispatchQueue.main.asyncAfter(deadline: .now() + onScreenNoticeTime, execute: clearNoticeTask)
70+
}
71+
72+
/// Synchronously performs the clear notice task and cancels it to prever any future execution.
73+
///
74+
private func performClearNoticeTask() {
75+
clearNoticeTask.perform()
76+
clearNoticeTask.cancel()
77+
}
5578
}
5679

5780
// MARK: Custom Views
@@ -60,7 +83,7 @@ struct NoticeModifier: ViewModifier {
6083
///
6184
private struct NoticeAlert: UIViewRepresentable {
6285

63-
/// Notice object render
86+
/// Notice object to render.
6487
///
6588
let notice: Notice
6689

@@ -74,14 +97,14 @@ private struct NoticeAlert: UIViewRepresentable {
7497

7598
func makeUIView(context: Context) -> NoticeWrapper {
7699
let noticeView = NoticeView(notice: notice)
77-
noticeView.dismissHandler = onDismiss
78-
79100
let wrapperView = NoticeWrapper(noticeView: noticeView)
80101
wrapperView.translatesAutoresizingMaskIntoConstraints = false
81102
return wrapperView
82103
}
83104

84105
func updateUIView(_ uiView: NoticeWrapper, context: Context) {
106+
uiView.noticeView = NoticeView(notice: notice)
107+
uiView.noticeView.dismissHandler = onDismiss
85108
uiView.width = width
86109
}
87110

@@ -96,12 +119,17 @@ private struct NoticeAlert: UIViewRepresentable {
96119

97120

98121
private extension NoticeAlert {
99-
/// Wrapper type to force the underlying `NoticeView` to fixed width
122+
/// Wrapper type to force the underlying `NoticeView` to have a fixed width.
100123
///
101124
class NoticeWrapper: UIView {
102125
/// Underlying notice view
103126
///
104-
let noticeView: NoticeView
127+
var noticeView: NoticeView {
128+
didSet {
129+
oldValue.removeFromSuperview()
130+
setUpNoticeLayout()
131+
}
132+
}
105133

106134
/// Fixed width constraint.
107135
///
@@ -123,13 +151,24 @@ private extension NoticeAlert {
123151
self.noticeView = noticeView
124152
super.init(frame: .zero)
125153

154+
setUpNoticeLayout()
155+
createWidthConstraint()
156+
}
157+
158+
/// Set ups the notice layout.
159+
///
160+
private func setUpNoticeLayout() {
126161
// Add notice view to edges
127162
noticeView.translatesAutoresizingMaskIntoConstraints = false
128163
addSubview(noticeView)
129164

130165
layoutMargins = defaultInsets
131166
pinSubviewToAllEdgeMargins(noticeView)
167+
}
132168

169+
/// Forces the wrapper view to have a fixed width.
170+
///
171+
private func createWidthConstraint() {
133172
noticeViewWidthConstraint = widthAnchor.constraint(equalToConstant: width)
134173
noticeViewWidthConstraint.isActive = true
135174
}

0 commit comments

Comments
 (0)