From 0de2a42bd754af020081c479b75fbbb1d2325710 Mon Sep 17 00:00:00 2001 From: Rivu Chakraborty Date: Thu, 19 Nov 2020 10:10:38 +0530 Subject: [PATCH] feat: migrate to Room-RxJava3 and dump RxBridge --- README.md | 7 +- app/build.gradle | 8 +- .../moviesearch/data/MovieRepository.kt | 2 +- .../data/local/LocalMovieDataStore.kt | 211 +++++++++--------- .../data/local/database/MovieDao.kt | 6 +- .../data/MovieRepositorySpecTest.kt | 2 +- .../data/local/LocalMovieDataStoreSpec.kt | 8 +- 7 files changed, 115 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index 92d0b3c..d36b9bc 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,10 @@ The goal of this project is to have a demo application using popular MVI Arch pa - Lottie for Splash Animation - Dagger HILT for DI (Migrated from Koin in this commit: [21a9db64bee1359bd57cf99ba757467da63f10b2](https://github.com/RivuChk/Jetpack-Compose-MVI-Demo/commit/21a9db64bee1359bd57cf99ba757467da63f10b2)) - RxJava 3 for Streams -- Room for DB (With RxJava 2) -- RxBridge, to convert Rx2 Streams (from Room) to Rx3 +- Room for DB (With RxJava 3) - Mockito for mocking -- LiveData for emiting states from ViewModel -- `LazyColumnItems` from Compose for showing List +- LiveData for emitting states from ViewModel +- `LazyColumnFor` from Compose for showing List - *Glide* for image loading with Composable `Image` - Composable `ConstraintLayout` - Composable `TopAppBar` diff --git a/app/build.gradle b/app/build.gradle index eccef3f..35fb95e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -103,7 +103,7 @@ dependencies { implementation "androidx.compose.runtime:runtime:$compose_version" implementation "androidx.compose.runtime:runtime-livedata:$compose_version" - implementation "androidx.compose.runtime:runtime-rxjava2:$compose_version" + implementation "androidx.compose.runtime:runtime-rxjava3:$compose_version" implementation "androidx.compose.compiler:compiler:$compose_version" implementation "androidx.compose.foundation:foundation:$compose_version" implementation "androidx.compose.ui:ui:$compose_version" @@ -161,12 +161,10 @@ dependencies { androidTestImplementation("androidx.ui:ui-test:$compose_version") //Room DB - def room_version = "2.3.0-alpha01" + def room_version = "2.3.0-alpha03" implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-ktx:$room_version" - implementation "androidx.room:room-rxjava2:$room_version" + implementation "androidx.room:room-rxjava3:$room_version" kapt "androidx.room:room-compiler:$room_version" - //RxJava3 Bridge Required as Room doesn't support RxJava3 yet - implementation "com.github.akarnokd:rxjava3-bridge:3.0.0" } \ No newline at end of file diff --git a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepository.kt b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepository.kt index deaf5c7..04d1b7c 100644 --- a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepository.kt +++ b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepository.kt @@ -1,6 +1,6 @@ package dev.rivu.mvijetpackcomposedemo.moviesearch.data -import androidx.room.EmptyResultSetException +import androidx.room.rxjava3.EmptyResultSetException import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.Movie import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.MovieDetail import io.reactivex.rxjava3.core.Completable diff --git a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStore.kt b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStore.kt index b4aee11..75f4896 100644 --- a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStore.kt +++ b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStore.kt @@ -1,12 +1,11 @@ package dev.rivu.mvijetpackcomposedemo.moviesearch.data.local -import androidx.room.EmptyResultSetException +import androidx.room.rxjava3.EmptyResultSetException import dev.rivu.mvijetpackcomposedemo.moviesearch.data.MovieDataStore import dev.rivu.mvijetpackcomposedemo.moviesearch.data.local.database.MovieDao import dev.rivu.mvijetpackcomposedemo.moviesearch.data.local.database.MovieEnitity import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.Movie import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.MovieDetail -import hu.akarnokd.rxjava3.bridge.RxJavaBridge import io.reactivex.rxjava3.core.Completable import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.core.Single @@ -14,44 +13,25 @@ import io.reactivex.rxjava3.core.Single class LocalMovieDataStore(private val movieDao: MovieDao) : MovieDataStore { override fun getMoviesStream(searchQuery: String): Flowable> { - return RxJavaBridge.toV3Flowable( - movieDao.getMoviesStream(searchQuery) - .map { list -> - list.map { - Movie( - imdbID = it.imdbID, - poster = it.poster, - title = it.title, - type = it.type, - year = it.year - ) - } + return movieDao.getMoviesStream(searchQuery) + .map { list -> + list.map { + Movie( + imdbID = it.imdbID, + poster = it.poster, + title = it.title, + type = it.type, + year = it.year + ) } - ) + } } override fun getMovies(searchQuery: String): Single> { - return RxJavaBridge.toV3Single( - movieDao.getMovies(searchQuery) - .map { list -> - list.map { - Movie( - imdbID = it.imdbID, - poster = it.poster, - title = it.title, - type = it.type, - year = it.year - ) - } - } - ) - } - - override fun addMovies(movieList: List): Completable { - return return RxJavaBridge.toV3Completable( - movieDao.addMovies( - movieList.map { - MovieEnitity( + return movieDao.getMovies(searchQuery) + .map { list -> + list.map { + Movie( imdbID = it.imdbID, poster = it.poster, title = it.title, @@ -59,91 +39,100 @@ class LocalMovieDataStore(private val movieDao: MovieDao) : MovieDataStore { year = it.year ) } - ) - ) + } } - override fun getMovieDetail(imdbId: String): Single { - return RxJavaBridge.toV3Single( - movieDao.getMovie(imdbId) - .map { - if (it.detail == null) { - throw EmptyResultSetException("detail not present for this movie") - } else { - MovieDetail( - imdbID = it.imdbID, - poster = it.poster, - title = it.title, - type = it.type, - year = it.year, - - response = it.detail.response, - actors = it.detail.actors, - awards = it.detail.awards, - boxOffice = it.detail.boxOffice, - country = it.detail.country, - dVD = it.detail.dVD, - director = it.detail.director, - genre = it.detail.genre, - imdbRating = it.detail.imdbRating, - imdbVotes = it.detail.imdbVotes, - language = it.detail.language, - metascore = it.detail.metascore, - plot = it.detail.plot, - production = it.detail.production, - rated = it.detail.rated, - ratings = it.detail.ratings.map { - MovieDetail.Rating( - source = it.source, - value = it.value - ) - }, - released = it.detail.released, - runtime = it.detail.runtime, - website = it.detail.website, - writer = it.detail.writer - ) - } - } + override fun addMovies(movieList: List): Completable { + return movieDao.addMovies( + movieList.map { + MovieEnitity( + imdbID = it.imdbID, + poster = it.poster, + title = it.title, + type = it.type, + year = it.year + ) + } ) } - override fun addMovieDetail(movie: MovieDetail): Completable { - return RxJavaBridge.toV3Completable( - movieDao.updateMovieInDB( - MovieEnitity( - imdbID = movie.imdbID, - poster = movie.poster, - title = movie.title, - type = movie.type, - year = movie.year, - detail = MovieEnitity.Detail( - response = movie.response, - actors = movie.actors, - awards = movie.awards, - boxOffice = movie.boxOffice, - country = movie.country, - dVD = movie.dVD, - director = movie.director, - genre = movie.genre, - imdbRating = movie.imdbRating, - imdbVotes = movie.imdbVotes, - language = movie.language, - metascore = movie.metascore, - plot = movie.plot, - production = movie.production, - rated = movie.rated, - ratings = movie.ratings.map { - MovieEnitity.Detail.Rating( + override fun getMovieDetail(imdbId: String): Single { + return movieDao.getMovie(imdbId) + .map { + if (it.detail == null) { + throw EmptyResultSetException("detail not present for this movie") + } else { + MovieDetail( + imdbID = it.imdbID, + poster = it.poster, + title = it.title, + type = it.type, + year = it.year, + + response = it.detail.response, + actors = it.detail.actors, + awards = it.detail.awards, + boxOffice = it.detail.boxOffice, + country = it.detail.country, + dVD = it.detail.dVD, + director = it.detail.director, + genre = it.detail.genre, + imdbRating = it.detail.imdbRating, + imdbVotes = it.detail.imdbVotes, + language = it.detail.language, + metascore = it.detail.metascore, + plot = it.detail.plot, + production = it.detail.production, + rated = it.detail.rated, + ratings = it.detail.ratings.map { + MovieDetail.Rating( source = it.source, value = it.value ) }, - released = movie.released, - runtime = movie.runtime, - website = movie.website, - writer = movie.writer + released = it.detail.released, + runtime = it.detail.runtime, + website = it.detail.website, + writer = it.detail.writer ) + } + } + } + + override fun addMovieDetail(movie: MovieDetail): Completable { + return movieDao.updateMovieInDB( + MovieEnitity( + imdbID = movie.imdbID, + poster = movie.poster, + title = movie.title, + type = movie.type, + year = movie.year, + detail = MovieEnitity.Detail( + response = movie.response, + actors = movie.actors, + awards = movie.awards, + boxOffice = movie.boxOffice, + country = movie.country, + dVD = movie.dVD, + director = movie.director, + genre = movie.genre, + imdbRating = movie.imdbRating, + imdbVotes = movie.imdbVotes, + language = movie.language, + metascore = movie.metascore, + plot = movie.plot, + production = movie.production, + rated = movie.rated, + ratings = movie.ratings.map { + MovieEnitity.Detail.Rating( + source = it.source, + value = it.value + ) + }, + released = movie.released, + runtime = movie.runtime, + website = movie.website, + writer = movie.writer ) ) ) diff --git a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/database/MovieDao.kt b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/database/MovieDao.kt index 1ebe55d..38159b8 100644 --- a/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/database/MovieDao.kt +++ b/app/src/main/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/database/MovieDao.kt @@ -1,9 +1,9 @@ package dev.rivu.mvijetpackcomposedemo.moviesearch.data.local.database import androidx.room.* -import io.reactivex.Completable -import io.reactivex.Flowable -import io.reactivex.Single +import io.reactivex.rxjava3.core.Completable +import io.reactivex.rxjava3.core.Flowable +import io.reactivex.rxjava3.core.Single @Dao interface MovieDao { diff --git a/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepositorySpecTest.kt b/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepositorySpecTest.kt index f6e9aad..d667b3c 100644 --- a/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepositorySpecTest.kt +++ b/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/MovieRepositorySpecTest.kt @@ -1,4 +1,4 @@ -import androidx.room.EmptyResultSetException +import androidx.room.rxjava3.EmptyResultSetException import com.nhaarman.mockitokotlin2.* import dev.rivu.mvijetpackcomposedemo.moviesearch.data.MovieDataStore import dev.rivu.mvijetpackcomposedemo.moviesearch.data.MovieRepository diff --git a/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStoreSpec.kt b/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStoreSpec.kt index af24366..a4866e4 100644 --- a/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStoreSpec.kt +++ b/app/src/test/java/dev/rivu/mvijetpackcomposedemo/moviesearch/data/local/LocalMovieDataStoreSpec.kt @@ -1,4 +1,4 @@ -import androidx.room.EmptyResultSetException +import androidx.room.rxjava3.EmptyResultSetException import com.nhaarman.mockitokotlin2.atLeastOnce import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.verify @@ -12,9 +12,9 @@ import dev.rivu.mvijetpackcomposedemo.moviesearch.data.local.database.MovieDao import dev.rivu.mvijetpackcomposedemo.moviesearch.data.local.database.MovieEnitity import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.Movie import dev.rivu.mvijetpackcomposedemo.moviesearch.data.model.MovieDetail -import io.reactivex.Completable -import io.reactivex.Flowable -import io.reactivex.Single +import io.reactivex.rxjava3.core.Completable +import io.reactivex.rxjava3.core.Flowable +import io.reactivex.rxjava3.core.Single import org.mockito.ArgumentMatchers.anyList import org.mockito.ArgumentMatchers.anyString import org.spekframework.spek2.Spek