A Simple kotlin first EventBus implementation

Related tags

EventBus eventbus
Overview

EventBus

Simple event bus for event-driven programming. Taking advantage of kotlin language features instead of typical reflections

Table of Contents

Installation

Build Tool

If you are not using Gradle fallow the guide for other build tools on jitpack.io.

Gradle

Add https://www.jitpack.io as a maven repo to your project

repositories {
    maven { url 'https://www.jitpack.io' }
}

Then add the following dependency to your project

dependencies {
    implementation 'com.github.Zarzelcow:eventbus:-SNAPSHOT'
}

From Source

All code except dependencies is contained in one file Eventbus.kt

Dependencies are the Kotlin Standard Library and jodah's TypeTools

Usage

Unlike most 'conventional' annotation based event bus implementations, You instead register listeners on fields using kotlin functions

btw this still uses reflections, just not to call handlers


import com.github.zarzelcow.eventbus.Eventbus

val EVENT_BUS = EventBus()

class MyListener {

    @Handler
    val onKeypress = { event: EventKeypress ->
        println("Key pressed: ${event.key}")
    }

}

Btw. you can also create handlers like so if you don't need the event itself

@Handler(EventKeypress::class)
fun onKeypress() {
    println("A Key pressed")
}

This will register a listener on the event EventKeypress and will be called when the event is fired.


Events are completely normal java objects meaning you don't have to extend a type to publish anything

An example of an event is

    class EventKeypress(val key: Int)

To publish an event to all register listeners use code like this

fun `example publish`() {
    EVENT_BUS.publish(EventKeypress(key))
}

Registering the listener is done by calling subscribe with the instance of the class holding the listener

    EVENT_BUS.subscribe(MyListenerInstance)

To unregister a listener, call unsubscribe with the instance of the class holding the listener

    EVENT_BUS.unsubscribe(MyListenerInstance)

That gives you a simple event bus loop that you can use to publish events and subscribe to them in your code.

import com.github.zarzelcow.eventbus.Eventbus

val EVENT_BUS = EventBus()

class EventKeypress(val key: Int)
class MyListener {

    @Handler
    val onKeypress = { event: EventKeypress ->
        println("Key pressed: ${event.key}")
    }

}

fun main() {
    val MyListenerInstance = MyListener()
    EVENT_BUS.subscribe(MyListenerInstance)
    EVENT_BUS.publish(EventKeypress(1)) // prints "Key pressed: 1"
    EVENT_BUS.unsubscribe(MyListenerInstance)
}

Look at Test cases for some more examples

You might also like...
Showify is a my first simple ✅ Android application 📱 using DI, where I learn how to use dagger-hilt, retrofit2, mvvm, livedata, Requestly Interceptor
Showify is a my first simple ✅ Android application 📱 using DI, where I learn how to use dagger-hilt, retrofit2, mvvm, livedata, Requestly Interceptor

Showify is a my first simple ✅ Android application 📱 using DI, where I learn how to use dagger-hilt, retrofit2, mvvm, livedata, Requestly Interceptor and so much more...

Segmenkt - The SegmenKT Kotlin SDK is a Kotlin-first SDK for Segment

SegmenKT Kotlin SDK The SegmenKT Kotlin SDK is a Kotlin-first SDK for Segment. I

Clean Android multi-module offline-first scalable app in 2022. Including Jetpack Compose, MVI, Kotlin coroutines/Flow, Kotlin serialization, Hilt and Room.

Android Kotlin starter project - 2022 edition Android starter project, described precisely in this article. Purpose To show good practices using Kotli

This is a first kotlin project
This is a first kotlin project

SmallPocket This is a first kotlin app, help user to save links easily, and can export to Evernote as weekly. Steps: copy link anywhere open SmallPock

This is a first kotlin project
This is a first kotlin project

SmallPocket This is a first kotlin app, help user to save links easily, and can export to Evernote as weekly. Steps: copy link anywhere open SmallPock

This is a first kotlin project
This is a first kotlin project

SmallPocket This is a first kotlin app, help user to save links easily, and can export to Evernote as weekly. Steps: copy link anywhere open SmallPock

A Kotlin-first SDK for Firebase

Firebase Kotlin SDK Built and maintained with 🧡 by GitLive Real-time code collaboration inside any IDE The Firebase Kotlin SDK is a Kotlin-first SDK

This is my first Spring Boot with Kotlin project and used React as frontend.
This is my first Spring Boot with Kotlin project and used React as frontend.

༒☬༒ ꜱᴘʀɪɴɢ ʙᴏᴏᴛ + ᴋᴏᴛʟɪɴ ༒☬༒ This project is simple course enrolment site built with React with Typescript and Spring boot with Kotlin. Frontend - Ove

Starter code for the first independent project for Android Basics in Kotlin

Project: Lemonade App - Starter Code Starter code for the first independent project for Android Basics in Kotlin Introduction This is the starter code

My attempt at completing Advent of Code for the first time, might also learn Kotlin while doing so.

AdventOfCode2021 Welcome to the Advent of Code1 Kotlin project created by debskipawel using the Advent of Code Kotlin Template delivered by JetBrains.

