All Projects → bobekos → ReactiveLiveData

bobekos / ReactiveLiveData

Licence: Apache-2.0, Apache-2.0 licenses found Licenses found Apache-2.0 LICENSE Apache-2.0 LICENSE.md
An RxJava Extension for the LiveData observer introduced by Google.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to ReactiveLiveData

Jetpack-Compose-MVI-Demo
Demo / Sample Android Project created with Jetpack Compose and MVI Architecture Pattern
Stars: ✭ 114 (+570.59%)
Mutual labels:  room, rxandroid, viewmodel, livedata
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (+1182.35%)
Mutual labels:  room, viewmodel, livedata, android-architecture-components
Android Architecture Components Kotlin
Sample used to practice Kotlin and Android Architecture Components.
Stars: ✭ 326 (+1817.65%)
Mutual labels:  room, viewmodel, livedata, android-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 (+135.29%)
Mutual labels:  room, viewmodel, livedata, android-architecture-components
Easychatandroidclient
EasyChat是一个开源的社交类的App。主要包含消息、好友、群组等相关的IM核心功能。部分界面参照了QQ、微信等相关社交APP。EasyChat APP整体采用MVVM模式,基于JetPack(Lifecycle,LiveData,ViewModel,Room)构建
Stars: ✭ 64 (+276.47%)
Mutual labels:  room, viewmodel, livedata, android-architecture-components
Popularmovies
🎥 Movie discovery app showcasing Android best practices with Google's recommended architecture: MVVM + Repository + Offline support + Android Architecture Components + Paging library & Retrofit2.
Stars: ✭ 142 (+735.29%)
Mutual labels:  room, viewmodel, android-architecture-components
Androidroom
Android example to show how to use Room to access SQLite database on device for reading and writing data. This example also shows how to use LiveData and ViewModel with Room to build reactive, well performing and easy to maintain applications.
Stars: ✭ 36 (+111.76%)
Mutual labels:  room, viewmodel, livedata
Gallerit
A sample Android gallery to search images posted on Reddit built using modern Android development tools (Architecture Components, MVVM, Coroutines, Flow, Navigation, Retrofit, Room, Koin)
Stars: ✭ 153 (+800%)
Mutual labels:  room, viewmodel, livedata
Githubprojectbrowser
This is a sample Android Project that is based on Clean Architecture
Stars: ✭ 64 (+276.47%)
Mutual labels:  room, viewmodel, livedata
News
A sample News 🗞 app built using Modern Android Development [Architecture Components, Coroutines, Retrofit, Room, Kotlin, Dagger]
Stars: ✭ 774 (+4452.94%)
Mutual labels:  room, viewmodel, android-architecture-components
Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (+223.53%)
Mutual labels:  room, viewmodel, livedata
Wanandroid
Kotlin+JetPack+协程实现的MVVM架构Wanandroid客户端
Stars: ✭ 197 (+1058.82%)
Mutual labels:  room, viewmodel, livedata
Androidarchitecture
Recommended architecture by Android
Stars: ✭ 883 (+5094.12%)
Mutual labels:  room, viewmodel, livedata
Android Architecture Components Kotlin
Clean code App with Kotlin and Android Architecture Components
Stars: ✭ 23 (+35.29%)
Mutual labels:  room, livedata, android-architecture-components
Foodium
It simply loads Posts data from API and stores it in persistence storage (i.e. SQLite Database). Posts will be always loaded from local database. Remote data (from API) and Local data is always synchronized.
Stars: ✭ 1,940 (+11311.76%)
Mutual labels:  room, livedata, android-architecture-components
Base Mvvm
App built to showcase basic Android View components like ViewPager, RecyclerView(homogeneous and heterogeneous items), NavigationDrawer, Animated Vector Drawables, Collapsing Toolbar Layout etc. housed in a MVVM architecture
Stars: ✭ 18 (+5.88%)
Mutual labels:  room, viewmodel, livedata
Android Mvvm Coroutine
Kotlin android application example with MVVM pattern, android architecture, kotlin coroutine, unit test, and UI test
Stars: ✭ 111 (+552.94%)
Mutual labels:  room, viewmodel, livedata
Kotlin Pokedex
🌀 A Pokedex app using ViewModel, LiveData, Room and Navigation
Stars: ✭ 1,156 (+6700%)
Mutual labels:  room, viewmodel, livedata
Restaurants
Restaurants sample app built with the new architecture components (LiveData, Room, ViewModel) and Dagger 2
Stars: ✭ 47 (+176.47%)
Mutual labels:  room, viewmodel, livedata
Reactive Mvvm Android
My way to MVVM using KotlinX Coroutines and Android data-binding
Stars: ✭ 626 (+3582.35%)
Mutual labels:  room, viewmodel, livedata

ReactiveLiveData Android Arsenal

An RxJava Extension for the LiveData observer introduced by Google. Provides the ability to perform single actions using RxJava and takes advantage of an automatic subscription of the Lifecycle owner. Mainly designed to used Room CRUD commands with RxJava.

Why this lib?

Medium article

Getting Started

Setting up the dependency

implementation 'com.github.bobekos:reactivelivedata:x.x.x'

Usage

Just use one of the available reactiveSource classes:

