From a3f3f9d5626c120619e0f4cc7c2d82045e065201 Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 6 Dec 2022 22:20:13 -0500 Subject: [PATCH] Avoid some crashes --- .../tachiyomi/ui/category/CategoryScreen.kt | 1 - .../tachiyomi/ui/library/LibraryScreenModel.kt | 17 ++++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryScreen.kt index 0d10363b62..e71f43640a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryScreen.kt @@ -20,7 +20,6 @@ import kotlinx.coroutines.flow.collectLatest class CategoryScreen : Screen { - // Fix certain crash when wrapped inside a Controller override val key = uniqueScreenKey @Composable diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt index 6204c9346a..af8d9779db 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt @@ -589,7 +589,7 @@ class LibraryScreenModel( return withIOContext { state.value .getLibraryItemsByCategoryId(activeCategory.toLong()) - .randomOrNull() + ?.randomOrNull() } } @@ -624,7 +624,7 @@ class LibraryScreenModel( } val items = state.getLibraryItemsByCategoryId(manga.category) - .fastMap { it.libraryManga } + ?.fastMap { it.libraryManga }.orEmpty() val lastMangaIndex = items.indexOf(lastSelected) val curMangaIndex = items.indexOf(manga) @@ -649,12 +649,11 @@ class LibraryScreenModel( val newSelection = state.selection.toMutableList().apply { val categoryId = state.categories.getOrNull(index)?.id ?: -1 val selectedIds = fastMap { it.id } - val newSelections = state.getLibraryItemsByCategoryId(categoryId) - .fastMapNotNull { item -> + state.getLibraryItemsByCategoryId(categoryId) + ?.fastMapNotNull { item -> item.libraryManga.takeUnless { it.id in selectedIds } } - - addAll(newSelections) + ?.let { addAll(it) } } state.copy(selection = newSelection) } @@ -664,7 +663,7 @@ class LibraryScreenModel( mutableState.update { state -> val newSelection = state.selection.toMutableList().apply { val categoryId = state.categories[index].id - val items = state.getLibraryItemsByCategoryId(categoryId).fastMap { it.libraryManga } + val items = state.getLibraryItemsByCategoryId(categoryId)?.fastMap { it.libraryManga }.orEmpty() val selectedIds = fastMap { it.id } val (toRemove, toAdd) = items.fastPartition { it.id in selectedIds } val toRemoveIds = toRemove.fastMap { it.id } @@ -755,8 +754,8 @@ class LibraryScreenModel( .size } - fun getLibraryItemsByCategoryId(categoryId: Long): List { - return library.firstNotNullOf { (k, v) -> v.takeIf { k.id == categoryId } } + fun getLibraryItemsByCategoryId(categoryId: Long): List? { + return library.firstNotNullOfOrNull { (k, v) -> v.takeIf { k.id == categoryId } } } fun getLibraryItemsByPage(page: Int): List {