Skip to content

Commit 7294903

Browse files
feat: Allow showing alert from different scene
1 parent 8f9f92a commit 7294903

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Sources/NotificationToast/ToastView.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public class ToastView: UIView {
168168
clipsToBounds = true
169169
}
170170

171-
func prepareForShowing() {
172-
overlayWindow = ToastViewWindow(toastView: self)
171+
func prepareForShowing(originWindowScene: UIWindowScene?) {
172+
overlayWindow = ToastViewWindow(toastView: self, originWindowScene: originWindowScene)
173173
setupConstraints(position: position)
174174
setupStackViewConstraints()
175175
overlayWindow?.isHidden = false
@@ -180,17 +180,14 @@ public class ToastView: UIView {
180180
layer.cornerRadius = bounds.height / 2
181181
}
182182

183-
public func show(haptic: UINotificationFeedbackGenerator.FeedbackType? = nil) {
184-
if let hapticType = haptic {
185-
UINotificationFeedbackGenerator().notificationOccurred(hapticType)
183+
public func show(originWindowScene: UIWindowScene? = nil, haptic: UINotificationFeedbackGenerator.FeedbackType? = nil) {
184+
if let haptic {
185+
UINotificationFeedbackGenerator().notificationOccurred(haptic)
186186
}
187-
show()
188-
}
189-
190-
public func show() {
187+
191188
if overlayWindow == nil {
192189
// We don't have a window we need to recreate one
193-
prepareForShowing()
190+
prepareForShowing(originWindowScene: originWindowScene)
194191
} else {
195192
// We are still showing an alert with current window so we do nothing
196193
return
@@ -280,8 +277,10 @@ public class ToastView: UIView {
280277

281278
@available(iOSApplicationExtension, unavailable)
282279
class ToastViewWindow: UIWindow {
283-
init(toastView: ToastView) {
284-
if let activeForegroundScene = UIApplication.shared.connectedScenes
280+
init(toastView: ToastView, originWindowScene: UIWindowScene?) {
281+
if let originWindowScene {
282+
super.init(windowScene: originWindowScene)
283+
} else if let activeForegroundScene = UIApplication.shared.connectedScenes
285284
.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {
286285
super.init(windowScene: activeForegroundScene)
287286
} else if let inactiveForegroundScene = UIApplication.shared.connectedScenes

0 commit comments

Comments
 (0)