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.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}")

View file

@ -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 {

View file

@ -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)

View file

@ -146,33 +146,29 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track)
}
override fun bind(track: Track): Observable<Track> {
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<List<TrackSearch>> {
return runAsObservable({ api.search(query) })
}
override fun refresh(track: Track): Observable<Track> {
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)

View file

@ -39,41 +39,36 @@ 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) {
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<List<TrackSearch>> {
return runAsObservable({ api.search(query) })
}
override fun refresh(track: Track): Observable<Track> {
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

View file

@ -78,32 +78,28 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
return api.updateLibManga(track)
}
override fun bind(track: Track): Observable<Track> {
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<List<TrackSearch>> {
return runAsObservable({ api.search(query) })
}
override fun refresh(track: Track): Observable<Track> {
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) {

View file

@ -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({
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<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)

View file

@ -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<Track> {
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<List<TrackSearch>> {
return runAsObservable({ api.search(query) })
}
override fun refresh(track: Track): Observable<Track> {
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

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.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)
}