Skip to content

Commit

Permalink
Use view controller navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Programistich committed Mar 4, 2024
1 parent 81094cd commit 466e275
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 70 deletions.
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)
}
}
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

0 comments on commit 466e275

Please sign in to comment.