Skip to content

Commit

Permalink
Load data from database via diskio executor
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Apr 3, 2020
1 parent 52fe3c3 commit 8cbff35
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fun bindMoviePagination(view: RecyclerView, viewModel: MainActivityViewModel) {
recyclerView = view,
isLoading = { viewModel.getTvListValues()?.status == Status.LOADING },
loadMore = { viewModel.postMoviePage(it) },
onLast = { viewModel.getTvListValues()?.onLastPage!! }
onLast = { false }
).run {
currentPage = 1
}
Expand All @@ -84,7 +84,7 @@ fun bindPersonPagination(view: RecyclerView, viewModel: MainActivityViewModel) {
recyclerView = view,
isLoading = { viewModel.getPeopleValues()?.status == Status.LOADING },
loadMore = { viewModel.postPeoplePage(it) },
onLast = { viewModel.getPeopleValues()?.onLastPage!! }
onLast = { false }
).run {
currentPage = 1
}
Expand All @@ -104,7 +104,7 @@ fun bindTvPagination(view: RecyclerView, viewModel: MainActivityViewModel) {
recyclerView = view,
isLoading = { viewModel.getTvListValues()?.status == Status.LOADING },
loadMore = { viewModel.postTvPage(it) },
onLast = { viewModel.getTvListValues()?.onLastPage!! }
onLast = { false }
).run {
currentPage = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class PersistenceModule {
fun provideDatabase(@NonNull application: Application): AppDatabase {
return Room
.databaseBuilder(application, AppDatabase::class.java, "TheMovies.db")
.allowMainThreadQueries()
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MovieRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Keyword>> {
val movie = movieDao.getMovie(id_ = id)
val data: MutableLiveData<List<Keyword>> = MutableLiveData()
data.value = movie.keywords
data.postValue(movie.keywords)
return data
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class MovieRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Video>> {
val movie = movieDao.getMovie(id_ = id)
val data: MutableLiveData<List<Video>> = MutableLiveData()
data.value = movie.videos
data.postValue(movie.videos)
return data
}

Expand Down Expand Up @@ -134,7 +134,7 @@ class MovieRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Review>> {
val movie = movieDao.getMovie(id_ = id)
val data: MutableLiveData<List<Review>> = MutableLiveData()
data.value = movie.reviews
data.postValue(movie.reviews)
return data
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ internal constructor() {

init {
Timber.d("Injection NetworkBoundRepository")
val loadedFromDB = this.loadFromDb()
result.addSource(loadedFromDB) { data ->
result.removeSource(loadedFromDB)
if (shouldFetch(data)) {
result.postValue(Resource.loading(null))
fetchFromNetwork(loadedFromDB)
} else {
result.addSource<ResultType>(loadedFromDB) { newData ->
setValue(Resource.success(newData))
AppExecutors.diskIO().execute {
val loadedFromDB = this.loadFromDb()
AppExecutors.mainThread().execute {
result.addSource(loadedFromDB) { data ->
result.removeSource(loadedFromDB)
if (shouldFetch(data)) {
result.postValue(Resource.loading(null))
fetchFromNetwork(loadedFromDB)
} else {
result.addSource<ResultType>(loadedFromDB) { newData ->
setValue(Resource.success(newData))
}
}
}
}
}
Expand All @@ -67,8 +71,8 @@ internal constructor() {
AppExecutors.diskIO().execute {
response.body?.let {
saveFetchData(it)
val loaded = loadFromDb()
AppExecutors.mainThread().execute {
val loaded = loadFromDb()
result.addSource(loaded) { newData ->
newData?.let {
setValue(Resource.success(newData))
Expand Down Expand Up @@ -103,13 +107,10 @@ internal constructor() {
return result
}

@WorkerThread
protected abstract fun saveFetchData(items: RequestType)

@MainThread
protected abstract fun shouldFetch(data: ResultType?): Boolean

@MainThread
@WorkerThread
protected abstract fun loadFromDb(): LiveData<ResultType>

@MainThread
Expand All @@ -118,6 +119,9 @@ internal constructor() {
@MainThread
protected abstract fun mapper(): Mapper

@WorkerThread
protected abstract fun saveFetchData(items: RequestType)

@MainThread
protected abstract fun onFetchFailed(message: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PeopleRepository @Inject constructor(
override fun loadFromDb(): LiveData<PersonDetail> {
val person = peopleDao.getPerson(id_ = id)
val data: MutableLiveData<PersonDetail> = MutableLiveData()
data.value = person.personDetail
data.postValue(person.personDetail)
return data
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TvRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Keyword>> {
val movie = tvDao.getTv(id_ = id)
val data: MutableLiveData<List<Keyword>> = MutableLiveData()
data.value = movie.keywords
data.postValue(movie.keywords)
return data
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class TvRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Video>> {
val movie = tvDao.getTv(id_ = id)
val data: MutableLiveData<List<Video>> = MutableLiveData()
data.value = movie.videos
data.postValue(movie.videos)
return data
}

Expand Down Expand Up @@ -134,7 +134,7 @@ class TvRepository @Inject constructor(
override fun loadFromDb(): LiveData<List<Review>> {
val movie = tvDao.getTv(id_ = id)
val data: MutableLiveData<List<Review>> = MutableLiveData()
data.value = movie.reviews
data.postValue(movie.reviews)
return data
}

Expand Down

0 comments on commit 8cbff35

Please sign in to comment.