Avoid some crashes

This commit is contained in:
arkon 2022-10-26 18:12:11 -04:00
parent 3fb42b6ce9
commit ca06516900
3 changed files with 7 additions and 8 deletions

View File

@ -52,7 +52,7 @@ class CategoryRepositoryImpl(
} }
override suspend fun updatePartial(updates: List<CategoryUpdate>) { override suspend fun updatePartial(updates: List<CategoryUpdate>) {
handler.await(true) { handler.await(inTransaction = true) {
for (update in updates) { for (update in updates) {
updatePartialBlocking(update) updatePartialBlocking(update)
} }

View File

@ -74,7 +74,7 @@ class MangaRepositoryImpl(
} }
override suspend fun insert(manga: Manga): Long? { override suspend fun insert(manga: Manga): Long? {
return handler.awaitOneOrNull { return handler.awaitOneOrNull(inTransaction = true) {
mangasQueries.insert( mangasQueries.insert(
source = manga.source, source = manga.source,
url = manga.url, url = manga.url,

View File

@ -205,7 +205,7 @@ class DownloadCache(
fun removeManga(manga: Manga) { fun removeManga(manga: Manga) {
val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return
val mangaDirName = provider.getMangaDirName(manga.title) val mangaDirName = provider.getMangaDirName(manga.title)
if (mangaDirName in sourceDir.mangaDirs) { if (sourceDir.mangaDirs.containsKey(mangaDirName)) {
sourceDir.mangaDirs -= mangaDirName sourceDir.mangaDirs -= mangaDirName
} }
@ -271,9 +271,8 @@ class DownloadCache(
val mangaDirs = sourceDir.dir.listFiles().orEmpty() val mangaDirs = sourceDir.dir.listFiles().orEmpty()
.filterNot { it.name.isNullOrBlank() } .filterNot { it.name.isNullOrBlank() }
.associate { it.name!! to MangaDirectory(it) } .associate { it.name!! to MangaDirectory(it) }
.toMutableMap()
sourceDir.mangaDirs = mangaDirs sourceDir.mangaDirs = ConcurrentHashMap(mangaDirs)
mangaDirs.values.forEach { mangaDir -> mangaDirs.values.forEach { mangaDir ->
val chapterDirs = mangaDir.dir.listFiles().orEmpty() 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. * Returns a new map containing only the key entries of [transform] that are not null.
*/ */
private inline fun <K, V, R> Map<out K, V>.mapNotNullKeys(transform: (Map.Entry<K?, V>) -> R?): MutableMap<R, V> { private inline fun <K, V, R> Map<out K, V>.mapNotNullKeys(transform: (Map.Entry<K?, V>) -> R?): ConcurrentHashMap<R, V> {
val mutableMap = ConcurrentHashMap<R, V>() val mutableMap = ConcurrentHashMap<R, V>()
forEach { element -> transform(element)?.let { mutableMap[it] = element.value } } forEach { element -> transform(element)?.let { mutableMap[it] = element.value } }
return mutableMap return mutableMap
@ -320,7 +319,7 @@ class DownloadCache(
*/ */
private class RootDirectory( private class RootDirectory(
val dir: UniFile, val dir: UniFile,
var sourceDirs: MutableMap<Long, SourceDirectory> = mutableMapOf(), var sourceDirs: ConcurrentHashMap<Long, SourceDirectory> = ConcurrentHashMap(),
) )
/** /**
@ -328,7 +327,7 @@ private class RootDirectory(
*/ */
private class SourceDirectory( private class SourceDirectory(
val dir: UniFile, val dir: UniFile,
var mangaDirs: MutableMap<String, MangaDirectory> = mutableMapOf(), var mangaDirs: ConcurrentHashMap<String, MangaDirectory> = ConcurrentHashMap(),
) )
/** /**