All Projects → adibfara → Lives

adibfara / Lives

Lives - Android LiveData Extensions for Kotlin and Java

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Lives

Android Architecture Components Kotlin
Sample used to practice Kotlin and Android Architecture Components.
Stars: ✭ 326 (-35.95%)
Mutual labels:  architecture-components, livedata
Livedata Testing
TestObserver to easily test LiveData and make assertions on them.
Stars: ✭ 358 (-29.67%)
Mutual labels:  architecture-components, livedata
movies
An example approach for modularization, reactive clean architecture and persistancy.
Stars: ✭ 110 (-78.39%)
Mutual labels:  livedata, architecture-components
AndroidCleanArchitecture
Android Project with clean android architecture contain Dagger, Retrofit, Retrofit, Android archtecture components, LiveData with MVVM architecture
Stars: ✭ 22 (-95.68%)
Mutual labels:  livedata, architecture-components
Architecturecomponentsdemo
Kotlin demo project that uses some Android Architecture Components (ViewModel and LiveData) with Dagger 2 and Coroutines
Stars: ✭ 269 (-47.15%)
Mutual labels:  architecture-components, livedata
Superhero-App
🦸🏻‍♂️🦹🏻‍♀️Superhero app built with Kotlin, ViewModel, LiveData, ViewBinding, Room, and Hilt
Stars: ✭ 27 (-94.7%)
Mutual labels:  livedata, architecture-components
Githubarchitecturecomponents
The implementation of Android "Architecture Components" sample explained by Google : https://developer.android.com/topic/libraries/architecture/guide.html
Stars: ✭ 302 (-40.67%)
Mutual labels:  architecture-components, livedata
TeamManagerApp
A sample app structure using the MVVM architecture LiveData, RxJava, ViewModel, Room and the Navigation Arch Components.
Stars: ✭ 36 (-92.93%)
Mutual labels:  livedata, architecture-components
Daggerandroidmvvm
Demonstrates using Dagger 2.11+ in MVVM app with Android Architecture Components, Clean Architecture, RxJava
Stars: ✭ 255 (-49.9%)
Mutual labels:  architecture-components, livedata
LifecycleCells
An Android library that provides a Lifecycle to any ViewHolder through the implementation of the LifecycleOwner interface, allowing it to interact with a Lifecycle-Aware Component.
Stars: ✭ 19 (-96.27%)
Mutual labels:  livedata, architecture-components
Simple-Notes-Kotlin-App
✍️ Simple Note Making App use mvvm architecture , dagger , coroutines and navigation component. Features includes 🗒️ create , edit and ❌ delete notes
Stars: ✭ 40 (-92.14%)
Mutual labels:  livedata, architecture-components
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (-19.45%)
Mutual labels:  architecture-components, livedata
KTAndroidArchitecture
A Kotlin android architecture with Google Architecture Components
Stars: ✭ 33 (-93.52%)
Mutual labels:  livedata, architecture-components
LiveData-DataBinding-Kotlin
Sample to practice LiveData + DataBinding
Stars: ✭ 89 (-82.51%)
Mutual labels:  livedata, architecture-components
Android-MVVM-News-App
MVVM News Application with clean code architecture & android jetpack components.
Stars: ✭ 38 (-92.53%)
Mutual labels:  livedata, architecture-components
Android-Mvi-Starter
Android MVI Starter application
Stars: ✭ 19 (-96.27%)
Mutual labels:  livedata, architecture-components
livedata-recyclerview-sample
No description or website provided.
Stars: ✭ 76 (-85.07%)
Mutual labels:  livedata, architecture-components
Restaurants
Restaurants sample app built with the new architecture components (LiveData, Room, ViewModel) and Dagger 2
Stars: ✭ 47 (-90.77%)
Mutual labels:  livedata, architecture-components
ReactiveLiveData
Transformation functions for LiveData
Stars: ✭ 80 (-84.28%)
Mutual labels:  livedata, architecture-components
Livedata Ktx
Kotlin extension for LiveData, chaining like RxJava
Stars: ✭ 466 (-8.45%)
Mutual labels:  architecture-components, livedata

Lives - Android LiveData Extensions for Kotlin and Java

Build Status Latest Version

Add RxJava-like operators to your LiveData objects with ease, usable in Kotlin (as extension functions) and Java (as static functions to a class called Lives)

Download

Add the dependencies to your project:

AndroidX Version

implementation 'com.snakydesign.livedataextensions:lives:1.3.0'
implementation 'android.arch.lifecycle:extensions:x.x.x' // If you are using the AndroidX version, that's fine too, as the Jetifier will take care of the conversion.

Non AndroidX Version

implementation 'com.snakydesign.livedataextensions:lives:1.2.1'
implementation 'androidx.lifecycle:lifecycle-livedata:x.x.x' 

