-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Programistich
committed
Mar 4, 2024
1 parent
81094cd
commit f815126
Showing
5 changed files
with
15 additions
and
66 deletions.
There are no files selected for viewing
59 changes: 13 additions & 46 deletions
59
Flipper/Packages/UI/Sources/InAppNotifications/Overlay/OverlayController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,26 @@ | ||
import SwiftUI | ||
|
||
class OverlayController: ObservableObject { | ||
private var overlay: UIWindow? | ||
private var views: [UIView] | ||
var rootVC: UIViewController? | ||
|
||
init() { | ||
self.overlay = OverlayWindow() | ||
self.views = [] | ||
} | ||
|
||
func present<Content: View>( | ||
@MainActor func present<Content: View>( | ||
@ViewBuilder content: @escaping () -> Content | ||
) { | ||
guard let overlay else { return } | ||
|
||
let viewController = UIHostingController( | ||
rootView: content() | ||
.environmentObject(self) | ||
) | ||
let viewController = UIHostingController(rootView: content()) | ||
viewController.view.backgroundColor = .clear | ||
viewController.modalTransitionStyle = .crossDissolve | ||
viewController.modalPresentationStyle = .overCurrentContext | ||
|
||
views.append(viewController.view) | ||
self.rootVC = UIApplication | ||
.shared | ||
.connectedScenes | ||
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] } | ||
.first(where: { $0.isKeyWindow })?.rootViewController | ||
|
||
if let rootViewController = overlay.rootViewController { | ||
viewController.view.frame = rootViewController.view.frame | ||
} else { | ||
overlay.rootViewController = viewController | ||
overlay.isUserInteractionEnabled = true | ||
overlay.isHidden = false | ||
} | ||
rootVC?.present(viewController, animated: true) | ||
} | ||
|
||
func dismiss() { | ||
guard let overlay else { return } | ||
|
||
guard !views.isEmpty else { | ||
return | ||
} | ||
|
||
views.removeFirst() | ||
|
||
if let first = views.first { | ||
guard | ||
let rootViewController = overlay.rootViewController | ||
else { | ||
return | ||
} | ||
rootViewController.view.subviews.forEach { view in | ||
view.removeFromSuperview() | ||
} | ||
rootViewController.view.addSubview(first) | ||
} else { | ||
overlay.isHidden = true | ||
overlay.isUserInteractionEnabled = false | ||
overlay.rootViewController = nil | ||
} | ||
@MainActor func dismiss() { | ||
rootVC?.dismiss(animated: false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters