GithubHelp home page GithubHelp logo

ibrahimyilmaz / archdata Goto Github PK

View Code? Open in Web Editor NEW
31.0 4.0 2.0 107 KB

LiveData types for Publishing and Replaying Data

License: MIT License

Kotlin 100.00%
android androidarchitecturecomponets mutablelivedata android-library google

archdata's Introduction

Build Status Download

ArchData

ArchData provides two missing LiveData of Android Lifecycle Component and useful extension functions.

MutableLiveData shares only last state to its new observers when they start to observe.

But in some cases, either these observers may want to do fresh start or they want to get all previous states.

PublishLiveData and ReplayLiveData types are extensions of MutableLiveData.

Basically:

  • PublishLiveData is MutableLiveData, which only notify its observers if there is an event after their starting of observation.
  • ReplayLiveData is MutableLiveData, which only notify its observers for all previous events happened before their observation starts.

Extension Functions:

  • nonNullObserve is extension function, which eliminates nullable invokes of Observer.

LiveDataAction:

  • LiveDataAction is special object type for LiveData,which enables observer can listen LiveData object without parameter. It is useful when Action/Command use cases.

Usage

  • PublishLiveData

    LiveData that, once an Observer has subscribed, emits all subsequently observed items to the subscriber.

    val liveData = PublishLiveData<String>();
    // observer1 will receive all events
    liveData.observe(lifeCycleOwner,observer1)
    liveData.postValue("one")
    liveData.postValue("two")
    // observer2 will only receive "three"
    liveData.observe(lifeCycleOwner,observer2)
    liveData.postValue("three")
  • ReplayLiveData

    LiveData that buffers all items it observes and replays them to any Observer that subscribes.

    val liveData =  ReplayLiveData<String>()
    liveData.postValue("one")
    liveData.postValue("two")
    liveData.postValue("three")
    // both of the following will get the events from above
    liveData.observe(lifeCycleOwner,observer1)
    liveData.postValue(lifeCycleOwner,observer2)
  • nonNullObserve

    observe method which eliminates nullable invocation of observers.

    val liveData = MutableLiveData<String>()
    ...
    
    viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataEvent)
    
    ...
    
    fun onLiveDataEvent(event: String){
       //Do something good!.
    }
    
  • LiveDataAction

    Object Type which enables invocation listener methods without parameter

    val liveData = MutableLiveData<LiveDataAction>()
    ...
    
    viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataAction)
    
    ...
    
    fun onLiveDataEvent(){
       //Do something really good!.
    }
    

Download

Maven:

<dependency>
  <groupId>me.ibrahimyilmaz</groupId>
  <artifactId>ArchData</artifactId>
  <version>0.0.4</version>
  <type>pom</type>
</dependency>

Gradle:

compile 'me.ibrahimyilmaz:ArchData:0.0.4'

License

MIT License

Copyright (c) [2019] [IBRAHIM YILMAZ | [email protected]]

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.

archdata's People

Contributors

ibrahimyilmaz 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

Watchers

 avatar  avatar  avatar  avatar

Forkers

morristech hazer

archdata'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.