Skip to content

ininmm/RxBus2

Repository files navigation

RxBus2

This is a RxBus using by RxJava2

Download

Getting Started

In your build.gradle:

dependencies {
    implementation 'com.github.ininmm:library:1.0.0'
}

Usage

Need to register and unregister before post something.

It is recommended to register in onCreate and unregister in onDestroy:

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        RxBus.getBus().register(this)

    }
    
    override fun onDestroy() {
        // ...
        RxBus.getBus().unregister(this)
    }

You can post an event like this:

    private fun postSomeThing() {
        val news = "simpleSubscribe"
        RxBus.getBus().post(tag = SAMPLE_POST, event = news)
    }

And use @Subscribe to receive event:

    @Subscribe(thread = EventThread.MainThread, tags = [TagModel.Tag(SAMPLE_POST)])
    fun simpleSubscribe(news: String) {
        Toast.makeText(this, news, Toast.LENGTH_SHORT).show()
    }

If you want to post a sticky event, you should use @Produce to post an event:

    private fun postDataStick() {
        sendByProduce.add("123")
        sendByProduce.add("456")
    }
    
    @Produce(thread = EventThread.IO, tags = [TagModel.Tag(SAMPLE_STICK)])
    fun produceSample(): ArrayList<String> {
        return sendByProduce
    }

then receive event by @Subscribe.

That's all, you're good to go!

About

This is a RxBus using by RxJava2.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages