Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use view controller navigation for alert #201

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ struct OverlayModifier<OverlayContent: View>: ViewModifier {

@EnvironmentObject private var controller: OverlayController

init(
isPresented: Binding<Bool>,
@ViewBuilder overlayContent: @escaping () -> OverlayContent
) {
self.isPresented = isPresented
self.overlayContent = overlayContent
}

func body(content: Content) -> some View {
content
// NOTE: can't use controller.dismiss here as the isPresented
// change doesn't fire when containing view was dismissed
.onChange(of: isPresented.wrappedValue) { newValue in
if newValue {
controller.present(content: overlayContent)
} else {
controller.dismiss()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ struct AlertView<Content: View>: View {
let content: Content

@State private var isPresentedAnimated: Bool = false

@EnvironmentObject private var controller: OverlayController

var animationDuration: Double { 0.1 }

var body: some View {
Expand Down Expand Up @@ -62,7 +59,6 @@ struct AlertView<Content: View>: View {
isPresentedAnimated = false
}
try? await Task.sleep(seconds: animationDuration)
controller.dismiss()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ struct NotificationView<Content: View>: View {

@State private var isPresentedAnimated: Bool = false

@EnvironmentObject var controller: OverlayController

var animationDuration: Double { 0.1 }
var presentingDuration: Double { 3.0 }

Expand Down Expand Up @@ -42,7 +40,6 @@ struct NotificationView<Content: View>: View {
isPresentedAnimated = false
}
try? await Task.sleep(seconds: animationDuration)
controller.dismiss()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ struct PopupView<Content: View>: View {

@State private var isPresentedAnimated: Bool = false

@EnvironmentObject var controller: OverlayController

var animationDuration: Double { 0.1 }

var body: some View {
Expand Down Expand Up @@ -43,7 +41,6 @@ struct PopupView<Content: View>: View {
isPresentedAnimated = false
}
try? await Task.sleep(seconds: animationDuration)
controller.dismiss()
}
}
}
Expand Down