From d88dbe6409b9c4dd7fbb37757e89c09af73f1fd3 Mon Sep 17 00:00:00 2001 From: krysanify Date: Thu, 7 Aug 2025 21:48:21 +0800 Subject: [PATCH] Fix crash opening filter sheet with empty library and mark as read/unread for selected items (#2355) --- CHANGELOG.md | 2 ++ .../tachiyomi/ui/library/LibraryScreenModel.kt | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e143e947..e8e54a485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ The format is a modified version of [Keep a Changelog](https://keepachangelog.co - Fix local source EPUB files not loading ([@AntsyLich](https://github.com/AntsyLich)) ([#2369](https://github.com/mihonapp/mihon/pull/2369)) - Fix title text color in light mode on mass migration list ([@AntsyLich](https://github.com/AntsyLich)) ([#2370](https://github.com/mihonapp/mihon/pull/2370)) - Fix 'Default' category showing in library with no user-added categories ([@AntsyLich](https://github.com/AntsyLich)) ([#2371](https://github.com/mihonapp/mihon/pull/2371)) +- Fix crash when opening filter sheet with an empty library ([@krysanify](https://github.com/krysanify/)) ([#2355](https://github.com/mihonapp/mihon/pull/2355)) +- Fix mark as read/unread not working for selected library items ([@krysanify](https://github.com/krysanify/)) ([#2355](https://github.com/mihonapp/mihon/pull/2355)) ## [v0.19.0] - 2025-08-04 ### Added 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 d07609340..c5ac3131e 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 @@ -499,8 +499,9 @@ class LibraryScreenModel( * Marks mangas' chapters read status. */ fun markReadSelection(read: Boolean) { + val selection = state.value.selectedManga screenModelScope.launchNonCancellable { - state.value.selectedManga.forEach { manga -> + selection.forEach { manga -> setReadStatus.await( manga = manga, read = read, @@ -573,7 +574,7 @@ class LibraryScreenModel( fun getRandomLibraryItemForCurrentCategory(): LibraryItem? { val state = state.value - return state.getItemsForCategoryId(state.activeCategory.id).randomOrNull() + return state.getItemsForCategoryId(state.activeCategory?.id).randomOrNull() } fun showSettingsDialog() { @@ -631,7 +632,7 @@ class LibraryScreenModel( lastSelectionCategory = null mutableState.update { state -> val newSelection = state.selection.mutate { list -> - state.getItemsForCategoryId(state.activeCategory.id).map { it.id }.let(list::addAll) + state.getItemsForCategoryId(state.activeCategory?.id).map { it.id }.let(list::addAll) } state.copy(selection = newSelection) } @@ -641,7 +642,7 @@ class LibraryScreenModel( lastSelectionCategory = null mutableState.update { state -> val newSelection = state.selection.mutate { list -> - val itemIds = state.getItemsForCategoryId(state.activeCategory.id).fastMap { it.id } + val itemIds = state.getItemsForCategoryId(state.activeCategory?.id).fastMap { it.id } val (toRemove, toAdd) = itemIds.partition { it in list } list.removeAll(toRemove) list.addAll(toAdd) @@ -756,7 +757,7 @@ class LibraryScreenModel( maximumValue = displayedCategories.lastIndex.coerceAtLeast(0), ) - val activeCategory: Category by lazy { displayedCategories[coercedActiveCategoryIndex] } + val activeCategory: Category? = displayedCategories.getOrNull(coercedActiveCategoryIndex) val isLibraryEmpty = libraryData.favorites.isEmpty() @@ -764,7 +765,8 @@ class LibraryScreenModel( val selectedManga by lazy { selection.mapNotNull { libraryData.favoritesById[it]?.libraryManga?.manga } } - fun getItemsForCategoryId(categoryId: Long): List { + fun getItemsForCategoryId(categoryId: Long?): List { + if (categoryId == null) return emptyList() val category = displayedCategories.find { it.id == categoryId } ?: return emptyList() return getItemsForCategory(category) }