mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
Removing Globalscope usage in CoverCache
This commit is contained in:
parent
7f0b58156d
commit
bf746dd7fb
2 changed files with 39 additions and 34 deletions
|
@ -11,6 +11,8 @@ import eu.kanade.tachiyomi.data.database.models.MangaImpl
|
|||
import eu.kanade.tachiyomi.util.storage.DiskUtil
|
||||
import eu.kanade.tachiyomi.util.system.executeOnIO
|
||||
import eu.kanade.tachiyomi.util.system.toast
|
||||
import eu.kanade.tachiyomi.util.system.withIOContext
|
||||
import eu.kanade.tachiyomi.util.system.withUIContext
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -66,15 +68,14 @@ class CoverCache(val context: Context) {
|
|||
return Formatter.formatFileSize(context, DiskUtil.getDirectorySize(onlineCoverDirectory))
|
||||
}
|
||||
|
||||
fun deleteOldCovers() {
|
||||
GlobalScope.launch(Dispatchers.Default) {
|
||||
suspend fun deleteOldCovers() {
|
||||
val db = Injekt.get<DatabaseHelper>()
|
||||
var deletedSize = 0L
|
||||
val urls = db.getLibraryMangas().executeOnIO().mapNotNull {
|
||||
it.thumbnail_url?.let { url -> return@mapNotNull DiskUtil.hashKeyForDisk(url) }
|
||||
null
|
||||
}
|
||||
val files = cacheDir.listFiles()?.iterator() ?: return@launch
|
||||
val files = cacheDir.listFiles()?.iterator() ?: return
|
||||
while (files.hasNext()) {
|
||||
val file = files.next()
|
||||
if (file.isFile && file.name !in urls) {
|
||||
|
@ -82,7 +83,7 @@ class CoverCache(val context: Context) {
|
|||
file.delete()
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
withUIContext {
|
||||
context.toast(
|
||||
context.getString(
|
||||
R.string.deleted_,
|
||||
|
@ -91,17 +92,15 @@ class CoverCache(val context: Context) {
|
|||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear out all online covers
|
||||
*/
|
||||
fun deleteAllCachedCovers() {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
suspend fun deleteAllCachedCovers() {
|
||||
val directory = onlineCoverDirectory
|
||||
var deletedSize = 0L
|
||||
val files =
|
||||
directory.listFiles()?.sortedBy { it.lastModified() }?.iterator() ?: return@launch
|
||||
directory.listFiles()?.sortedBy { it.lastModified() }?.iterator() ?: return
|
||||
while (files.hasNext()) {
|
||||
val file = files.next()
|
||||
deletedSize += file.length()
|
||||
|
@ -116,7 +115,6 @@ class CoverCache(val context: Context) {
|
|||
)
|
||||
}
|
||||
context.imageLoader.memoryCache.clear()
|
||||
}
|
||||
|
||||
lastClean = System.currentTimeMillis()
|
||||
}
|
||||
|
@ -124,18 +122,18 @@ class CoverCache(val context: Context) {
|
|||
/**
|
||||
* Clear out online covers until its under a certain size
|
||||
*/
|
||||
fun deleteCachedCovers() {
|
||||
suspend fun deleteCachedCovers() {
|
||||
withIOContext {
|
||||
if (lastClean + renewInterval < System.currentTimeMillis()) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val directory = onlineCoverDirectory
|
||||
val size = DiskUtil.getDirectorySize(directory)
|
||||
if (size <= maxOnlineCacheSize) {
|
||||
return@launch
|
||||
return@withIOContext
|
||||
}
|
||||
var deletedSize = 0L
|
||||
val files = directory.listFiles()?.sortedBy { it.lastModified() }?.iterator()
|
||||
?: return@launch
|
||||
?: return@withIOContext
|
||||
while (files.hasNext()) {
|
||||
val file = files.next()
|
||||
deletedSize += file.length()
|
||||
|
@ -147,10 +145,10 @@ class CoverCache(val context: Context) {
|
|||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
lastClean = System.currentTimeMillis()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the custom cover from cache.
|
||||
|
|
|
@ -8,7 +8,9 @@ import android.os.Bundle
|
|||
import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.net.toUri
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.PreferenceScreen
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.list.listItemsMultiChoice
|
||||
|
@ -27,6 +29,7 @@ import eu.kanade.tachiyomi.network.PREF_DOH_GOOGLE
|
|||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
import eu.kanade.tachiyomi.ui.base.controller.DialogController
|
||||
import eu.kanade.tachiyomi.util.CrashLogUtil
|
||||
import eu.kanade.tachiyomi.util.system.launchIO
|
||||
import eu.kanade.tachiyomi.util.system.launchUI
|
||||
import eu.kanade.tachiyomi.util.system.toast
|
||||
import eu.kanade.tachiyomi.util.view.openInBrowser
|
||||
|
@ -131,9 +134,11 @@ class SettingsAdvancedController : SettingsController() {
|
|||
|
||||
onClick {
|
||||
context.toast(R.string.starting_cleanup)
|
||||
(activity as? AppCompatActivity)?.lifecycleScope?.launchIO {
|
||||
coverCache.deleteOldCovers()
|
||||
}
|
||||
}
|
||||
}
|
||||
preference {
|
||||
key = "clear_cached_not_library"
|
||||
titleRes = R.string.clear_cached_covers_non_library
|
||||
|
@ -144,9 +149,11 @@ class SettingsAdvancedController : SettingsController() {
|
|||
|
||||
onClick {
|
||||
context.toast(R.string.starting_cleanup)
|
||||
(activity as? AppCompatActivity)?.lifecycleScope?.launchIO {
|
||||
coverCache.deleteAllCachedCovers()
|
||||
}
|
||||
}
|
||||
}
|
||||
preference {
|
||||
key = "clean_downloaded_chapters"
|
||||
titleRes = R.string.clean_up_downloaded_chapters
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue