Skip to content

Commit

Permalink
updated to use domain layer in a smart way
Browse files Browse the repository at this point in the history
  • Loading branch information
swbain committed Jan 4, 2019
1 parent 85584ec commit abee762
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stephenbain.pagingsamples.data.datasource

import com.stephenbain.pagingsamples.data.db.PlaylistDao
import com.stephenbain.pagingsamples.data.domain.GetMyPlaylists
import com.stephenbain.pagingsamples.data.domain.UpdatePlaylists
import com.stephenbain.pagingsamples.data.model.Playlist
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
Expand All @@ -11,19 +10,16 @@ import timber.log.Timber
class PlaylistsBoundaryCallback(
private val pageSize: Int,
scope: CoroutineScope,
private val playlistDao: PlaylistDao,
private val getMyPlaylists: GetMyPlaylists
private val updatePlaylists: UpdatePlaylists
) : PositionalBoundaryCallback<Playlist>(pageSize, scope) {

override suspend fun initialLoad() {
val paging = getMyPlaylists(limit = pageSize)
playlistDao.insertAll(paging.items)
updatePlaylists(limit = pageSize)
}

override suspend fun itemAtEndLoad(offset: Int) {
try {
val paging = getMyPlaylists(limit = pageSize, offset = playlistDao.getItemCount())
playlistDao.insertAll(paging.items)
updatePlaylists(limit = pageSize)
} catch (t: Throwable) {
Timber.e(t, "error getting playlists")
// hack around rate limiting. delay by 200 and retry
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.stephenbain.pagingsamples.data.domain

import com.stephenbain.pagingsamples.data.db.PlaylistDao
import com.stephenbain.pagingsamples.data.repo.TokenRepository
import com.stephenbain.pagingsamples.data.retrofit.SpotifyService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject


class UpdatePlaylists @Inject constructor(
private val spotifyService: SpotifyService,
private val playlistDao: PlaylistDao,
private val tokenRepository: TokenRepository
) {

suspend operator fun invoke(limit: Int) {
withContext(Dispatchers.IO) {
val paging = spotifyService.getMyPlaylists(
token = "Bearer ${tokenRepository.token}",
offset = playlistDao.getItemCount(),
limit = limit
).await()
playlistDao.insertAll(paging.items)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.stephenbain.pagingsamples.data.datasource.NewReleasesDataSourceFactory
import com.stephenbain.pagingsamples.data.datasource.PlaylistsBoundaryCallback
import com.stephenbain.pagingsamples.data.db.PlaylistDao
import com.stephenbain.pagingsamples.data.domain.GetMyPlaylists
import com.stephenbain.pagingsamples.data.domain.UpdatePlaylists
import com.stephenbain.pagingsamples.data.model.Playlist
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -17,7 +16,7 @@ import javax.inject.Inject

class MyPlaylistsViewModel @Inject constructor(
private val playlistDao: PlaylistDao,
private val getMyPlaylists: GetMyPlaylists
private val updatePlaylists: UpdatePlaylists
) : ViewModel() {

private val parentJob = Job()
Expand All @@ -33,7 +32,7 @@ class MyPlaylistsViewModel @Inject constructor(

val factory = playlistDao.getAllPlaylists()
return LivePagedListBuilder(factory, config)
.setBoundaryCallback(PlaylistsBoundaryCallback(20, scope, playlistDao, getMyPlaylists))
.setBoundaryCallback(PlaylistsBoundaryCallback(20, scope, updatePlaylists))
.build()
}

Expand Down

0 comments on commit abee762

Please sign in to comment.