A simplified event-driven library on top of Kotlin and Rx
Methods | Usage |
---|---|
subscribe<Event> (subscriber) |
Register type object as an observer |
post(event: Any) | Passing event class to be ommitted on the observer |
unsubscribe(subscriber: Any) | Removes registered observers from the disposables |
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
// Replace version with release version, e.g. 1.0.0-alpha, -SNAPSHOT
implementation 'io.jmdg:rxbus:[VERSION]'
}
// Events.kt
// Define your events here
// Passing data
data class ShowDataEvent(val data: String)
// No passing of data
class NoDataEvent
// Activity.kt
// This defines subscriptions (eg. on onCreate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
// Register observers
RxBus.subscribe<ShowDataEvent>(this) {
Log.e(tag, it.data)
}
RxBus.subscribe<NoDataEvent>(this) {
callSomeMethod()
}
}
// Remove subscriptions
override fun onStop() {
super.onStop()
RxBus.unsubscribe(this)
}
override fun onDestroy() {
super.onDestroy()
RxBus.unsubscribe(this)
}
// Fragment.kt
// This sends data to event observers/subscribers
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_fragment)
bt_send_data.setOnClickListener {
RxBus.post(ShowDataEvent("Hi, this is from Fragment.kt"))
}
}
RxBus is released under the MIT License. See LICENSE for details.
- Fork it (https://github.com/joshuadeguzman/RxBus/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request