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.
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.
}
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Xcode 9.0 +
- Swift 4.0 +
AgnesUnicorn is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AgnesUnicorn'
magic_harly@hotmail.com, hy@mogoroom.com
AgnesUnicorn is available under the MIT license. See the LICENSE file for more info.