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,33 +146,29 @@ 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()) return if (remoteTrack != null) {
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) } else {
} else { // 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 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 return track
track
})
} }
override suspend fun login(username: String, password: String) = login(password) override suspend fun login(username: String, password: String) = login(password)

View file

@ -39,41 +39,36 @@ 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 track.last_chapter_read = remoteTrack.last_chapter_read
track.last_chapter_read = remoteTrack.last_chapter_read refresh(track)
refresh(track) } else {
} else { // 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 add(track)
runAsObservable({ add(track) }) update(track)
runAsObservable({ update(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 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 }
} return track
track
})
} }
override fun getLogo() = R.drawable.ic_tracker_bangumi override fun getLogo() = R.drawable.ic_tracker_bangumi

View file

@ -78,32 +78,28 @@ 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()) return if (remoteTrack != null) {
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 { track.score = DEFAULT_SCORE
track.score = DEFAULT_SCORE 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 return track
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) return if (remoteTrack != null) {
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,34 +51,30 @@ 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()) return if (remoteTrack != null) {
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) } else {
} else { // 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 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 }
} return track
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 {
service.bind(item) try {
.flatMap { db.insertTrack(item).asRxObservable() } service.bind(item)
.subscribeOn(Schedulers.io()) db.insertTrack(item).await()
.observeOn(AndroidSchedulers.mainThread()) } catch (e: Throwable) {
.subscribe( launchUI { context.toast(e.message) }
{ }, }
{ error -> context.toast(error.message) } }
)
)
} else { } else {
unregisterTracking(service) unregisterTracking(service)
} }