refactor: Move file that supposed to be in common

Also rename launchNonCancellable to launchNonCancellableIO and change the comment in LibraryPresenter to be more accurate.
This commit is contained in:
Ahmad Ansori Palembani 2024-06-20 09:28:46 +07:00
parent 8cfe5a1182
commit ff08e19880
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
6 changed files with 16 additions and 16 deletions

View file

@ -44,7 +44,7 @@ import eu.kanade.tachiyomi.util.lang.removeArticles
import eu.kanade.tachiyomi.util.manga.MangaCoverMetadata
import eu.kanade.tachiyomi.util.mapStatus
import eu.kanade.tachiyomi.util.system.executeOnIO
import eu.kanade.tachiyomi.util.system.launchNonCancellable
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
import eu.kanade.tachiyomi.util.system.launchUI
import eu.kanade.tachiyomi.util.system.withIOContext
import eu.kanade.tachiyomi.util.system.withUIContext
@ -751,7 +751,7 @@ class LibraryPresenter(
val libraryManga = libraryMangaList.mapNotNull {
// header item is used to identify which category the library manga is actually belong to.
// because J2K have "fake tabbed" option and "show all" option.
// because J2K have an option to show everything in a single category.
val headerItem = if (headerItems == null) { catItemAll } else { headerItems[it.category] }
?: return@mapNotNull null
categorySet.add(it.category)
@ -1147,7 +1147,7 @@ class LibraryPresenter(
/** Remove manga from the library and delete the downloads */
fun confirmDeletion(mangas: List<Manga>, coverCacheToo: Boolean = true) {
presenterScope.launchNonCancellable {
presenterScope.launchNonCancellableIO {
val mangaToDelete = mangas.distinctBy { it.id }
mangaToDelete.forEach { manga ->
if (coverCacheToo) {
@ -1363,7 +1363,7 @@ class LibraryPresenter(
markRead: Boolean,
): HashMap<Manga, List<Chapter>> {
val mapMangaChapters = HashMap<Manga, List<Chapter>>()
presenterScope.launchNonCancellable {
presenterScope.launchNonCancellableIO {
mangaList.forEach { manga ->
val chapters = getChapters.await(manga)
val updates = chapters.copy().mapNotNull {
@ -1382,7 +1382,7 @@ class LibraryPresenter(
fun undoMarkReadStatus(
mangaList: HashMap<Manga, List<Chapter>>,
) {
presenterScope.launchNonCancellable {
presenterScope.launchNonCancellableIO {
val updates = mangaList.values.map { chapters ->
chapters.mapNotNull {
if (it.id == null) return@mapNotNull null

View file

@ -126,7 +126,7 @@ import eu.kanade.tachiyomi.util.system.isInNightMode
import eu.kanade.tachiyomi.util.system.isLTR
import eu.kanade.tachiyomi.util.system.isTablet
import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.launchNonCancellable
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
import eu.kanade.tachiyomi.util.system.launchUI
import eu.kanade.tachiyomi.util.system.materialAlertDialog
import eu.kanade.tachiyomi.util.system.openInBrowser
@ -355,7 +355,7 @@ class ReaderActivity : BaseActivity<ReaderActivityBinding>() {
finish()
return
}
lifecycleScope.launchNonCancellable {
lifecycleScope.launchNonCancellableIO {
val initResult = viewModel.init(manga, chapter)
if (!initResult.getOrDefault(false)) {
val exception = initResult.exceptionOrNull() ?: IllegalStateException("Unknown err")

View file

@ -48,7 +48,7 @@ import eu.kanade.tachiyomi.util.system.ImageUtil
import eu.kanade.tachiyomi.util.system.e
import eu.kanade.tachiyomi.util.system.executeOnIO
import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.launchNonCancellable
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
import eu.kanade.tachiyomi.util.system.localeContext
import eu.kanade.tachiyomi.util.system.withIOContext
import eu.kanade.tachiyomi.util.system.withUIContext
@ -826,7 +826,7 @@ class ReaderViewModel(
notifier.onClear()
// Copy file in background.
viewModelScope.launchNonCancellable {
viewModelScope.launchNonCancellableIO {
try {
val file = saveImage(page, destDir, manga)
DiskUtil.scanMedia(context, file)
@ -882,7 +882,7 @@ class ReaderViewModel(
val destDir = UniFile.fromFile(context.cacheDir)!!.createDirectory("shared_image")!!
viewModelScope.launchNonCancellable {
viewModelScope.launchNonCancellableIO {
val file = saveImage(page, destDir, manga)
eventChannel.send(Event.ShareImage(file, page))
}
@ -912,7 +912,7 @@ class ReaderViewModel(
val manga = manga ?: return
val stream = page.stream ?: return
viewModelScope.launchNonCancellable {
viewModelScope.launchNonCancellableIO {
val result = try {
if (manga.isLocal()) {
val context = Injekt.get<Application>()

View file

@ -37,7 +37,7 @@ import eu.kanade.tachiyomi.util.compose.LocalAlertDialog
import eu.kanade.tachiyomi.util.compose.currentOrThrow
import eu.kanade.tachiyomi.util.system.DeviceUtil
import eu.kanade.tachiyomi.util.system.e
import eu.kanade.tachiyomi.util.system.launchNonCancellable
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.system.withUIContext
import kotlinx.collections.immutable.persistentListOf
@ -253,7 +253,7 @@ object SettingsDataScreen : ComposableSettings {
title = stringResource(R.string.clear_chapter_cache),
subtitle = stringResource(R.string.used_, cacheReadableSize),
onClick = {
scope.launchNonCancellable {
scope.launchNonCancellableIO {
try {
val deletedFiles = chapterCache.clear()
withUIContext {
@ -279,7 +279,7 @@ object SettingsDataScreen : ComposableSettings {
),
onClick = {
context.toast(R.string.starting_cleanup)
scope.launchNonCancellable {
scope.launchNonCancellableIO {
coverCache.deleteAllCachedCovers()
}
}
@ -292,7 +292,7 @@ object SettingsDataScreen : ComposableSettings {
),
onClick = {
context.toast(R.string.starting_cleanup)
scope.launchNonCancellable {
scope.launchNonCancellableIO {
coverCache.deleteOldCovers()
}
}

View file

@ -28,7 +28,7 @@ fun CoroutineScope.launchIO(block: suspend CoroutineScope.() -> Unit): Job =
fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job =
launch(Dispatchers.Main, block = block)
fun CoroutineScope.launchNonCancellable(block: suspend CoroutineScope.() -> Unit): Job =
fun CoroutineScope.launchNonCancellableIO(block: suspend CoroutineScope.() -> Unit): Job =
launchIO { withContext(NonCancellable, block) }
suspend fun <T> withUIContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block)