Coder Social home page Coder Social logo

patilshreyas / livestream-kt Goto Github PK

View Code? Open in Web Editor NEW
97.0 9.0 8.0 603 KB

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

License: MIT License

Kotlin 86.42% Java 13.58%
kotlin kotlin-android kotlin-library android android-library coroutines-android livestream thread livestream-kt

livestream-kt's Introduction

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.

livestream-kt's People

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  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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

livestream-kt's Issues

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.