mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-04 22:05:08 +01:00
Convert more TrackService methods to coroutines
This commit is contained in:
parent
8d68859c2a
commit
2d0a5eb02c
@ -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<T : AbstractBackupManager>(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}")
|
||||
|
@ -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 {
|
||||
|
@ -46,11 +46,11 @@ abstract class TrackService(val id: Int) {
|
||||
|
||||
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 refresh(track: Track): Observable<Track>
|
||||
abstract suspend fun refresh(track: Track): Track
|
||||
|
||||
abstract suspend fun login(username: String, password: String)
|
||||
|
||||
|
@ -146,10 +146,9 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
|
||||
return api.updateLibManga(track)
|
||||
}
|
||||
|
||||
override fun bind(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun bind(track: Track): Track {
|
||||
val remoteTrack = api.findLibManga(track, getUsername().toInt())
|
||||
if (remoteTrack != null) {
|
||||
return if (remoteTrack != null) {
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.library_id = remoteTrack.library_id
|
||||
update(track)
|
||||
@ -159,20 +158,17 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
|
||||
track.status = DEFAULT_STATUS
|
||||
add(track)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun search(query: String): Observable<List<TrackSearch>> {
|
||||
return runAsObservable({ api.search(query) })
|
||||
}
|
||||
|
||||
override fun refresh(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun refresh(track: Track): Track {
|
||||
val remoteTrack = api.getLibManga(track, getUsername().toInt())
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.total_chapters = remoteTrack.total_chapters
|
||||
track
|
||||
})
|
||||
return track
|
||||
}
|
||||
|
||||
override suspend fun login(username: String, password: String) = login(password)
|
||||
|
@ -39,11 +39,10 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
|
||||
return api.updateLibManga(track)
|
||||
}
|
||||
|
||||
override fun bind(track: Track): Observable<Track> {
|
||||
return runAsObservable({ api.statusLibManga(track) })
|
||||
.flatMap {
|
||||
runAsObservable({ api.findLibManga(track) }).flatMap { remoteTrack ->
|
||||
if (remoteTrack != null && it != null) {
|
||||
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
|
||||
@ -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
|
||||
track.score = DEFAULT_SCORE.toFloat()
|
||||
track.status = DEFAULT_STATUS
|
||||
runAsObservable({ add(track) })
|
||||
runAsObservable({ update(track) })
|
||||
}
|
||||
}
|
||||
add(track)
|
||||
update(track)
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,16 +61,14 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
|
||||
return runAsObservable({ api.search(query) })
|
||||
}
|
||||
|
||||
override fun refresh(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
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
|
||||
}
|
||||
track
|
||||
})
|
||||
return track
|
||||
}
|
||||
|
||||
override fun getLogo() = R.drawable.ic_tracker_bangumi
|
||||
|
@ -78,10 +78,9 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
|
||||
return api.updateLibManga(track)
|
||||
}
|
||||
|
||||
override fun bind(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun bind(track: Track): Track {
|
||||
val remoteTrack = api.findLibManga(track, getUserId())
|
||||
if (remoteTrack != null) {
|
||||
return if (remoteTrack != null) {
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.media_id = remoteTrack.media_id
|
||||
update(track)
|
||||
@ -90,20 +89,17 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
|
||||
track.status = DEFAULT_STATUS
|
||||
add(track)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun search(query: String): Observable<List<TrackSearch>> {
|
||||
return runAsObservable({ api.search(query) })
|
||||
}
|
||||
|
||||
override fun refresh(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun refresh(track: Track): Track {
|
||||
val remoteTrack = api.getLibManga(track)
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.total_chapters = remoteTrack.total_chapters
|
||||
track
|
||||
})
|
||||
return track
|
||||
}
|
||||
|
||||
override suspend fun login(username: String, password: String) {
|
||||
|
@ -72,17 +72,15 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
|
||||
return api.updateItem(track)
|
||||
}
|
||||
|
||||
override fun bind(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun bind(track: Track): Track {
|
||||
val remoteTrack = api.findListItem(track)
|
||||
if (remoteTrack != null) {
|
||||
return if (remoteTrack != null) {
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.media_id = remoteTrack.media_id
|
||||
update(track)
|
||||
} else {
|
||||
add(track)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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) })
|
||||
}
|
||||
|
||||
override fun refresh(track: Track): Observable<Track> {
|
||||
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)
|
||||
|
@ -51,10 +51,9 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
|
||||
return api.updateLibManga(track, getUsername())
|
||||
}
|
||||
|
||||
override fun bind(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun bind(track: Track): Track {
|
||||
val remoteTrack = api.findLibManga(track, getUsername())
|
||||
if (remoteTrack != null) {
|
||||
return if (remoteTrack != null) {
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.library_id = remoteTrack.library_id
|
||||
update(track)
|
||||
@ -64,21 +63,18 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
|
||||
track.status = DEFAULT_STATUS
|
||||
add(track)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun search(query: String): Observable<List<TrackSearch>> {
|
||||
return runAsObservable({ api.search(query) })
|
||||
}
|
||||
|
||||
override fun refresh(track: Track): Observable<Track> {
|
||||
return runAsObservable({
|
||||
override suspend fun refresh(track: Track): Track {
|
||||
api.findLibManga(track, getUsername())?.let { remoteTrack ->
|
||||
track.copyPersonalFrom(remoteTrack)
|
||||
track.total_chapters = remoteTrack.total_chapters
|
||||
}
|
||||
track
|
||||
})
|
||||
return track
|
||||
}
|
||||
|
||||
override fun getLogo() = R.drawable.ic_tracker_shikimori
|
||||
|
@ -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(
|
||||
launchIO {
|
||||
try {
|
||||
service.bind(item)
|
||||
.flatMap { db.insertTrack(item).asRxObservable() }
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{ },
|
||||
{ error -> context.toast(error.message) }
|
||||
)
|
||||
)
|
||||
db.insertTrack(item).await()
|
||||
} catch (e: Throwable) {
|
||||
launchUI { context.toast(e.message) }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unregisterTracking(service)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user