diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt index 9a0926e3b..c0219b40f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.util.chapter.NoChaptersException +import eu.kanade.tachiyomi.util.lang.runAsObservable import kotlinx.coroutines.Job import rx.Observable import uy.kohesive.injekt.injectLazy @@ -89,7 +90,7 @@ abstract class AbstractBackupRestore(protected val co .flatMap { track -> val service = trackManager.getService(track.sync_id) if (service != null && service.isLogged) { - service.refresh(track) + runAsObservable({ service.refresh(track) }) .doOnNext { db.insertTrack(it).executeAsBlocking() } .onErrorReturn { errors.add(Date() to "${manga.title} - ${it.message}") diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index adfccb11e..9450c0d1f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -419,7 +419,7 @@ class LibraryUpdateService( .concatMap { track -> val service = trackManager.getService(track.sync_id) if (service != null && service in loggedServices) { - service.refresh(track) + runAsObservable({ service.refresh(track) }) .doOnNext { db.insertTrack(it).executeAsBlocking() } .onErrorReturn { track } } else { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt index c1c57389f..5dfc7462f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt @@ -46,11 +46,11 @@ abstract class TrackService(val id: Int) { abstract suspend fun update(track: Track): Track - abstract fun bind(track: Track): Observable + abstract suspend fun bind(track: Track): Track abstract fun search(query: String): Observable> - abstract fun refresh(track: Track): Observable + abstract suspend fun refresh(track: Track): Track abstract suspend fun login(username: String, password: String) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt index 997d3c3c3..416641800 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt @@ -146,33 +146,29 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) { return api.updateLibManga(track) } - override fun bind(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.findLibManga(track, getUsername().toInt()) - if (remoteTrack != null) { - track.copyPersonalFrom(remoteTrack) - track.library_id = remoteTrack.library_id - update(track) - } else { - // Set default fields if it's not found in the list - track.score = DEFAULT_SCORE.toFloat() - track.status = DEFAULT_STATUS - add(track) - } - }) + override suspend fun bind(track: Track): Track { + val remoteTrack = api.findLibManga(track, getUsername().toInt()) + return if (remoteTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.library_id = remoteTrack.library_id + update(track) + } else { + // Set default fields if it's not found in the list + track.score = DEFAULT_SCORE.toFloat() + track.status = DEFAULT_STATUS + add(track) + } } override fun search(query: String): Observable> { return runAsObservable({ api.search(query) }) } - override fun refresh(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.getLibManga(track, getUsername().toInt()) - track.copyPersonalFrom(remoteTrack) - track.total_chapters = remoteTrack.total_chapters - track - }) + override suspend fun refresh(track: Track): Track { + val remoteTrack = api.getLibManga(track, getUsername().toInt()) + track.copyPersonalFrom(remoteTrack) + track.total_chapters = remoteTrack.total_chapters + return track } override suspend fun login(username: String, password: String) = login(password) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt index e212704e7..7973d78c6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt @@ -39,41 +39,36 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) { return api.updateLibManga(track) } - override fun bind(track: Track): Observable { - return runAsObservable({ api.statusLibManga(track) }) - .flatMap { - runAsObservable({ api.findLibManga(track) }).flatMap { remoteTrack -> - if (remoteTrack != null && it != null) { - track.copyPersonalFrom(remoteTrack) - track.library_id = remoteTrack.library_id - track.status = remoteTrack.status - track.last_chapter_read = remoteTrack.last_chapter_read - refresh(track) - } else { - // Set default fields if it's not found in the list - track.score = DEFAULT_SCORE.toFloat() - track.status = DEFAULT_STATUS - runAsObservable({ add(track) }) - runAsObservable({ update(track) }) - } - } - } + override suspend fun bind(track: Track): Track { + val statusTrack = api.statusLibManga(track) + val remoteTrack = api.findLibManga(track) + return if (remoteTrack != null && statusTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.library_id = remoteTrack.library_id + track.status = remoteTrack.status + track.last_chapter_read = remoteTrack.last_chapter_read + refresh(track) + } else { + // Set default fields if it's not found in the list + track.score = DEFAULT_SCORE.toFloat() + track.status = DEFAULT_STATUS + add(track) + update(track) + } } override fun search(query: String): Observable> { return runAsObservable({ api.search(query) }) } - override fun refresh(track: Track): Observable { - return runAsObservable({ - val remoteStatusTrack = api.statusLibManga(track) - track.copyPersonalFrom(remoteStatusTrack!!) - api.findLibManga(track)?.let { remoteTrack -> - track.total_chapters = remoteTrack.total_chapters - track.status = remoteTrack.status - } - track - }) + override suspend fun refresh(track: Track): Track { + val remoteStatusTrack = api.statusLibManga(track) + track.copyPersonalFrom(remoteStatusTrack!!) + api.findLibManga(track)?.let { remoteTrack -> + track.total_chapters = remoteTrack.total_chapters + track.status = remoteTrack.status + } + return track } override fun getLogo() = R.drawable.ic_tracker_bangumi diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt index 07a67be9d..279c969ce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt @@ -78,32 +78,28 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) { return api.updateLibManga(track) } - override fun bind(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.findLibManga(track, getUserId()) - if (remoteTrack != null) { - track.copyPersonalFrom(remoteTrack) - track.media_id = remoteTrack.media_id - update(track) - } else { - track.score = DEFAULT_SCORE - track.status = DEFAULT_STATUS - add(track) - } - }) + override suspend fun bind(track: Track): Track { + val remoteTrack = api.findLibManga(track, getUserId()) + return if (remoteTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.media_id = remoteTrack.media_id + update(track) + } else { + track.score = DEFAULT_SCORE + track.status = DEFAULT_STATUS + add(track) + } } override fun search(query: String): Observable> { return runAsObservable({ api.search(query) }) } - override fun refresh(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.getLibManga(track) - track.copyPersonalFrom(remoteTrack) - track.total_chapters = remoteTrack.total_chapters - track - }) + override suspend fun refresh(track: Track): Track { + val remoteTrack = api.getLibManga(track) + track.copyPersonalFrom(remoteTrack) + track.total_chapters = remoteTrack.total_chapters + return track } override suspend fun login(username: String, password: String) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index 0fcb12188..416957031 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -72,17 +72,15 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { return api.updateItem(track) } - override fun bind(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.findListItem(track) - if (remoteTrack != null) { - track.copyPersonalFrom(remoteTrack) - track.media_id = remoteTrack.media_id - update(track) - } else { - add(track) - } - }) + override suspend fun bind(track: Track): Track { + val remoteTrack = api.findListItem(track) + return if (remoteTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.media_id = remoteTrack.media_id + update(track) + } else { + add(track) + } } override fun search(query: String): Observable> { @@ -95,8 +93,8 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { return runAsObservable({ api.search(query) }) } - override fun refresh(track: Track): Observable { - return runAsObservable({ api.getListItem(track) }) + override suspend fun refresh(track: Track): Track { + return api.getListItem(track) } override suspend fun login(username: String, password: String) = login(password) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt index d1880b517..99b8ae396 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt @@ -51,34 +51,30 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) { return api.updateLibManga(track, getUsername()) } - override fun bind(track: Track): Observable { - return runAsObservable({ - val remoteTrack = api.findLibManga(track, getUsername()) - if (remoteTrack != null) { - track.copyPersonalFrom(remoteTrack) - track.library_id = remoteTrack.library_id - update(track) - } else { - // Set default fields if it's not found in the list - track.score = DEFAULT_SCORE.toFloat() - track.status = DEFAULT_STATUS - add(track) - } - }) + override suspend fun bind(track: Track): Track { + val remoteTrack = api.findLibManga(track, getUsername()) + return if (remoteTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.library_id = remoteTrack.library_id + update(track) + } else { + // Set default fields if it's not found in the list + track.score = DEFAULT_SCORE.toFloat() + track.status = DEFAULT_STATUS + add(track) + } } override fun search(query: String): Observable> { return runAsObservable({ api.search(query) }) } - override fun refresh(track: Track): Observable { - return runAsObservable({ - api.findLibManga(track, getUsername())?.let { remoteTrack -> - track.copyPersonalFrom(remoteTrack) - track.total_chapters = remoteTrack.total_chapters - } - track - }) + override suspend fun refresh(track: Track): Track { + api.findLibManga(track, getUsername())?.let { remoteTrack -> + track.copyPersonalFrom(remoteTrack) + track.total_chapters = remoteTrack.total_chapters + } + return track } override fun getLogo() = R.drawable.ic_tracker_shikimori diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackPresenter.kt index ff3c2130c..d93ea1757 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackPresenter.kt @@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.util.lang.await import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchUI +import eu.kanade.tachiyomi.util.lang.runAsObservable import eu.kanade.tachiyomi.util.system.toast import rx.Observable import rx.Subscription @@ -62,7 +63,7 @@ class TrackPresenter( refreshSubscription = Observable.from(trackList) .filter { it.track != null } .flatMap { item -> - item.service.refresh(item.track!!) + runAsObservable({ item.service.refresh(item.track!!) }) .flatMap { db.insertTrack(it).asRxObservable() } .map { item } .onErrorReturn { item } @@ -90,16 +91,14 @@ class TrackPresenter( fun registerTracking(item: Track?, service: TrackService) { if (item != null) { item.manga_id = manga.id!! - add( - service.bind(item) - .flatMap { db.insertTrack(item).asRxObservable() } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe( - { }, - { error -> context.toast(error.message) } - ) - ) + launchIO { + try { + service.bind(item) + db.insertTrack(item).await() + } catch (e: Throwable) { + launchUI { context.toast(e.message) } + } + } } else { unregisterTracking(service) }