Convert more TrackService methods to coroutines

This commit is contained in:
arkon 2021-01-03 23:33:21 -05:00
parent 8d68859c2a
commit 2d0a5eb02c
9 changed files with 101 additions and 120 deletions

View File

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.chapter.NoChaptersException import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.lang.runAsObservable
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import rx.Observable import rx.Observable
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -89,7 +90,7 @@ abstract class AbstractBackupRestore<T : AbstractBackupManager>(protected val co
.flatMap { track -> .flatMap { track ->
val service = trackManager.getService(track.sync_id) val service = trackManager.getService(track.sync_id)
if (service != null && service.isLogged) { if (service != null && service.isLogged) {
service.refresh(track) runAsObservable({ service.refresh(track) })
.doOnNext { db.insertTrack(it).executeAsBlocking() } .doOnNext { db.insertTrack(it).executeAsBlocking() }
.onErrorReturn { .onErrorReturn {
errors.add(Date() to "${manga.title} - ${it.message}") errors.add(Date() to "${manga.title} - ${it.message}")

View File

@ -419,7 +419,7 @@ class LibraryUpdateService(
.concatMap { track -> .concatMap { track ->
val service = trackManager.getService(track.sync_id) val service = trackManager.getService(track.sync_id)
if (service != null && service in loggedServices) { if (service != null && service in loggedServices) {
service.refresh(track) runAsObservable({ service.refresh(track) })
.doOnNext { db.insertTrack(it).executeAsBlocking() } .doOnNext { db.insertTrack(it).executeAsBlocking() }
.onErrorReturn { track } .onErrorReturn { track }
} else { } else {

View File

@ -46,11 +46,11 @@ abstract class TrackService(val id: Int) {
abstract suspend fun update(track: Track): Track abstract suspend fun update(track: Track): Track
abstract fun bind(track: Track): Observable<Track> abstract suspend fun bind(track: Track): Track
abstract fun search(query: String): Observable<List<TrackSearch>> abstract fun search(query: String): Observable<List<TrackSearch>>
abstract fun refresh(track: Track): Observable<Track> abstract suspend fun refresh(track: Track): Track
abstract suspend fun login(username: String, password: String) abstract suspend fun login(username: String, password: String)

View File

@ -146,10 +146,9 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track) return api.updateLibManga(track)
} }
override fun bind(track: Track): Observable<Track> { override suspend fun bind(track: Track): Track {
return runAsObservable({
val remoteTrack = api.findLibManga(track, getUsername().toInt()) val remoteTrack = api.findLibManga(track, getUsername().toInt())
if (remoteTrack != null) { return if (remoteTrack != null) {
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.library_id = remoteTrack.library_id track.library_id = remoteTrack.library_id
update(track) update(track)
@ -159,20 +158,17 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
track.status = DEFAULT_STATUS track.status = DEFAULT_STATUS
add(track) add(track)
} }
})
} }
override fun search(query: String): Observable<List<TrackSearch>> { override fun search(query: String): Observable<List<TrackSearch>> {
return runAsObservable({ api.search(query) }) return runAsObservable({ api.search(query) })
} }
override fun refresh(track: Track): Observable<Track> { override suspend fun refresh(track: Track): Track {
return runAsObservable({
val remoteTrack = api.getLibManga(track, getUsername().toInt()) val remoteTrack = api.getLibManga(track, getUsername().toInt())
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.total_chapters = remoteTrack.total_chapters track.total_chapters = remoteTrack.total_chapters
track return track
})
} }
override suspend fun login(username: String, password: String) = login(password) override suspend fun login(username: String, password: String) = login(password)

View File

@ -39,11 +39,10 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track) return api.updateLibManga(track)
} }
override fun bind(track: Track): Observable<Track> { override suspend fun bind(track: Track): Track {
return runAsObservable({ api.statusLibManga(track) }) val statusTrack = api.statusLibManga(track)
.flatMap { val remoteTrack = api.findLibManga(track)
runAsObservable({ api.findLibManga(track) }).flatMap { remoteTrack -> return if (remoteTrack != null && statusTrack != null) {
if (remoteTrack != null && it != null) {
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.library_id = remoteTrack.library_id track.library_id = remoteTrack.library_id
track.status = remoteTrack.status track.status = remoteTrack.status
@ -53,10 +52,8 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
// Set default fields if it's not found in the list // Set default fields if it's not found in the list
track.score = DEFAULT_SCORE.toFloat() track.score = DEFAULT_SCORE.toFloat()
track.status = DEFAULT_STATUS track.status = DEFAULT_STATUS
runAsObservable({ add(track) }) add(track)
runAsObservable({ update(track) }) update(track)
}
}
} }
} }
@ -64,16 +61,14 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
return runAsObservable({ api.search(query) }) return runAsObservable({ api.search(query) })
} }
override fun refresh(track: Track): Observable<Track> { override suspend fun refresh(track: Track): Track {
return runAsObservable({
val remoteStatusTrack = api.statusLibManga(track) val remoteStatusTrack = api.statusLibManga(track)
track.copyPersonalFrom(remoteStatusTrack!!) track.copyPersonalFrom(remoteStatusTrack!!)
api.findLibManga(track)?.let { remoteTrack -> api.findLibManga(track)?.let { remoteTrack ->
track.total_chapters = remoteTrack.total_chapters track.total_chapters = remoteTrack.total_chapters
track.status = remoteTrack.status track.status = remoteTrack.status
} }
track return track
})
} }
override fun getLogo() = R.drawable.ic_tracker_bangumi override fun getLogo() = R.drawable.ic_tracker_bangumi

View File

@ -78,10 +78,9 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track) return api.updateLibManga(track)
} }
override fun bind(track: Track): Observable<Track> { override suspend fun bind(track: Track): Track {
return runAsObservable({
val remoteTrack = api.findLibManga(track, getUserId()) val remoteTrack = api.findLibManga(track, getUserId())
if (remoteTrack != null) { return if (remoteTrack != null) {
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.media_id = remoteTrack.media_id track.media_id = remoteTrack.media_id
update(track) update(track)
@ -90,20 +89,17 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
track.status = DEFAULT_STATUS track.status = DEFAULT_STATUS
add(track) add(track)
} }
})
} }
override fun search(query: String): Observable<List<TrackSearch>> { override fun search(query: String): Observable<List<TrackSearch>> {
return runAsObservable({ api.search(query) }) return runAsObservable({ api.search(query) })
} }
override fun refresh(track: Track): Observable<Track> { override suspend fun refresh(track: Track): Track {
return runAsObservable({
val remoteTrack = api.getLibManga(track) val remoteTrack = api.getLibManga(track)
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.total_chapters = remoteTrack.total_chapters track.total_chapters = remoteTrack.total_chapters
track return track
})
} }
override suspend fun login(username: String, password: String) { override suspend fun login(username: String, password: String) {

View File

@ -72,17 +72,15 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
return api.updateItem(track) return api.updateItem(track)
} }
override fun bind(track: Track): Observable<Track> { override suspend fun bind(track: Track): Track {
return runAsObservable({
val remoteTrack = api.findListItem(track) val remoteTrack = api.findListItem(track)
if (remoteTrack != null) { return if (remoteTrack != null) {
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.media_id = remoteTrack.media_id track.media_id = remoteTrack.media_id
update(track) update(track)
} else { } else {
add(track) add(track)
} }
})
} }
override fun search(query: String): Observable<List<TrackSearch>> { override fun search(query: String): Observable<List<TrackSearch>> {
@ -95,8 +93,8 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
return runAsObservable({ api.search(query) }) return runAsObservable({ api.search(query) })
} }
override fun refresh(track: Track): Observable<Track> { override suspend fun refresh(track: Track): Track {
return runAsObservable({ api.getListItem(track) }) return api.getListItem(track)
} }
override suspend fun login(username: String, password: String) = login(password) override suspend fun login(username: String, password: String) = login(password)

View File

@ -51,10 +51,9 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track, getUsername()) return api.updateLibManga(track, getUsername())
} }
override fun bind(track: Track): Observable<Track> { override suspend fun bind(track: Track): Track {
return runAsObservable({
val remoteTrack = api.findLibManga(track, getUsername()) val remoteTrack = api.findLibManga(track, getUsername())
if (remoteTrack != null) { return if (remoteTrack != null) {
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.library_id = remoteTrack.library_id track.library_id = remoteTrack.library_id
update(track) update(track)
@ -64,21 +63,18 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
track.status = DEFAULT_STATUS track.status = DEFAULT_STATUS
add(track) add(track)
} }
})
} }
override fun search(query: String): Observable<List<TrackSearch>> { override fun search(query: String): Observable<List<TrackSearch>> {
return runAsObservable({ api.search(query) }) return runAsObservable({ api.search(query) })
} }
override fun refresh(track: Track): Observable<Track> { override suspend fun refresh(track: Track): Track {
return runAsObservable({
api.findLibManga(track, getUsername())?.let { remoteTrack -> api.findLibManga(track, getUsername())?.let { remoteTrack ->
track.copyPersonalFrom(remoteTrack) track.copyPersonalFrom(remoteTrack)
track.total_chapters = remoteTrack.total_chapters track.total_chapters = remoteTrack.total_chapters
} }
track return track
})
} }
override fun getLogo() = R.drawable.ic_tracker_shikimori override fun getLogo() = R.drawable.ic_tracker_shikimori

