Skip to content
Permalink
Browse files

Tidy up followed list id fetching

We now handle exceptions and errors correctly

Closes #427
  • Loading branch information...
chrisbanes committed Aug 25, 2019
1 parent 0449b72 commit 64542c0b155ce2d0ac894f93ab87bbf56493e00a
@@ -18,6 +18,7 @@ package app.tivi.data.repositories

import app.tivi.data.TiviEntityInserter
import app.tivi.data.daos.FollowedShowsDao
import app.tivi.data.entities.ErrorResult
import app.tivi.data.entities.Success
import app.tivi.data.repositories.followedshows.FollowedShowsDataSource
import app.tivi.data.repositories.followedshows.FollowedShowsLastRequestStore
@@ -88,7 +89,7 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {

@Test
fun testSync() = runBlockingTest {
coEvery { traktDataSource.getFollowedListId() } returns 0
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow1Network to show))

repository.syncFollowedShows()
@@ -100,7 +101,7 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
fun testSync_emptyResponse() = runBlockingTest {
insertFollowedShow(db)

coEvery { traktDataSource.getFollowedListId() } returns 0
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
coEvery { traktDataSource.getListShows(0) } returns Success(emptyList())

repository.syncFollowedShows()
@@ -112,7 +113,7 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
fun testSync_responseDifferentShow() = runBlockingTest {
insertFollowedShow(db)

coEvery { traktDataSource.getFollowedListId() } returns 0
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow2Network to show2))

repository.syncFollowedShows()
@@ -124,8 +125,8 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
fun testSync_pendingDelete() = runBlockingTest {
followShowsDao.insert(followedShow1PendingDelete)

// Return null for the list ID so that we disable syncing
coEvery { traktDataSource.getFollowedListId() } returns null
// Return error for the list ID so that we disable syncing
coEvery { traktDataSource.getFollowedListId() } returns ErrorResult()

repository.syncFollowedShows()

@@ -136,8 +137,8 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
fun testSync_pendingAdd() = runBlockingTest {
followShowsDao.insert(followedShow1PendingUpload)

// Return null for the list ID so that we disable syncing
coEvery { traktDataSource.getFollowedListId() } returns null
// Return an error for the list ID so that we disable syncing
coEvery { traktDataSource.getFollowedListId() } returns ErrorResult()

repository.syncFollowedShows()

@@ -24,5 +24,5 @@ interface FollowedShowsDataSource {
suspend fun getListShows(listId: Int): Result<List<Pair<FollowedShowEntry, TiviShow>>>
suspend fun addShowIdsToList(listId: Int, shows: List<TiviShow>): Result<Unit>
suspend fun removeShowIdsFromList(listId: Int, shows: List<TiviShow>): Result<Unit>
suspend fun getFollowedListId(): Int?
suspend fun getFollowedListId(): Result<Int>
}
@@ -186,7 +186,12 @@ class FollowedShowsRepository @Inject constructor(
}

private suspend fun getFollowedTraktListId(): Int? {
if (followedShowsStore.traktListId == null) {
val result = dataSource.getFollowedListId()
if (result is Success) {
followedShowsStore.traktListId = result.get()
}
}
return followedShowsStore.traktListId
?: dataSource.getFollowedListId()?.also { followedShowsStore.traktListId = it }
}
}
@@ -16,12 +16,15 @@

package app.tivi.data.repositories.followedshows

import app.tivi.data.entities.ErrorResult
import app.tivi.data.entities.FollowedShowEntry
import app.tivi.data.entities.Result
import app.tivi.data.entities.Success
import app.tivi.data.entities.TiviShow
import app.tivi.data.mappers.TraktListEntryToFollowedShowEntry
import app.tivi.data.mappers.TraktListEntryToTiviShow
import app.tivi.data.mappers.pairMapperOf
import app.tivi.extensions.bodyOrThrow
import app.tivi.extensions.executeWithRetry
import app.tivi.extensions.toResult
import app.tivi.extensions.toResultUnit
@@ -33,6 +36,7 @@ import com.uwetrottmann.trakt5.entities.UserSlug
import com.uwetrottmann.trakt5.enums.Extended
import com.uwetrottmann.trakt5.enums.ListPrivacy
import com.uwetrottmann.trakt5.services.Users
import java.io.IOException
import javax.inject.Inject
import javax.inject.Provider

@@ -42,7 +46,7 @@ class TraktFollowedShowsDataSource @Inject constructor(
listEntryToFollowedEntry: TraktListEntryToFollowedShowEntry
) : FollowedShowsDataSource {
companion object {
private val LIST_NAME = "Following"
private val LIST_NAME = "Following TEST2"
}

private val listShowsMapper = pairMapperOf(listEntryToFollowedEntry, listEntryToShowMapper)
@@ -85,21 +89,29 @@ class TraktFollowedShowsDataSource @Inject constructor(
.toResult(listShowsMapper)
}

override suspend fun getFollowedListId(): Int? {
val id = usersService.get().lists(UserSlug.ME)
.executeWithRetry()
.toResult()
.get()
?.firstOrNull { it.name == LIST_NAME }
?.let { it.ids?.trakt }
if (id != null) {
return id
override suspend fun getFollowedListId(): Result<Int> {
val fetchResult: Result<Int>? = try {
usersService.get().lists(UserSlug.ME)
.executeWithRetry()
.bodyOrThrow()
.firstOrNull { it.name == LIST_NAME }
?.let { Success(it.ids.trakt) }
} catch (e: IOException) {
ErrorResult(e)
}

return usersService.get().createList(UserSlug.ME,
TraktList().name(LIST_NAME)!!.privacy(ListPrivacy.PRIVATE))
.executeWithRetry()
.toResult()
.let { it.get()?.ids?.trakt }
if (fetchResult is Success) {
return fetchResult
}

return try {
usersService.get().createList(UserSlug.ME,
TraktList().name(LIST_NAME).privacy(ListPrivacy.PRIVATE))
.executeWithRetry()
.bodyOrThrow()
.let { Success(it.ids.trakt) }
} catch (e: IOException) {
ErrorResult(e)
}
}
}

0 comments on commit 64542c0

Please sign in to comment.
You can’t perform that action at this time.