Created
December 17, 2018 11:46
-
-
Save barrault01/cd16694cd2cdaf9a097044bec452aa94 to your computer and use it in GitHub Desktop.
swift protocol to present a react native view
This file contains hidden or 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
| 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