Giter Site home page Giter Site logo

adrielcafe / kbus Goto Github PK

View Code? Open in Web Editor NEW
46.0 4.0 7.0 132 KB

Dead simple EventBus for Android made with Kotlin and RxJava 2

License: MIT License

Kotlin 100.00%
android android-library eventbus kotlin kotlin-android kotlin-library rxjava2 rxjava-android rxjava

kbus's Introduction

Release Android Arsenal

KBus

Super lightweight (13 LOC) and minimalistic (post(), subscribe(), unsubscribe()) EventBus written with idiomatic Kotlin and RxJava 2

KBus in 3 steps

1. Define your events

// With data
data class ShowMessageEvent(val message: String)

// Without data
class OtherEvent

2. Add subscribers

override fun onStart() {
    super.onStart()
    KBus.subscribe<ShowMessageEvent>(this) {
        showMessage(it.message)
    }
    KBus.subscribe<OtherEvent>(this) {
        doSomething()
    }
}

override fun onStop() {
    super.onStop()
    KBus.unsubscribe(this)
}

Where:

  • ShowMessageEvent - the event you're subscribing
  • this - the subscriber (by default the current Activity/Fragment)
  • { showMessage() } - the subscription observer
  • it - the event instance

Sticky events

If you want to post events between Activities/Fragments just subscribe in onCreate() (or onPostCreate()) and unsubscribe in onDestroy()

override fun onPostCreate(savedInstanceState: Bundle?) {
    super.onPostCreate(savedInstanceState)
    KBus.subscribe<ShowMessageEvent>(this) {
        showMessage(it.message)
    }
}

override fun onDestroy() {
    super.onDestroy()
    KBus.unsubscribe(this)
}

3. Post events

KBus.post(ShowMessageEvent("Hello KBus!"))

Add KBus to your project

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

dependencies {
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'com.github.adrielcafe:KBus:1.1'
}

kbus's People

Contributors

adrielcafe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kbus's Issues

KT and Java mixed code in the same project

I have kt and Java mixed in the same project.
I send messages to bus from KT

How do I receive messages in Java part of the project?
Some lines of code would be useful

problem implementing

hi, sorry if this is an obvious question, i'm only a sprout with android. i was trying to implement your library in my project as you suggest, adding a few lines in my build.gradle (after copying "library" folder from KBus-master to my project "lib" folder):

    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'com.github.adrielcafe:KBus:1.1'

also tried

implementation 'com.github.adrielcafe:KBus:1.0'

as this is the version github is reporting, yet to no avail. it tells me dependency cannot be resolved. what am i missing?

--
using AndroidStudio 3.5.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.