AgnesUnicorn 0.0.4

AgnesUnicorn 0.0.4

Maintained by Undead1116.



 
Depends on:
RxSwift>= 0
RxCocoa>= 0
 

Agnes

CI Status Version License Platform

Situration

The simple requirement is like:

In MVVM with RxSwift, we may face the situation below:

If we can use the instance of viewModelA in ControllerB, it will be excellent! But they are in the different modules, and the import actions is unavailable due to our infrastructure guidline.

Actually, even A and B are both in the same module, the reference of ViewModelA in ControllerB is also not of elegance.

Usage

Agnes makes your viewModels in different Modules or Components to interact with each other like normal bindings in MVVM.

So in your ViewModelA , you just need to call the donate method with a common key to keep the current signal , then others can onNext or make something to bindTo it.

extension String : Unicorn
{
    
}


class ViewModel : NSObject {
    
    let input: PublishRelay<String> = PublishRelay()
    
    let output: PublishRelay<String> = PublishRelay()
    
    override init() {
        super.init()
        input.donate(key: key, to: agnes).bind(to: output)
    }
}

All the generic type T in PublishRelay should impletment Unicorn , which is Agnes's belief~.

Then we can call the input in ViewModelA in other controllers:

class SecondViewController: UIViewController {

    let disposeBag = DisposeBag()
    let secondPusher: PublishRelay<String> = PublishRelay<String>()
    @IBOutlet weak var secondButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        secondButton.rx.tap.map { "Unicorn comes!" }.bind(to: secondPusher).disposed(by: disposeBag)
        secondPusher.bind(to: agnes.unicorn(key: key)).disposed(by: disposeBag)
        // Do any additional setup after loading the view.
    }
}

Example

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

Requirements

  • Xcode 9.0 +
  • Swift 4.0 +

Installation

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

pod 'AgnesUnicorn'

Author

[email protected], [email protected]

License

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