From ca065169002475cc92b7736d02340127a5e419bd Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 26 Oct 2022 18:12:11 -0400 Subject: [PATCH] Avoid some crashes --- .../eu/kanade/data/category/CategoryRepositoryImpl.kt | 2 +- .../java/eu/kanade/data/manga/MangaRepositoryImpl.kt | 2 +- .../kanade/tachiyomi/data/download/DownloadCache.kt | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/data/category/CategoryRepositoryImpl.kt b/app/src/main/java/eu/kanade/data/category/CategoryRepositoryImpl.kt index 49be1bae74..cfbd2eb64b 100644 --- a/app/src/main/java/eu/kanade/data/category/CategoryRepositoryImpl.kt +++ b/app/src/main/java/eu/kanade/data/category/CategoryRepositoryImpl.kt @@ -52,7 +52,7 @@ class CategoryRepositoryImpl( } override suspend fun updatePartial(updates: List) { - handler.await(true) { + handler.await(inTransaction = true) { for (update in updates) { updatePartialBlocking(update) } diff --git a/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt b/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt index 650331167a..9fcb8f79de 100644 --- a/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt +++ b/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt @@ -74,7 +74,7 @@ class MangaRepositoryImpl( } override suspend fun insert(manga: Manga): Long? { - return handler.awaitOneOrNull { + return handler.awaitOneOrNull(inTransaction = true) { mangasQueries.insert( source = manga.source, url = manga.url, 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 9523d9ffc6..4868d8d6f5 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 @@ -205,7 +205,7 @@ class DownloadCache( fun removeManga(manga: Manga) { val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val mangaDirName = provider.getMangaDirName(manga.title) - if (mangaDirName in sourceDir.mangaDirs) { + if (sourceDir.mangaDirs.containsKey(mangaDirName)) { sourceDir.mangaDirs -= mangaDirName } @@ -271,9 +271,8 @@ class DownloadCache( val mangaDirs = sourceDir.dir.listFiles().orEmpty() .filterNot { it.name.isNullOrBlank() } .associate { it.name!! to MangaDirectory(it) } - .toMutableMap() - sourceDir.mangaDirs = mangaDirs + sourceDir.mangaDirs = ConcurrentHashMap(mangaDirs) mangaDirs.values.forEach { mangaDir -> val chapterDirs = mangaDir.dir.listFiles().orEmpty() @@ -308,7 +307,7 @@ class DownloadCache( /** * Returns a new map containing only the key entries of [transform] that are not null. */ - private inline fun Map.mapNotNullKeys(transform: (Map.Entry) -> R?): MutableMap { + private inline fun Map.mapNotNullKeys(transform: (Map.Entry) -> R?): ConcurrentHashMap { val mutableMap = ConcurrentHashMap() forEach { element -> transform(element)?.let { mutableMap[it] = element.value } } return mutableMap @@ -320,7 +319,7 @@ class DownloadCache( */ private class RootDirectory( val dir: UniFile, - var sourceDirs: MutableMap = mutableMapOf(), + var sourceDirs: ConcurrentHashMap = ConcurrentHashMap(), ) /** @@ -328,7 +327,7 @@ private class RootDirectory( */ private class SourceDirectory( val dir: UniFile, - var mangaDirs: MutableMap = mutableMapOf(), + var mangaDirs: ConcurrentHashMap = ConcurrentHashMap(), ) /**