If you want to use this library on a Java project, add the following dependency:

implementation 'org.jetbrains.kotlin:kotlin-stdlib:x.x.x'

Usage

Kotlin

Import the functions

    import com.snakydesign.livedataextensions.*

Creating LiveData

  • liveDataOf : Create a LiveData object from a value (like just in RxJava, although it immediately emits the value)
    val liveData = liveDataOf(2) //liveData will produce 2 (as Int) when observed
  • from : Creates a LiveData that emits the value that the callable function produces, and immediately emits it.
    val liveData = liveDataOf {computePI()}
  • emptyLiveData : Creates an empty LiveData
    val liveData = emptyLiveData<Int>()

Filtering

  • distinct : Emits the items that are different from all the values that have been emitted so far
    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.distinct()
    originalLiveData.value = 2
    originalLiveData.value = 2 // newLiveData will not produce this
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will not produce this
  • distinctUntilChanged : Emits the items that are different from the last item
    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.distinctUntilChanged()
    originalLiveData.value = 2
    originalLiveData.value = 2 // newLiveData will not produce this
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will produce
  • filter :Emits the items that pass through the predicate
    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.filter { it > 2 }
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will not produce this
  • first() : produces a SingleLiveData that produces only one Item.
  • take(n:Int) : produces a LiveData that produces only the first n Items.
  • takeUntil(predicate) : Takes until a certain predicate is met, and does not emit anything after that, whatever the value.
  • skip(n) : Skips the first n values.
  • skipUntil(predicate) : Skips all values until a certain predicate is met (the item that actives the predicate is also emitted).
  • elementAt(index) : emits the item that was emitted at index position
  • nonNull() : Will never emit the nulls to the observers.
  • defaultIfNull(value): Will produce the value when null is received.

Combining

  • merge(List<LiveData>) : Merges multiple LiveData, and emits any item that was emitted by any of them
  • LiveData.merge(LiveData) : Merges this LiveData with another one, and emits any item that was emitted by any of them
  • concat(LiveData...) : Concats multiple LiveData objects (and converts them to SingleLiveData if necessary, and emits their first item in order. (Please check the note below.)
  • LiveData.then(LiveData) : Concats the first LiveData with the given one. (Please check the note below.)
  • startWith(startingValue): Emits the startingValue before any other value.
  • zip(firstLiveData, secondLiveData, zipFunction): zips both of the LiveDatas using the zipFunction and emits a value after both of them have emitted their values, after that, emits values whenever any of them emits a value.
  • combineLatest(firstLiveData, secondLiveData, combineFunction): combines both of the LiveDatas using the combineFunction and emits a value after any of them have emitted a value.
  • LiveData.sampleWith(otherLiveData): Samples the current live data with other live data, resulting in a live data that emits the last value emitted by the original live data (if any) whenever the other live data emits

Transforming

  • map(mapperFunction) : Map each value emitted to another value (and type) with the given function
  • switchMap(mapperFunction) : Maps any values that were emitted by the LiveData to the given function that produces another LiveData
  • doBeforeNext(OnNextAction) : Does the onNext function before everything actually emitting the item to the observers
  • doAfterNext(OnNextAction) : Does the onNext function after emitting the item to the observers(function) : Does the onNext function before everything actually emitting the item to the observers
  • buffer(count) : Buffers the items emitted by the LiveData, and emits them when they reach the count as a List.
  • scan(accumulator) : Applies the accumulator function to each emitted item, starting with the second emitted item. Initial value of the accumulator is the first item.
  • scan(seed, accumulator) : Applies the accumulator function to each emitted item, starting with the initial seed.
  • amb(LiveData...) : Emits the items of the first LiveData that emits the item. Items of other LiveDatas will never be emitted and are not considered.
  • toMutableLiveData() : Converts a LiveData to a MutableLiveData with the initial value set by this LiveData's value

Java

You can call any function prefixed with Lives keyword.

import com.snakydesign.livedataextensions.Lives;
  • Example (create a LiveData with the initial value of 2 and map each value to its String type
    LiveData<String> liveData = Lives.map(Lives.just(2), new Function1<Integer, String>() {
                @Override
                public Integer invoke(Integer integer) {
                    return String.valueOf(integer);
                }
            }) ;
    

Notes

Please note that because of design of LiveData, after a value is emitted to an observer, and then another value is emitted, the old value is destroyed in any LiveData object. So unlike RxJava, if a new Observer is attached, It will only receive the most recent value.

So If you want to use operators like concat, you have to consider allowing only one observer to the LiveData.

PRs are more than welcome, and please file an issue If you encounter something 🍻.

You can also ping me on twitter @TheSNAKY.

License

Copyright 2018 Adib Faramarzi.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].