CompletableReactiveSource

//ViewModel
class UserViewModel(private val dao: UserDao) : ViewModel() {

    fun insert(id: Int, name: String): LiveData<Optional<Nothing>> {
        return CompletableReactiveSource.fromAction {
            dao.insert(UserEntity(id, name))
        }
    }
}

//Activity/Fragment/etc.
...
//short
viewModel.insert(1, "User").subscribeCompletable(this)
//or with callback
viewModel.insert(1, "Bobekos").subscribeCompletable(this,
                    //optional
                    onComplete = {
                        showToast("User inserted")
                    },
                    //optional
                    onError = {
                        showToast(it.message)
                    })

SingleReactiveSource

//ViewModel
class UserViewModel(private val dao: UserDao) : ViewModel() {

    fun getFromSingle(id: Int): LiveData<Optional<UserEntity>> {
        return SingleReactiveSource.from(dao.getByIdAsSingle(id))
    }
}

//Activity/Fragment/etc.
...
viewModel.getFromSingle(1).subscribeSingle(this,
                    onSuccess = {
                        showToast("User ${it.username} loaded")
                    },
                    //optional
                    onError = {
                        showToast(it.message)
                    })

MaybeReactiveSource

//ViewModel
class UserViewModel(private val dao: UserDao) : ViewModel() {

    fun getFromMaybe(id: Int): LiveData<Optional<UserEntity>> {
        return MaybeReactiveSource.from(dao.getByIdAsMaybe(id))
    }
}

//Activity/Fragment/etc.
...
viewModel.getFromMaybe(1).subscribeMaybe(this,
                    onSuccess = {
                        showToast("User ${it.username} loaded")
                    },
                    //optional
                    onError = {
                        showToast(it.message)
                    },
                    //optional
                    onComplete = {
                        showToast("No user found")
                    })

FlowableReactiveSource

//ViewModel
class UserViewModel(private val dao: UserDao) : ViewModel() {

    fun getFromFlowable(): LiveData<Optional<UserEntity>> {
        return FlowableReactiveSource.from(dao.getUsers())
    }
}

//Activity/Fragment/etc.
...
viewModel.getFromFlowable().subscribeFlowable(this,
                    onNext = {
                        showToast("User ${it.name} loaded")
                    },
                    //optional
                    onError = {
                        showToast(it.message)
                    },
                    //optional
                    onComplete = {
                        showToast("No user found")
                    })

NullSafe extension for LiveDataReactiveStreams

But without exception support. Use FlowableReactiveStream instead.

//ViewModel
class UserViewModel(private val dao: UserDao) : ViewModel() {

    fun loadUser(): LiveData<UserEntity> {
        return LiveDataReactiveStreams.fromPublisher(dao.getUsers())
    }
}

//Activity/Fragment/etc.
...
//short
viewModel.loadUser().nonNullObserver(this, observer = {
            showToast("I'm not null ${it.username}")
        })
//or with null callback
viewModel.loadUser().nonNullObserver(this,
                observer = {
                    showToast("I'm observing ${it.username}")
                },
                //optional
                nullObserver = {
                    showToast("Value is null")
                })

Testing

For each reactive source there is a specific test method.

liveData.testCompletableSubscribe(...)
liveData.testMaybeSubscribe(...)
liveData.testSingleSubscribe(...)
liveData.testFlowableSubscribe(...)

Make sure to include the 'InstantTastExecutorRule' (core-testing) into your tests. Furthermore, the default IoSchedulerHandler (or the scheduler which you used) should be overwritten.

@RunWith(JUnit4::class)
class UserViewModelTest {
    
    private inline fun <reified T> lambdaMock(): T = Mockito.mock(T::class.java)
    
    @get:Rule
    var rule: TestRule = InstantTaskExecutorRule()
    
    private val userDao = mock(UserDao::class.java)
    private val viewModel = UserViewModel(userDao)
    
    @Before
    fun setup() {
        RxJavaPlugins.setIoSchedulerHandler {
            Schedulers.trampoline()
        }
    }
    
    @Test
    fun testGetFromSingleSuccess() {
        val testObject = UserEntity(1, "Bobekos")

        `when`(userDao.getByIdAsSingle(1)).then { Single.just(testObject) }

        val lifecycle = LifecycleRegistry(mock(LifecycleOwner::class.java))
        lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)

        val observer = lambdaMock<(t: UserEntity) -> Unit>()

        viewModel.getFromSingle(1).testSingleSubscribe(lifecycle, onSuccess = observer)

        verify(observer).invoke(testObject)
    }
    
    @Test
    fun testGetFromSingleError() {
        val testObject = SQLiteConstraintException()

        `when`(userDao.getByIdAsSingle(1)).then { Single.error<UserEntity>(testObject) }

        val lifecycle = LifecycleRegistry(mock(LifecycleOwner::class.java))
        lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)

        val observer = lambdaMock<(e: Throwable) -> Unit>()

        viewModel.getFromSingle(1).testSingleSubscribe(lifecycle, onError = observer)

        verify(observer).invoke(testObject)
    }

For more tests look into the sample app

Resources and Credits

License

Copyright 2018 Bobek Bobekos

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].