Avoid using global scope where appropriate

Also fixes the crash in tracking when an exception is thrown during a refresh.
This commit is contained in:
arkon
2021-01-08 18:05:51 -05:00
parent 96b8beb9cd
commit 2ffbee3db2
14 changed files with 88 additions and 81 deletions

View File

@@ -55,13 +55,13 @@ class SearchPresenter(
replacingMangaRelay.call(true)
launchIO {
presenterScope.launchIO {
val chapters = source.getChapterList(manga.toMangaInfo())
.map { it.toSChapter() }
migrateMangaInternal(source, chapters, prevManga, manga, replace)
}.invokeOnCompletion {
launchUI { replacingMangaRelay.call(false) }
presenterScope.launchUI { replacingMangaRelay.call(false) }
}
}

View File

@@ -31,7 +31,7 @@ import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateItem
import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateSectionItem
import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.removeCovers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asFlow
@@ -213,12 +213,12 @@ open class BrowseSourcePresenter(
* @param mangas the list of manga to initialize.
*/
fun initializeMangas(mangas: List<Manga>) {
launchIO {
presenterScope.launchIO {
mangas.asFlow()
.filter { it.thumbnail_url == null && !it.initialized }
.map { getMangaDetails(it) }
.onEach {
launchUI {
withUIContext {
@Suppress("DEPRECATION")
view?.onMangaInitialized(it)
}

View File

@@ -292,7 +292,7 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
setSelectedNavItem(startScreenId)
} else if (shouldHandleExitConfirmation()) {
// Exit confirmation (resets after 2 seconds)
launchUI { resetExitConfirmation() }
lifecycleScope.launchUI { resetExitConfirmation() }
} else if (backstackSize == 1 || !router.handleBack()) {
// Regular back
super.onBackPressed()

View File

@@ -26,7 +26,7 @@ import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.isLocal
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.withUIContext
import eu.kanade.tachiyomi.util.prepUpdateCover
import eu.kanade.tachiyomi.util.removeCovers
import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
@@ -161,7 +161,7 @@ class MangaPresenter(
*/
fun fetchMangaFromSource(manualFetch: Boolean = false) {
if (fetchMangaJob?.isActive == true) return
fetchMangaJob = launchIO {
fetchMangaJob = presenterScope.launchIO {
try {
val networkManga = source.getMangaDetails(manga.toMangaInfo())
val sManga = networkManga.toSManga()
@@ -170,9 +170,9 @@ class MangaPresenter(
manga.initialized = true
db.insertManga(manga).await()
launchUI { view?.onFetchMangaInfoDone() }
withUIContext { view?.onFetchMangaInfoDone() }
} catch (e: Throwable) {
launchUI { view?.onFetchMangaInfoError(e) }
withUIContext { view?.onFetchMangaInfoError(e) }
}
}
}
@@ -360,9 +360,9 @@ class MangaPresenter(
downloadNewChapters(newChapters)
}
launchUI { view?.onFetchChaptersDone() }
withUIContext { view?.onFetchChaptersDone() }
} catch (e: Throwable) {
launchUI { view?.onFetchChaptersError(e) }
withUIContext { view?.onFetchChaptersError(e) }
}
}
}

View File

@@ -10,11 +10,12 @@ import eu.kanade.tachiyomi.data.track.TrackService
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.withUIContext
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.supervisorScope
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import uy.kohesive.injekt.Injekt
@@ -59,20 +60,22 @@ class TrackPresenter(
fun refresh() {
refreshJob?.cancel()
refreshJob = launchIO {
try {
trackList
.filter { it.track != null }
.map {
async {
val track = it.service.refresh(it.track!!)
db.insertTrack(track).await()
supervisorScope {
try {
trackList
.filter { it.track != null }
.map {
async {
val track = it.service.refresh(it.track!!)
db.insertTrack(track).await()
}
}
}
.awaitAll()
.awaitAll()
view?.onRefreshDone()
} catch (e: Throwable) {
view?.onRefreshError(e)
withUIContext { view?.onRefreshDone() }
} catch (e: Throwable) {
withUIContext { view?.onRefreshError(e) }
}
}
}
}
@@ -82,9 +85,9 @@ class TrackPresenter(
searchJob = launchIO {
try {
val results = service.search(query)
launchUI { view?.onSearchResults(results) }
withUIContext { view?.onSearchResults(results) }
} catch (e: Throwable) {
launchUI { view?.onSearchResultsError(e) }
withUIContext { view?.onSearchResultsError(e) }
}
}
}
@@ -97,7 +100,7 @@ class TrackPresenter(
service.bind(item)
db.insertTrack(item).await()
} catch (e: Throwable) {
launchUI { context.toast(e.message) }
withUIContext { context.toast(e.message) }
}
}
} else {
@@ -114,9 +117,9 @@ class TrackPresenter(
try {
service.update(track)
db.insertTrack(track).await()
view?.onRefreshDone()
withUIContext { view?.onRefreshDone() }
} catch (e: Throwable) {
launchUI { view?.onRefreshError(e) }
withUIContext { view?.onRefreshError(e) }
// Restart on error to set old values
fetchTrackings()

View File

@@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.preference.LoginDialogPreference
import uy.kohesive.injekt.Injekt
@@ -46,11 +46,11 @@ class TrackLoginDialog(
try {
service.login(user, pass)
dialog?.dismiss()
launchUI { view?.context?.toast(R.string.login_success) }
withUIContext { view?.context?.toast(R.string.login_success) }
} catch (e: Throwable) {
binding?.login?.progress = -1
binding?.login?.setText(R.string.unknown_error)
launchUI { e.message?.let { view?.context?.toast(it) } }
withUIContext { e.message?.let { view?.context?.toast(it) } }
}
}
}