Android Country Picker is a Kotlin-first, flexible and powerful Android library that allows to integrate Country Picker with just a few lines.
Android Country Picker is a Kotlin-first, flexible and powerful Android library that allows to integrate Country Picker with just a few lines.

1. Add dependency dependencies { implementation 'com.hbb20:android-country-picker:X.Y.Z' } For latest version, 2. Decide your use-case

A Task Management and Habit-building App, representating my journey in Android as well as my first Kotlin App. 🤗 ✨
A Task Management and Habit-building App, representating my journey in Android as well as my first Kotlin App. 🤗 ✨

A Task Management and Habit-building App, representating my journey in Android as well as my first Kotlin App. 🤗 ✨

Android library to generate image avatar from the first letter of a username. Letter avatar like Gmail Android best practice
Android library to generate image avatar from the first letter of a username. Letter avatar like Gmail Android best practice

AvatarImageGenerator Generate first letter avatar Image like gmail's contact avatar. It generates an drawable that can be be set to an ImageView. Inst

An Android library introducing a stack of Views with the first item being flippable.
An Android library introducing a stack of Views with the first item being flippable.

FlippableStackView An Android library introducing a stack of Views with the first item being flippable. Views inside the stack remain the aspect ratio

A React Native library making file access easier for developers as first class citizens, without the tears

React Native File Gateway A React Native library making file access easier for developers as first class citizens, without the tears. ⚠️ NOTE: This li

Build your first Android app - a tip calculator!
Build your first Android app - a tip calculator!

Tippy - A Simple Tip Calculator for Android Build Tippy as your first Android app! Follow the YouTube walkthrough video Author: Rahul Pandey Features:

VasSonic is a lightweight and high-performance Hybrid framework developed by tencent VAS team, which is intended to speed up the first screen of websites working on Android and iOS platform.
VasSonic is a lightweight and high-performance Hybrid framework developed by tencent VAS team, which is intended to speed up the first screen of websites working on Android and iOS platform.

VasSonic: A Lightweight And High-performance Hybrid Framework VasSonic is a lightweight and high-performance Hybrid framework developed by tencent VAS

First submission in Belajar Fundamental Aplikasi Android Class from Dicoding Indonesia
First submission in Belajar Fundamental Aplikasi Android Class from Dicoding Indonesia

Github User App (Submission 1) Before I explain how this application works and submission checklist, I want to say thank you to Dicoding Indonesia for

Shoe Store project first Attempt

Shoe Store project first Attempt User Info: email: [email protected] password:12345 I had problem to select the home Screen for the navigation gr

Owner
Zarzel K. Shih
🌟Starry eyed🌟 Novice Developer, Known for making both Astolfo🚀 client and Astro🚀 client. currently working part time 💸 to make ends meet in this crisis😷
Zarzel K. Shih
EventBus for Android,消息总线,基于SharedFlow,具有生命周期感知能力,支持Sticky,支持线程切换,支持延迟发送。

背景 跨页面通信是一个比较常见的场景,通常我们会选择使用EventBus,但EventBus无法感知声明周期,收到消息就会回调,所以有了LiveData之后很快就有了LiveEventBus。不过它也有缺点,比如不能切换线程。

BiuBiuQiu0 167 Jan 3, 2023
A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.

AndroidEventBus This is an EventBus library for Android. It simplifies the communication between Activities, Fragments, Threads, Services, etc. and lo

Mr.Simple 1.6k Nov 30, 2022
eventbus-intellij-plugin 3.8 0.0 L1 Java Plugin to navigate between events posted by EventBus.

eventbus-intellij-plugin Plugin to navigate between events posted by EventBus. Post to onEvent and onEvent to Post Install There are two ways. Prefere

Shinnosuke Kugimiya 315 Aug 8, 2022
Dead simple EventBus for Android made with Kotlin and RxJava 2

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

Adriel Café 46 Dec 6, 2022
Eventbus implemented by Flow

FlowBus FlowVersion EventBus Usage Add it in your root build.gradle at the end of repositories

wenchieh 3 Jun 24, 2021
EventBus for Android,消息总线,基于SharedFlow,具有生命周期感知能力,支持Sticky,支持线程切换,支持延迟发送。

背景 跨页面通信是一个比较常见的场景,通常我们会选择使用EventBus,但EventBus无法感知声明周期,收到消息就会回调,所以有了LiveData之后很快就有了LiveEventBus。不过它也有缺点,比如不能切换线程。

BiuBiuQiu0 167 Jan 3, 2023
Realize EventBus with LiveData

LiveDataBus 增強UnPeek-LiveData,將其包裝成觀察者模式的事件總線 Getting started Add it in your root build.gradle at the end of repositories: allprojects { repositor

JaredDoge 2 Oct 25, 2022
A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.

AndroidEventBus This is an EventBus library for Android. It simplifies the communication between Activities, Fragments, Threads, Services, etc. and lo

Mr.Simple 1.6k Nov 30, 2022
A simple karaoke app. My first Android project.

Chorus This is my first Android project ever. I'm very happy that I managed to start it. Thanks a lot to the devs, who made all of the software I'm us

Ilya 1 Jan 18, 2022