Skip to content

Commit 4e1c563

Browse files
committed
Make sure notice works correctly on iPad
1 parent 97d98be commit 4e1c563

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import SwiftUI
22
import UIKit
33

44
/// View Modifier that shows a notice in front of a view.
5-
/// NOTE: This currently does not support.
5+
///
6+
/// NOTE: This currently does not support:
67
/// - Enqueuing multiple notices like `DefaultNoticePresenter` does.
78
/// - Presenting foreground system notifications.
89
///
910
struct NoticeModifier: ViewModifier {
10-
11-
/// Notice object to render
11+
/// Notice object to render.
1212
///
1313
@Binding var notice: Notice?
1414

@@ -24,6 +24,10 @@ struct NoticeModifier: ViewModifier {
2424
///
2525
private let feedbackGenerator = UINotificationFeedbackGenerator()
2626

27+
/// Current horizontal size class.
28+
///
29+
@Environment(\.horizontalSizeClass) var horizontalSizeClass
30+
2731
func body(content: Content) -> some View {
2832
content
2933
.overlay(buildNoticeStack())
@@ -42,7 +46,7 @@ struct NoticeModifier: ViewModifier {
4246
Spacer()
4347

4448
// NoticeView wrapper
45-
NoticeAlert(notice: notice, width: geometry.size.width)
49+
NoticeAlert(notice: notice, width: preferredSizeClassWidth(geometry))
4650
.onDismiss {
4751
performClearNoticeTask()
4852
}
@@ -54,9 +58,9 @@ struct NoticeModifier: ViewModifier {
5458
provideHapticFeedbackIfNecessary(notice.feedbackType)
5559
dispatchClearNoticeTask()
5660
}
57-
58-
.fixedSize(horizontal: false, vertical: true)
61+
.fixedSize()
5962
}
63+
.frame(width: geometry.size.width) // Force a full container width so the notice is always centered.
6064
}
6165
}
6266
}
@@ -71,7 +75,7 @@ struct NoticeModifier: ViewModifier {
7175
DispatchQueue.main.asyncAfter(deadline: .now() + onScreenNoticeTime, execute: clearNoticeTask)
7276
}
7377

74-
/// Synchronously performs the clear notice task and cancels it to prever any future execution.
78+
/// Synchronously performs the clear notice task and cancels it to prevent any future execution.
7579
///
7680
private func performClearNoticeTask() {
7781
clearNoticeTask.perform()
@@ -85,6 +89,13 @@ struct NoticeModifier: ViewModifier {
8589
feedbackGenerator.notificationOccurred(feedbackType)
8690
}
8791
}
92+
93+
/// Returns a scaled width for a regular horizontal size class.
94+
///
95+
private func preferredSizeClassWidth(_ geometry: GeometryProxy) -> CGFloat {
96+
let multiplier = horizontalSizeClass == .regular ? 0.5 : 1.0
97+
return geometry.size.width * multiplier
98+
}
8899
}
89100

90101
// MARK: Custom Views
@@ -155,7 +166,7 @@ private extension NoticeAlert {
155166

156167
/// Notice view padding.
157168
///
158-
let defaultInsets = UIEdgeInsets(top: 16, left: 16, bottom: 28, right: 16)
169+
let defaultInsets = UIEdgeInsets(top: 16, left: 16, bottom: 32, right: 16)
159170

160171
init(noticeView: NoticeView) {
161172
self.noticeView = noticeView
@@ -186,7 +197,7 @@ private extension NoticeAlert {
186197
/// Returns the preferred size of the view using the fixed width.
187198
///
188199
override var intrinsicContentSize: CGSize {
189-
let targetSize = CGSize(width: width - defaultInsets.left - defaultInsets.right, height: 0)
200+
let targetSize = CGSize(width: width - defaultInsets.left - defaultInsets.right, height: 0)
190201
let noticeHeight = noticeView.systemLayoutSizeFitting(
191202
targetSize,
192203
withHorizontalFittingPriority: .required,

0 commit comments

Comments
 (0)