FlowDirection 0.0.5

FlowDirection 0.0.5

Pavel维护。



 
依赖
RxSwift~> 4.4.1
RxCocoa~> 4.4.1
 

  • Pavel

FlowDirection

Platform Swift License Version

基于RxSwift的协调器实现模式

📲安装

Cocoapods
pod 'FlowDirection'
pod install

👨🏼‍💻 如何使用?

在App代理中设置 RxCoordinatorRxTabBarController

import RxSwift
import FlowDirection
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    let disposeBag = DisposeBag()
    var window: UIWindow?
    var coordinator: RxCoordinator?
    let bag = DisposeBag()
    let factory: FlowFactory = ViewControllerFactory()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let tab = RxTabBarController(flows: [ViewControllerType.tabOne, ViewControllerType.tabTwo])
        let nav = UINavigationController(rootViewController: tab)
        coordinator = RxCoordinator(navigationController: nav, tabBarController: tab, factory: factory)
        // optional
        coordinator?.rx.willNavigate.subscribe(onNext: { (direction) in
            if let flow = direction.1 {
                print("direction -> \(direction.0) flow -> \(flow)")
            } else {
                print("direction -> \(direction.0)")
            }
        }).disposed(by: bag)
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
        
        return true
    }
}

创建新文件 ViewControllerFactory(可以取其他名字),我的工厂看起来如下👇🏼

enum ViewControllerType: Flow {
    
    case first
    case second
    
    case tabOne
    case tabTwo
    
    var index: Int? {
        switch self {
        case .tabOne:
            return 0
        case .tabTwo:
            return 1
        default:
            return nil
        }
    }
    
    var flow: UIViewController? {
        switch self {
        case .tabOne:
            return UINavigationController(rootViewController: ViewController())
        case .tabTwo:
            return UINavigationController(rootViewController: SecondViewController())
        default:
            return nil
        }
    }
}


class ViewControllerFactory: FlowFactory {
    func makeViewController(with flow: Flow) -> UIViewController {
        guard let flow = flow as? ViewControllerType else {
            fatalError()
        }
        switch flow {
        case .first:
            return ViewController()
        case .second:
            return SecondViewController()
        default:
            return ViewController()
        }
    }
}
在 ViewController 中,您必须继承自 RxFlowViewController
import RxSwift
import RxCocoa
import FlowDirection

class ViewController: RxFlowViewController {

  func bind() {}
        button.rx.tap.map { (_) -> (DirectionRoute, [RxCoordinatorMiddleware]?) in
            return (DirectionRoute.present(flow: ViewControllerType.second, animated: true), .none)
        }
            .bind(to: rxcoordinator!.rx.route)
            .disposed(by: bag)
    }
    
}
这就是全部了

中间件

您可以使用中间件来处理导航命令,这也很简单。创建新文件,创建类
class DeniedMiddleware: RxCoordinatorMiddleware {
    func perfom(_ route: DirectionRoute) -> DirectionRoute {
        switch route {
        case .push(flow: _, animated: _, hideTab: _):
            return .present(flow: ViewControllerType.second, animated: true)
        default:
            return .push(flow: ViewControllerType.first, animated: true, hideTab: false)
        }
    }
}
现在,您需要将此中间件添加到 DirectionRoute,回到您的 ViewController 并更改 map 函数中的代码。
button.rx.tap.map { (_) -> (DirectionRoute, [RxCoordinatorMiddleware]?) in
    let mid = DeniedMiddleware()
    return (DirectionRoute.present(flow: ViewControllerType.second, animated: true), [mid])
}.bind(to: rxcoordinator!.rx.route)