Skip to content

Instantly share code, notes, and snippets.

@barrault01
Created December 17, 2018 11:46
Show Gist options
  • Select an option

  • Save barrault01/cd16694cd2cdaf9a097044bec452aa94 to your computer and use it in GitHub Desktop.

Select an option

Save barrault01/cd16694cd2cdaf9a097044bec452aa94 to your computer and use it in GitHub Desktop.
swift protocol to present a react native view
import UIKit
public protocol ReactPresenter {
func presentReactViewController(moduleName: String?, jsFileName: String?,bundle: Bundle? , isDebug: Bool, props: [String: Any]?, hidesBottomBarWhenPushed: Bool, showNavigationBar: Bool)
func showReactViewController(moduleName: String?, jsFileName: String?,bundle: Bundle? , isDebug: Bool, props: [String: Any]? , hidesBottomBarWhenPushed: Bool, showNavigationBar: Bool)
func presenterViewController() -> UIViewController
}
extension ReactPresenter where Self: UIViewController {
public func presenterViewController() -> UIViewController {
return self
}
}
extension ReactPresenter {
public func presentReactViewController(moduleName: String? = nil,
jsFileName: String? = nil,
bundle: Bundle? = nil,
isDebug: Bool = false,
props: [String: Any]? = nil,
hidesBottomBarWhenPushed: Bool = false,
showNavigationBar: Bool = true) {
let vc = ReactViewController(moduleName: moduleName,
jsFileName: jsFileName,
bundle:bundle,
isDebug: isDebug,
showNavigationBar: showNavigationBar,
props: props){
DispatchQueue.main.async {
self.presenterViewController().dismiss(animated: true, completion: nil)
}
}
presenterViewController().present(vc, animated: true, completion: nil)
}
public func showReactViewController(moduleName: String? = nil,
jsFileName: String? = nil,
bundle: Bundle? = nil,
isDebug: Bool = false,
props: [String: Any]? = nil,
hidesBottomBarWhenPushed: Bool = false,
showNavigationBar: Bool = true) {
let vc = ReactViewController(moduleName: moduleName,
jsFileName: jsFileName,
bundle:bundle,
isDebug: isDebug,
showNavigationBar: showNavigationBar,
props: props) {
DispatchQueue.main.async {
if let ctrl = self.presenterViewController() as? UINavigationController {
ctrl.popViewController(animated: true)
return
}
if let ctrl = self.presenterViewController().navigationController {
ctrl.popViewController(animated: true)
return
}
self.presenterViewController().dismiss(animated: true, completion: nil)
}
}
vc.hidesBottomBarWhenPushed = hidesBottomBarWhenPushed
presenterViewController().show(vc, sender: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment