|  |  |  | // Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import SwiftUI | 
					
						
							|  |  |  | import UIKit | 
					
						
							|  |  |  | import SessionUtilitiesKit | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ViewControllerHolder { | 
					
						
							|  |  |  |     weak var value: UIViewController? | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ViewControllerKey: EnvironmentKey { | 
					
						
							|  |  |  |     static var defaultValue: ViewControllerHolder { | 
					
						
							|  |  |  |         return ViewControllerHolder( | 
					
						
							|  |  |  |             value: HasAppContext() ? CurrentAppContext().mainWindow?.rootViewController : nil | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extension EnvironmentValues { | 
					
						
							|  |  |  |     public var viewController: UIViewController? { | 
					
						
							|  |  |  |         get { return self[ViewControllerKey.self].value } | 
					
						
							|  |  |  |         set { self[ViewControllerKey.self].value = newValue } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extension UIViewController { | 
					
						
							|  |  |  |     public func present<Content: View>(style: UIModalPresentationStyle = .automatic, @ViewBuilder builder: () -> Content) { | 
					
						
							|  |  |  |         let toPresent = UIHostingController(rootView: AnyView(EmptyView())) | 
					
						
							|  |  |  |         toPresent.modalPresentationStyle = style | 
					
						
							|  |  |  |         toPresent.rootView = AnyView( | 
					
						
							|  |  |  |             builder() | 
					
						
							|  |  |  |                 .environment(\.viewController, toPresent) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "dismissModal"), object: nil, queue: nil) { [weak toPresent] _ in | 
					
						
							|  |  |  |             toPresent?.dismiss(animated: true, completion: nil) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         self.present(toPresent, animated: true, completion: nil) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public struct UIView_SwiftUI: UIViewRepresentable { | 
					
						
							|  |  |  |     public typealias UIViewType = UIView | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     private let view: UIView | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public init(view: UIView) { | 
					
						
							|  |  |  |         self.view = view | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public func makeUIView(context: Context) -> UIView { | 
					
						
							|  |  |  |         return self.view | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public func updateUIView(_ uiView: UIView, context: Context) { | 
					
						
							|  |  |  |         uiView.layoutIfNeeded() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |