From 006c3580534ca44b0d42815ef4a5d30b7809a5b7 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Tue, 12 Mar 2024 19:07:24 +0100 Subject: [PATCH] fix: Sort lists by title wherever they occur --- app/src/main/java/app/pachli/ListsActivity.kt | 3 ++- app/src/main/java/app/pachli/MainActivity.kt | 4 ++-- app/src/main/java/app/pachli/TabPreferenceActivity.kt | 3 ++- .../pachli/core/data/repository/ListsRepository.kt | 11 +++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/pachli/ListsActivity.kt b/app/src/main/java/app/pachli/ListsActivity.kt index e30451096..8810c2bca 100644 --- a/app/src/main/java/app/pachli/ListsActivity.kt +++ b/app/src/main/java/app/pachli/ListsActivity.kt @@ -43,6 +43,7 @@ import app.pachli.core.common.extensions.hide import app.pachli.core.common.extensions.show import app.pachli.core.common.extensions.viewBinding import app.pachli.core.data.repository.Lists +import app.pachli.core.data.repository.ListsRepository.Companion.compareByListTitle import app.pachli.core.navigation.StatusListActivityIntent import app.pachli.core.network.model.MastoList import app.pachli.core.network.retrofit.apiresult.ApiError @@ -210,7 +211,7 @@ class ListsActivity : BaseActivity(), MenuProvider { state.onSuccess { lists -> when (lists) { is Lists.Loaded -> { - adapter.submitList(lists.lists.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.title })) + adapter.submitList(lists.lists.sortedWith(compareByListTitle)) binding.swipeRefreshLayout.isRefreshing = false if (lists.lists.isEmpty()) { binding.messageView.show() diff --git a/app/src/main/java/app/pachli/MainActivity.kt b/app/src/main/java/app/pachli/MainActivity.kt index 7d9ed5ad7..9fb452551 100644 --- a/app/src/main/java/app/pachli/MainActivity.kt +++ b/app/src/main/java/app/pachli/MainActivity.kt @@ -76,6 +76,7 @@ import app.pachli.core.common.extensions.show import app.pachli.core.common.extensions.viewBinding import app.pachli.core.data.repository.Lists import app.pachli.core.data.repository.ListsRepository +import app.pachli.core.data.repository.ListsRepository.Companion.compareByListTitle import app.pachli.core.database.model.AccountEntity import app.pachli.core.database.model.TabData import app.pachli.core.designsystem.EmbeddedFontFamily @@ -605,8 +606,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider { when (result) { Lists.Loading -> Pair(emptyList(), R.string.title_lists_loading) is Lists.Loaded -> Pair( - result.lists - .sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.title }) + result.lists.sortedWith(compareByListTitle) .map { list -> primaryDrawerItem { nameText = list.title diff --git a/app/src/main/java/app/pachli/TabPreferenceActivity.kt b/app/src/main/java/app/pachli/TabPreferenceActivity.kt index b806e7040..ea2803b11 100644 --- a/app/src/main/java/app/pachli/TabPreferenceActivity.kt +++ b/app/src/main/java/app/pachli/TabPreferenceActivity.kt @@ -48,6 +48,7 @@ import app.pachli.core.common.extensions.viewBinding import app.pachli.core.common.extensions.visible import app.pachli.core.data.repository.Lists import app.pachli.core.data.repository.ListsRepository +import app.pachli.core.data.repository.ListsRepository.Companion.compareByListTitle import app.pachli.core.database.model.TabData import app.pachli.core.designsystem.R as DR import app.pachli.core.navigation.ListActivityIntent @@ -345,7 +346,7 @@ class TabPreferenceActivity : BaseActivity(), ItemInteractionListener { if (lists is Lists.Loaded) { showProgressBarJob.cancel() adapter.clear() - adapter.addAll(lists.lists) + adapter.addAll(lists.lists.sortedWith(compareByListTitle)) if (lists.lists.isEmpty()) noListsText.show() } } diff --git a/core/data/src/main/kotlin/app/pachli/core/data/repository/ListsRepository.kt b/core/data/src/main/kotlin/app/pachli/core/data/repository/ListsRepository.kt index 404577719..88a643e0c 100644 --- a/core/data/src/main/kotlin/app/pachli/core/data/repository/ListsRepository.kt +++ b/core/data/src/main/kotlin/app/pachli/core/data/repository/ListsRepository.kt @@ -21,6 +21,7 @@ import app.pachli.core.network.model.MastoList import app.pachli.core.network.model.TimelineAccount import app.pachli.core.network.retrofit.apiresult.ApiError import com.github.michaelbull.result.Result +import java.text.Collator import kotlinx.coroutines.flow.StateFlow sealed interface Lists { @@ -122,4 +123,14 @@ interface ListsRepository { * @return A successful result, or an error */ suspend fun deleteAccountsFromList(listId: String, accountIds: List): Result + + companion object { + /** + * Locale-aware comparator for lists. Case-insenstive comparison by + * the list's title. + */ + val compareByListTitle: Comparator = compareBy( + Collator.getInstance().apply { strength = Collator.SECONDARY }, + ) { it.title } + } }