11// SPDX-License-Identifier: Apache-2.0
2- // Copyright 2015-2024 Uwe Trottmann
2+ // SPDX-FileCopyrightText: Copyright © 2015 Uwe Trottmann <uwe@uwetrottmann.com>
33
44package com.battlelancer.seriesguide.shows.search.discover
55
@@ -8,14 +8,13 @@ import androidx.annotation.StringRes
88import com.battlelancer.seriesguide.R
99import com.battlelancer.seriesguide.SgApp
1010import com.battlelancer.seriesguide.shows.ShowsSettings
11- import com.battlelancer.seriesguide.traktapi.SgTrakt
12- import com.battlelancer.seriesguide.util.Errors
11+ import com.battlelancer.seriesguide.traktapi.TraktTools4
12+ import com.battlelancer.seriesguide.traktapi.TraktTools4.TraktNonNullResponse.Success
1313import com.uwetrottmann.androidutils.AndroidUtils
1414import com.uwetrottmann.androidutils.GenericSimpleLoader
1515import com.uwetrottmann.trakt5.TraktV2
16- import com.uwetrottmann.trakt5.entities.BaseShow
17- import com.uwetrottmann.trakt5.enums.Extended
18- import retrofit2.Response
16+ import kotlinx.coroutines.Dispatchers
17+ import kotlinx.coroutines.runBlocking
1918import java.util.LinkedList
2019
2120/* *
@@ -48,46 +47,28 @@ class TraktAddLoader(
4847 private val trakt: TraktV2 = SgApp .getServicesComponent(context).trakt()
4948
5049 override fun loadInBackground (): Result {
51- var shows: List <BaseShow > = emptyList()
52- var action: String? = null
53- try {
54- val response: Response <List <BaseShow >>
50+ val response = runBlocking(Dispatchers .Default ) {
51+ val traktSync = trakt.sync()
5552 when (type) {
56- Type .WATCHED -> {
57- action = " load watched shows"
58- response = trakt.sync().watchedShows(Extended .NOSEASONS ).execute()
59- }
60-
61- Type .COLLECTION -> {
62- action = " load show collection"
63- response = trakt.sync().collectionShows(1 , 1000 ,null ).execute()
64- }
53+ Type .WATCHED -> TraktTools4 .getWatchedShows(traktSync, noSeasons = true )
54+ Type .COLLECTION -> TraktTools4 .getCollectedShows(traktSync)
55+ Type .WATCHLIST -> TraktTools4 .getShowsOnWatchlist(traktSync)
56+ }
57+ }
6558
66- Type . WATCHLIST -> {
67- action = " load show watchlist "
68- response = trakt.sync().watchlistShows( Extended . FULL ).execute()
69- }
59+ val shows = when (response) {
60+ is Success -> response.data
61+ is TraktTools4 . TraktErrorResponse . IsUnauthorized -> {
62+ return buildResultFailure( R .string.trakt_error_credentials)
7063 }
7164
72- if (response.isSuccessful) {
73- val body = response.body()
74- if (body != null ) {
75- shows = body
65+ else -> {
66+ // Wait to check for network until here to allow hitting the response cache
67+ return if (AndroidUtils .isNetworkConnected(context)) {
68+ buildResultGenericFailure()
69+ } else {
70+ buildResultFailure(R .string.offline)
7671 }
77- } else {
78- if (SgTrakt .isUnauthorized(context, response)) {
79- return buildResultFailure(R .string.trakt_error_credentials)
80- }
81- Errors .logAndReport(action, response)
82- return buildResultGenericFailure()
83- }
84- } catch (e: Exception ) {
85- Errors .logAndReport(action!! , e)
86- // only check for network here to allow hitting the response cache
87- return if (AndroidUtils .isNetworkConnected(context)) {
88- buildResultGenericFailure()
89- } else {
90- buildResultFailure(R .string.offline)
9172 }
9273 }
9374
0 commit comments