Skip to content

A simplified event-driven library on top of Kotlin and Rx

License

Notifications You must be signed in to change notification settings

joshuadeguzman/RxBus

Repository files navigation

RxBus

A simplified event-driven library on top of Kotlin and Rx

Kotlin RxJava Build Status GitHub (pre-)release

Features

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

Installation

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    // Replace version with release version, e.g. 1.0.0-alpha, -SNAPSHOT
    implementation 'io.jmdg:rxbus:[VERSION]'
}

Create Events

// Events.kt
// Define your events here

// Passing data
data class ShowDataEvent(val data: String)

// No passing of data
class NoDataEvent

Implement methods

// 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"))
    }
}

License

RxBus is released under the MIT License. See LICENSE for details.

Contributing

  1. Fork it (https://github.com/joshuadeguzman/RxBus/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request