From c06beac6601f583965656602023c839de7ade998 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 24 Aug 2023 22:41:22 -0400 Subject: [PATCH] Better ignore irrelevant files when indexing downloads --- .../java/eu/kanade/core/util/CollectionUtils.kt | 10 ---------- .../tachiyomi/data/download/DownloadCache.kt | 15 ++++++++------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt index 501763150..8dab8a054 100644 --- a/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt +++ b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt @@ -1,7 +1,6 @@ package eu.kanade.core.util import androidx.compose.ui.util.fastForEach -import java.util.concurrent.ConcurrentHashMap import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -20,15 +19,6 @@ fun List.insertSeparators( return newList } -/** - * Returns a new map containing only the key entries of [transform] that are not null. - */ -inline fun Map.mapNotNullKeys(transform: (Map.Entry) -> R?): ConcurrentHashMap { - val mutableMap = ConcurrentHashMap() - forEach { element -> transform(element)?.let { mutableMap[it] = element.value } } - return mutableMap -} - fun HashSet.addOrRemove(value: E, shouldAdd: Boolean) { if (shouldAdd) { add(value) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index f9391b3aa..81dfd7168 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -5,7 +5,6 @@ import android.content.Context import android.net.Uri import androidx.core.net.toUri import com.hippo.unifile.UniFile -import eu.kanade.core.util.mapNotNullKeys import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.source.Source import kotlinx.coroutines.CancellationException @@ -327,14 +326,16 @@ class DownloadCache( } } + val sourceMap = sources.associate { provider.getSourceDirName(it).lowercase() to it.id } + rootDownloadsDirLock.withLock { val sourceDirs = rootDownloadsDir.dir.listFiles().orEmpty() - .associate { it.name to SourceDirectory(it) } - .mapNotNullKeys { entry -> - sources.find { - provider.getSourceDirName(it).equals(entry.key, ignoreCase = true) - }?.id + .filter { it.isDirectory && !it.name.isNullOrBlank() } + .mapNotNull { dir -> + val sourceId = sourceMap[dir.name!!.lowercase()] + sourceId?.let { it to SourceDirectory(dir) } } + .toMap() rootDownloadsDir.sourceDirs = sourceDirs @@ -342,7 +343,7 @@ class DownloadCache( .map { sourceDir -> async { sourceDir.mangaDirs = sourceDir.dir.listFiles().orEmpty() - .filterNot { it.name.isNullOrBlank() } + .filter { it.isDirectory && !it.name.isNullOrBlank() } .associate { it.name!! to MangaDirectory(it) } sourceDir.mangaDirs.values.forEach { mangaDir ->