RxPanModal 0.2

RxPanModal 0.2

Maintained by [Meng Li].



  • By
  • lm2343635

RxPanModal

Build Status Version License Platform

RxPanModal is a RxSwift reactive extension for the library PanModal. With RxPanModal, a view controller can be presented as a pan modal from the view model directly.

Documentation

RxPanModal is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxPanModal'

Present customized view controller as a pan modal

To present a view controller as a pan modal from a view model directly, the view controller should implement the procotol RxPanModalPresentable.

extension SelectorViewController: RxPanModalPresentable {
    
    static func create(item: RxPanModalItem) -> Self? {
        guard let item = item as? SelectorPanModalItem else {
            return nil
        }
        return self.init(item: item)
    }
    
    var panScrollable: UIScrollView? {
        return tableView
    }
    
}

In the static method create, there is a struct SelectorPanModalItem which implements the protocol RxPanModalItem.

struct SelectorPanModalItem: RxPanModalItem {

    static let controllerType: RxPanModalPresentable.Type = SelectorViewController.self

    let names: [String]
    let didNameSelected: (String) -> Void
}

In the item struct, the controller type should be indicated. Other properties and closures for exchanging data with view model should also be defined in the item struct.

After implementing the customized view modal, a PublishSubject property panModal should be prepared in the view model.

class ViewModel {
    private let panModalSubject = PublishSubject<RxPanModal>()
    
    var panModal: Observable<RxPanModal> {
        return panModalSubject.asObservable()
    }
}

The corresponding view controller should implement the protocol RxPanModalShowable and bind the observable panModel.

class ViewController: UIViewController, RxPanModalShowable {
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.name.bind(to: nameLabel.rx.text).disposed(by: disposeBag)
    }
}

At last, we can present the customized view controller from the view model directly.

panModalSubject.onNext(item: SelectorPanModalItem(names: ["Alice", "Bob", "Carol"]) { 
    print($0)
})

Prepared template

To use the prepared template view controllers, just install with the subspec RxPanModal/Template.

pod 'RxPanModal/Template'

The following templates in prepared.

  • RxPanModalPickerViewController
panModalSubject.onNextPicker(
    theme: .dark,
    title: "Months",
    done: "Done",
    models: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Nov", "Dec"],
    didSelectItemAt: { index, model in
        print("select at \(index) " + model.description)
    },
    doneAt: { index, model in
        print("done at \(index) " + model.description)
    }
)

Dissmiss all presented pan modals.

Sometimes, if you want to dismiss all presented pan modals, just invoke RxPanModal.dismissAll() from anywhere.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

License

RxPanModal is available under the MIT license. See the LICENSE file for more info.