Giter Site home page Giter Site logo

zafarivaev / reactivecoordinator Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 1.0 191 KB

ReactiveCoordinator is a framework based on the Coordinator pattern. Implemented in RxSwift

License: MIT License

Ruby 40.60% Objective-C 20.43% Swift 38.97%
rxswift mvvm-architecture coordinator-pattern swift

reactivecoordinator's Introduction

ReactiveCoordinator

ReactiveCoordinator is a framework based on the Coordinator pattern. Implemented in RxSwift

Requirements

  • iOS 11.0
  • Swift 5

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate ReactiveCoordinator into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'ReactiveCoordinator', '4.0.0'

How To Use

Inherit from ReactiveCoordinator in Coordinator subclasses:

class HolidaysCoordinator: ReactiveCoordinator<Void> {
    
    let rootViewController: UIViewController
    
    init(rootViewController: UIViewController) {
        self.rootViewController = rootViewController
    }
    
    override func start() -> Observable<Void> {
        
        let viewController = HolidaysViewController()
        let viewModel = HolidaysViewModel()
        viewController.viewModel = viewModel
        
        rootViewController.navigationController?.pushViewController(viewController, animated: true)
        
        return Observable.empty()
    }
    
}

Replace Delegation with ReactiveCoordinator

In presented Coordinator:

enum ChooseCountryCoordinationResult {
    case country(String)
    case cancel
}

class ChooseCountryCoordinator: ReactiveCoordinator<ChooseCountryCoordinationResult> {
    
    private let rootViewController: UIViewController
    
    init(rootViewController: UIViewController) {
        self.rootViewController = rootViewController
    }
    
    override func start() -> Observable<CoordinationResult> {
        let viewController = ChooseCountryViewController()
        let navigationController = UINavigationController(rootViewController: viewController)
        
        let viewModel = ChooseCountryViewModel()
        viewController.viewModel = viewModel
        
        let country = viewModel.selectedCountry.map { CoordinationResult.country($0) }
        let cancel = viewModel.didClose.map { _ in
            CoordinationResult.cancel
        }
        
        rootViewController.present(navigationController, animated: true, completion: nil)
        
        return Observable.merge(country, cancel)
            .take(1)
            .do(onNext: { _ in
                viewController.dismiss(animated: true, completion: nil)
            })
    }
    
}

In presenting Coordinator:

class HolidaysCoordinator: ReactiveCoordinator<Void> {
    
    let rootViewController: UIViewController
    
    init(rootViewController: UIViewController) {
        self.rootViewController = rootViewController
    }
    
    override func start() -> Observable<Void> {
        
        let viewController = HolidaysViewController()
        let viewModel = HolidaysViewModel()
        viewController.viewModel = viewModel
        
        viewModel.chooseCountry
            .flatMap { [weak self] _ -> Observable<String?> in
                guard let `self` = self else { return .empty() }
                return self.coordinateToChooseCountry()
        }
        .filter { $0 != nil }
        .map { $0! }
        .bind(to: viewModel.selectedCountry)
        .disposed(by: disposeBag)
        
        return Observable.never()
    }
    
    // MARK: - Coordination
    private func coordinateToChooseCountry() -> Observable<String?> {
        let chooseCountryCoordinator = ChooseCountryCoordinator(rootViewController: rootViewController)
        return coordinate(to: chooseCountryCoordinator)
            .map { result in
                switch result {
                case .country(let country): return country
                case .cancel: return nil
                }
        }
    }
    
}

License

ReactiveCoordinator is released under the MIT license. See LICENSE for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.