From 271e4400146f146903ddce98c3068ae29e0433cd Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Thu, 17 Apr 2025 17:09:00 +0700 Subject: [PATCH] fix: Prevent potential "Comparison method violates its general contract!" crash --- CHANGELOG.md | 2 ++ .../tachiyomi/ui/library/LibraryPresenter.kt | 15 +++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be74d403d8..d63fcc879b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is simplified version of [Keep a Changelog](https://keepachangelog.co ### Changes - Temporarily disable log file +- Categories' header now show filtered count when you search the library (If you have "Show number of items" enabled) ### Fixes - Allow users to bypass onboarding's permission step if Shizuku is installed @@ -23,6 +24,7 @@ The format is simplified version of [Keep a Changelog](https://keepachangelog.co - Fix not fully loaded entries can't be selected on Library page - Fix certain Infinix devices being unable to use any "Open link in browser" actions, including tracker setup (@MajorTanya) - Fix source filter bottom sheet unable to be fully scrolled to the bottom +- Prevent potential "Comparison method violates its general contract!" crash ### Translation - Update translations from Weblate diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index e5d8db30c1..e82f372dcc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -660,6 +660,9 @@ class LibraryPresenter( * @param itemList the map to sort. */ private fun LibraryMap.applySort(): LibraryMap { + // Making sure `allCategories` is stable for `.sort()` + val categoryOrderMap = allCategories.associate { it.id to it.order } + val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> val category = i1.header.category val compare = when { @@ -688,12 +691,8 @@ class LibraryPresenter( LibrarySort.DateAdded -> i2.manga.manga.date_added.compareTo(i1.manga.manga.date_added) LibrarySort.DragAndDrop -> { if (category.isDynamic) { - val category1 = - allCategories.find { i1.manga.category == it.id }?.order - ?: 0 - val category2 = - allCategories.find { i2.manga.category == it.id }?.order - ?: 0 + val category1 = categoryOrderMap[i1.manga.category] ?: 0 + val category2 = categoryOrderMap[i2.manga.category] ?: 0 category1.compareTo(category2) } else { sortAlphabetical(i1, i2) @@ -727,7 +726,7 @@ class LibraryPresenter( } return this.mapValues { (category, values) -> - // Making sure category has valid sort + // Making sure category has valid sort before doing the actual sorting if (category.mangaOrder.isEmpty() && category.mangaSort == null) { category.changeSortTo(preferences.librarySortingMode().get()) if (category.id == 0) { @@ -804,7 +803,7 @@ class LibraryPresenter( preferences.groupLibraryBy().changes(), preferences.showAllCategories().changes(), - + preferences.librarySortingMode().changes(), preferences.librarySortingAscending().changes(),