Latest 0.0.4
Homepage https://github.com/SpiciedCrab/AgnesUnicorn
License MIT
Platforms ios 8.0
Dependencies RxSwift, RxCocoa
Authors

Agnes

CI Status
Version
License
Platform

AgnesUnicorn

Situration

The simple requirement is like:

AgnesUnicorn

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

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.

Latest podspec

{
    "name": "AgnesUnicorn",
    "version": "0.0.4",
    "summary": "To find your unicorn in your all components",
    "description": "Let your viewModel to call all Signals cross the modules.nYou can temporarily save your signal in to the cache, then another viewModel's signal can be binded to it.nThe lifecycle of the temp signal will be ended while the original viewModel is denited.",
    "homepage": "https://github.com/SpiciedCrab/AgnesUnicorn",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "[email protected]": "[email protected]"
    },
    "source": {
        "git": "https://github.com/SpiciedCrab/AgnesUnicorn.git",
        "tag": "0.0.4"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "AgnesUnicorn/Classes/**/*",
    "dependencies": {
        "RxSwift": [],
        "RxCocoa": []
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This