-
Notifications
You must be signed in to change notification settings - Fork 95
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 466e275
Showing
5 changed files
with
19 additions
and
70 deletions.
There are no files selected for viewing
67 changes: 17 additions & 50 deletions
67
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] | ||
|
||
init() { | ||
self.overlay = OverlayWindow() | ||
self.views = [] | ||
} | ||
|
||
func present<Content: View>( | ||
var rootVC: UIViewController? | ||
|
||
@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 | ||
|
||
views.append(viewController.view) | ||
|
||
if let rootViewController = overlay.rootViewController { | ||
viewController.view.frame = rootViewController.view.frame | ||
} else { | ||
overlay.rootViewController = viewController | ||
overlay.isUserInteractionEnabled = true | ||
overlay.isHidden = false | ||
} | ||
viewController.modalTransitionStyle = .crossDissolve | ||
viewController.modalPresentationStyle = .overCurrentContext | ||
self.rootVC = UIApplication | ||
.shared | ||
.connectedScenes | ||
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] } | ||
.first(where: { $0.isKeyWindow })?.rootViewController | ||
|
||
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