Avoid some crashes

(cherry picked from commit a3f3f9d5626c120619e0f4cc7c2d82045e065201)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt
This commit is contained in:
arkon 2022-12-06 22:20:13 -05:00 committed by Jobobby04
parent b2df0340e4
commit c81da465f6
2 changed files with 8 additions and 10 deletions

View File

@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.collectLatest
class CategoryScreen : Screen { class CategoryScreen : Screen {
// Fix certain crash when wrapped inside a Controller
override val key = uniqueScreenKey override val key = uniqueScreenKey
@Composable @Composable

View File

@ -861,7 +861,7 @@ class LibraryScreenModel(
return withIOContext { return withIOContext {
state.value state.value
.getLibraryItemsByCategoryId(activeCategory.toLong()) .getLibraryItemsByCategoryId(activeCategory.toLong())
.randomOrNull() ?.randomOrNull()
} }
} }
@ -1072,7 +1072,7 @@ class LibraryScreenModel(
} }
val items = state.getLibraryItemsByCategoryId(manga.category) val items = state.getLibraryItemsByCategoryId(manga.category)
.fastMap { it.libraryManga } ?.fastMap { it.libraryManga }.orEmpty()
val lastMangaIndex = items.indexOf(lastSelected) val lastMangaIndex = items.indexOf(lastSelected)
val curMangaIndex = items.indexOf(manga) val curMangaIndex = items.indexOf(manga)
@ -1097,12 +1097,11 @@ class LibraryScreenModel(
val newSelection = state.selection.toMutableList().apply { val newSelection = state.selection.toMutableList().apply {
val categoryId = state.categories.getOrNull(index)?.id ?: -1 val categoryId = state.categories.getOrNull(index)?.id ?: -1
val selectedIds = fastMap { it.id } val selectedIds = fastMap { it.id }
val newSelections = state.getLibraryItemsByCategoryId(categoryId) state.getLibraryItemsByCategoryId(categoryId)
.fastMapNotNull { item -> ?.fastMapNotNull { item ->
item.libraryManga.takeUnless { it.id in selectedIds } item.libraryManga.takeUnless { it.id in selectedIds }
} }
?.let { addAll(it) }
addAll(newSelections)
} }
state.copy(selection = newSelection) state.copy(selection = newSelection)
} }
@ -1112,7 +1111,7 @@ class LibraryScreenModel(
mutableState.update { state -> mutableState.update { state ->
val newSelection = state.selection.toMutableList().apply { val newSelection = state.selection.toMutableList().apply {
val categoryId = state.categories[index].id 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 selectedIds = fastMap { it.id }
val (toRemove, toAdd) = items.fastPartition { it.id in selectedIds } val (toRemove, toAdd) = items.fastPartition { it.id in selectedIds }
val toRemoveIds = toRemove.fastMap { it.id } val toRemoveIds = toRemove.fastMap { it.id }
@ -1335,8 +1334,8 @@ class LibraryScreenModel(
} }
// SY <-- // SY <--
fun getLibraryItemsByCategoryId(categoryId: Long): List<LibraryItem> { fun getLibraryItemsByCategoryId(categoryId: Long): List<LibraryItem>? {
return library.firstNotNullOf { (k, v) -> v.takeIf { k.id == categoryId } } return library.firstNotNullOfOrNull { (k, v) -> v.takeIf { k.id == categoryId } }
} }
fun getLibraryItemsByPage(page: Int): List<LibraryItem> { fun getLibraryItemsByPage(page: Int): List<LibraryItem> {