LiveStream is a simple class which makes communication easy among different modules of your application.

Overview

LiveStream-Kt (Android) 📱

LiveStream is a data holder class which can observe change of data in real-time and emit values too. Unlike other observables, LiveStream uses different approach. Here's emitter subscriber pattern. It's developed in Kotlin and fully compatible with Java.

Build

Bintray API JavaDoc

Github Followers GitHub stars GitHub forks GitHub watchers Twitter Follow

💡 Introduction

LiveStream is different than any other data observables. As compared to Android Architecture Component's LiveData it's totally different. In LiveData, you need to share the instance and then you need to add observer to listen for changes. It's useful in ViewModel to share data. LiveStream is useful in background and UI data sharing. Here, you need to subscribe to data stream and just need to emit data values and subscribe data stream from anywhere in the application using multiple instances of LiveStream.

For e.g. You have following classes in app-

  • UI Class - MainActivity
  • Service Class - BackgroundService

To share data in both classes, you will just need to add subscriber in UI class (MainActivity). In MainActivity, you'll subscribe to httpResponse stream. In BackgroundService class, whenever you want to share data, you will just need to emit data on stream - httpResponse. Then, those who subscribed for httpResponse data stream will be dispatched on value emission.

⚡️ Getting Started

This library is fully compatible with Java. Here we've demonstrated using Kotlin.

Add Dependency

Add below dependency in build.gradle of your app module.

Latest Build - Bintray

dependencies {
    implementation 'dev.shreyaspatil:LiveStream-kt:0.2'
}

Initialize LiveStream Class

LiveStream is a generic class. You can create instance as below.

        private val liveStream = LiveStream<String>()

Subscribe to Stream / Add Observer

LiveStream event will be dispatched when data is changed. See below code to add observer / subscriber.

Imagine, this code is in UI class of Android (e.g. MainActivity). Whenever value from background task (e.g. Service Class) is emitted, subscriber will listen live changes in data.

        val observer = liveStream.subscribe("httpResponse") { response ->
            showToast("This is HTTP response: $response")
        }

LiveStream#subscribe() method will return instance of StreamObserver. This instance will be useful to unsubscribe from LiveStream.

Emit Values/Data

You can emit values in LiveData from anywhere in the application. See below code.

From Main Thread :

To emit values from main thread, use LiveStream#set() method.

        val response = getResponse()
        liveStream.set("httpResponse", response)

From Other Thread :

To emit values from other thread, use LiveStream#post() method.

        val response = getResponse()
        liveStream.post("httpResponse", response)

Unsubscribe

To unsubscribe stream observer, use LiveStream#unsubscribe() method. It has to pass instance of StreamObserver which is obtained when you called subscribe() method. See below code.

    override fun onStop() {
        super.onStop()
        liveStream.unsubscribe(observer)
    }

🚀 Sample Usage

Sample code is available in /app directory. Both Java & Kotlin code sample available.

🤝 Contribute

If you want to contribute to this library, you're always welcome! See Contributing Guidelines.

✉️ Contact

If you need any help, you can connect with me.

Visit:- shreyaspatil.dev

📃 License

MIT License

Copyright (c) 2020 Shreyas Patil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You might also like...
A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays

PneumoniaClassifier A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays. W

A simple Kotlin class to use to connect to a MongoDB collection.
A simple Kotlin class to use to connect to a MongoDB collection.

mongodb-kotlin A simple Kotlin class to use to connect to a MongoDB collection. Once you have created a MongoDB project, choose Build a Database: Next

Simple view which allow you to customise your pizza's toppings and size as per your choice.
Simple view which allow you to customise your pizza's toppings and size as per your choice.

TwistedPizzaToppingsView Overview Simple view which allows options to customize your pizza toppings and size as per your choice. Features Android 12 s

A project that takes advantage of docker and makes the load test easier

Performance Test It's a project that takes advantage of docker and makes the load test easier. Also, it collects metrics from each running container.

Kotlin & Java class for gRPC client

jvm-minter-grpc-class Kotlin & Java class for gRPC client Use @TODO Разное Добавление git subtree add --prefix node-grpc-gateway https://github.com/Mi

Kotlin compiler plugin that allows class delegation to be dynamic like property delegations

kotlin-dynamic-delegation Kotlin compiler plugin that allows class delegation to be dynamic like property delegations. The plugin is working in progre

Minimalistic class proxy creator for Kotlin

reflektion Minimalistic class proxy creator for Kotlin. Reflektion allows you to create an implementation of an interface you provide containing proxy

Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class
Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class

Go Outside Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class. This app serves as a PROTOTYPE to demonstr

PenPath class is designed to be used to draw paths with variable thickness
PenPath class is designed to be used to draw paths with variable thickness

PenPath class is designed to be used to draw paths with variable thickness. It creates an object of android.graphics.Path and should be drawn with Paint.Style.FILL type paint.

Comments
Releases(v0.2)
Owner
Shreyas Patil
👨‍💻Google Developer Expert for Android ❤️Android, Kotlin 🌎Web Developer ⚙️Open Source Enthusiast 👨‍🚀Organizer @KotlinMumbai
Shreyas Patil
Scaloid makes your Android code easy to understand and maintain.

Simpler Android Scaloid is a library that simplifies your Android code. It makes your code easy to understand and maintain by leveraging Scala languag

Sung-Ho Lee 2.1k Dec 27, 2022
Muhammad Valian Masdani 2 Jul 5, 2022
Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

JetBrains 10k Jan 7, 2023
This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

Maksim Kostromin 2 Nov 29, 2021
Exktor - Extension modules for Ktor

Extension modules for Ktor ExKtor - it's set of extension libraries for Ktor tha

Simone Stefani 0 Sep 11, 2022
Reia is the Redis Pubsub client that Manase uses to communicate with other modules or nodes.

from Mana Reia is a simple wrapper around Lettuce to enable easy usage of its Redis Pubsub client. This library is only intended to be used for sendin

Mana 1 Apr 29, 2022
An annotation processor library that automatically creates Hilt's `@Binds` functions and modules.

HiltBinder An annotation processor library that automatically creates Hilt's @Binds functions and modules. If you think this library is useful, please

SangMin Park 5 Sep 19, 2022
Kotlin Example of how to organize your code using MVC and some patterns seen in class

Kotlin Example of how to organize your code using MVC and some patterns seen in class

José Luis González Sánchez 3 Mar 23, 2022
A very simple Android app which shows you random memes with the help of meme-api which you can share with your friends!

Meme Share A very simple Android app which shows you random memes with the help of meme-api which you can share with your friends! Tech stack 100% wri

Stɑrry Shivɑm 8 Aug 10, 2022