From 7357724392f7ebac85f1fdaefa2523a7eb6f9beb Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Fri, 1 Mar 2024 15:30:19 +0800 Subject: [PATCH] Address GlobalScope usage and minor cleanup --- .../com/hippo/ehviewer/EhApplication.kt | 35 ++++++------------- .../ehviewer/legacy/CleanupObsoleteCache.kt | 21 +++++------ .../com/hippo/ehviewer/util/FileUtils.kt | 10 ++++++ .../util/lang/CoroutinesExtensions.kt | 14 -------- 4 files changed, 29 insertions(+), 51 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt index c568fb86c..6b12e3739 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt @@ -65,13 +65,10 @@ import eu.kanade.tachiyomi.util.system.logcat import io.ktor.client.HttpClient import io.ktor.client.engine.okhttp.OkHttp import io.ktor.client.plugins.cookies.HttpCookies -import java.io.File -import kotlinx.coroutines.ensureActive import kotlinx.coroutines.launch import logcat.AndroidLogcatLogger import logcat.LogPriority import logcat.LogcatLogger -import moe.tarsin.coroutines.runSuspendCatching import okhttp3.AsyncDns import okhttp3.android.AndroidAsyncDns import okio.Path.Companion.toOkioPath @@ -110,41 +107,31 @@ class EhApplication : Application(), SingletonImageLoader.Factory { System.loadLibrary("ehviewer") ReadableTime.initialize(this) lifecycleScope.launchIO { - launchIO { + launch { EhTagDatabase } - launchIO { + launch { EhDB } - launchIO { + launch { DownloadManager.readPagesFromLocal() } - launchIO { - runSuspendCatching { - val files = mutableListOf() - AppConfig.externalCrashDir?.listFiles()?.let { files.addAll(it) } - AppConfig.externalParseErrorDir?.listFiles()?.let { files.addAll(it) } - files.sortByDescending { it.lastModified() } - files.forEachIndexed { index, file -> - ensureActive() - if (index > 9) { - file.delete() - } - } - }.onFailure { - logcat(it) - } + launch { + FileUtils.cleanupDirectory(AppConfig.externalCrashDir) + FileUtils.cleanupDirectory(AppConfig.externalParseErrorDir) } - launchIO { + launch { cleanupDownload() } if (Settings.requestNews) { - launchIO { + launch { checkDawn() } } + launch { + cleanObsoleteCache() + } } - cleanObsoleteCache(this) if (BuildConfig.DEBUG) { StrictMode.enableDefaults() Snapshot.registerApplyObserver { anies, _ -> diff --git a/app/src/main/kotlin/com/hippo/ehviewer/legacy/CleanupObsoleteCache.kt b/app/src/main/kotlin/com/hippo/ehviewer/legacy/CleanupObsoleteCache.kt index 6e5f27486..5417e1242 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/legacy/CleanupObsoleteCache.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/legacy/CleanupObsoleteCache.kt @@ -1,9 +1,7 @@ package com.hippo.ehviewer.legacy -import android.app.Application -import eu.kanade.tachiyomi.util.lang.launchIO import java.io.File -import kotlinx.coroutines.DelicateCoroutinesApi +import splitties.init.appCtx private val OBSOLETE_CACHE_DIRS = arrayOf( "image", @@ -12,16 +10,13 @@ private val OBSOLETE_CACHE_DIRS = arrayOf( "spider_info", ) -@OptIn(DelicateCoroutinesApi::class) -fun cleanObsoleteCache(application: Application) { - launchIO { - application.deleteDatabase("hosts.db") - val dir = application.cacheDir - for (subdir in OBSOLETE_CACHE_DIRS) { - val file = File(dir, subdir) - if (file.exists()) { - file.deleteRecursively() - } +fun cleanObsoleteCache() { + appCtx.deleteDatabase("hosts.db") + val dir = appCtx.cacheDir + for (subdir in OBSOLETE_CACHE_DIRS) { + val file = File(dir, subdir) + if (file.exists()) { + file.deleteRecursively() } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt index c9b6fb069..ffb10a90c 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt @@ -80,6 +80,15 @@ object FileUtils { return success } + fun cleanupDirectory(dir: File?, maxFiles: Int = 10) { + dir?.listFiles()?.let { files -> + files.sortByDescending { it.lastModified() } + files.forEachIndexed { index, file -> + if (index >= maxFiles) file.delete() + } + } + } + fun sanitizeFilename(rawFilename: String): String { // Remove forbidden_filename_characters var filename = rawFilename @@ -157,6 +166,7 @@ object FileUtils { } } +@Suppress("BlockingMethodInNonBlockingContext") suspend fun ByteReadChannel.copyTo(file: File) { RandomAccessFile(file, "rw").use { copyTo(it.channel) diff --git a/app/src/main/kotlin/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt b/app/src/main/kotlin/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt index 3b7cf25e1..9135cc08e 100644 --- a/app/src/main/kotlin/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt +++ b/app/src/main/kotlin/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt @@ -1,26 +1,12 @@ package eu.kanade.tachiyomi.util.lang import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.CoroutineStart -import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -/** - * Think twice before using this. This is a delicate API. It is easy to accidentally create resource or memory leaks when GlobalScope is used. - * - * **Possible replacements** - * - suspend function - * - custom scope like view or presenter scope - */ -@DelicateCoroutinesApi -fun launchIO(block: suspend CoroutineScope.() -> Unit): Job = - GlobalScope.launch(Dispatchers.IO, CoroutineStart.DEFAULT, block) - fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job = launch(Dispatchers.Main, block = block)