View File

@ -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.await
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.runAsObservable
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
@ -62,7 +63,7 @@ class TrackPresenter(
refreshSubscription = Observable.from(trackList) refreshSubscription = Observable.from(trackList)
.filter { it.track != null } .filter { it.track != null }
.flatMap { item -> .flatMap { item ->
item.service.refresh(item.track!!) runAsObservable({ item.service.refresh(item.track!!) })
.flatMap { db.insertTrack(it).asRxObservable() } .flatMap { db.insertTrack(it).asRxObservable() }
.map { item } .map { item }
.onErrorReturn { item } .onErrorReturn { item }
@ -90,16 +91,14 @@ class TrackPresenter(
fun registerTracking(item: Track?, service: TrackService) { fun registerTracking(item: Track?, service: TrackService) {
if (item != null) { if (item != null) {
item.manga_id = manga.id!! item.manga_id = manga.id!!
add( launchIO {
try {
service.bind(item) service.bind(item)
.flatMap { db.insertTrack(item).asRxObservable() } db.insertTrack(item).await()
.subscribeOn(Schedulers.io()) } catch (e: Throwable) {
.observeOn(AndroidSchedulers.mainThread()) launchUI { context.toast(e.message) }
.subscribe( }
{ }, }
{ error -> context.toast(error.message) }
)
)
} else { } else {
unregisterTracking(service) unregisterTracking(service)
} }