From bbaba3aad9fb088f8371389703b6a0b6413b6bb1 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 22:16:27 +0200 Subject: [PATCH] Fix pinned tags changing positions when renamed --- .../mm20/launcher2/database/SearchableDao.kt | 9 +++++++ .../searchable/SavableSearchableRepository.kt | 24 +++++++++++++++++++ .../services/tags/impl/TagsServiceImpl.kt | 3 +-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/data/database/src/main/java/de/mm20/launcher2/database/SearchableDao.kt b/data/database/src/main/java/de/mm20/launcher2/database/SearchableDao.kt index 92d6e41fa..46e233136 100644 --- a/data/database/src/main/java/de/mm20/launcher2/database/SearchableDao.kt +++ b/data/database/src/main/java/de/mm20/launcher2/database/SearchableDao.kt @@ -209,4 +209,13 @@ interface SearchableDao { @Query("SELECT pinPosition FROM Searchable WHERE `key` = :key UNION SELECT 0 as pinPosition ORDER BY pinPosition DESC LIMIT 1") fun isPinned(key: String): Flow + + @Transaction + suspend fun replace(key: String, item: SavedSearchableUpdateContentEntity) { + updateKey(key, item.key) + update(item) + } + + @Query("UPDATE Searchable SET `key` = :newKey WHERE `key` = :oldKey") + suspend fun updateKey(oldKey: String, newKey: String) } \ No newline at end of file diff --git a/data/searchable/src/main/java/de/mm20/launcher2/searchable/SavableSearchableRepository.kt b/data/searchable/src/main/java/de/mm20/launcher2/searchable/SavableSearchableRepository.kt index 65c273032..e185e51f0 100644 --- a/data/searchable/src/main/java/de/mm20/launcher2/searchable/SavableSearchableRepository.kt +++ b/data/searchable/src/main/java/de/mm20/launcher2/searchable/SavableSearchableRepository.kt @@ -6,6 +6,7 @@ import de.mm20.launcher2.backup.Backupable import de.mm20.launcher2.crashreporter.CrashReporter import de.mm20.launcher2.database.AppDatabase import de.mm20.launcher2.database.entities.SavedSearchableEntity +import de.mm20.launcher2.database.entities.SavedSearchableUpdateContentEntity import de.mm20.launcher2.database.entities.SavedSearchableUpdatePinEntity import de.mm20.launcher2.ktx.jsonObjectOf import de.mm20.launcher2.preferences.WeightFactor @@ -53,6 +54,16 @@ interface SavableSearchableRepository : Backupable { weight: Double? = null, ) + /** + * Replace a searchable in the database. + * The new entry will inherit the visibility, launch count, weight and pin position of the old entry, + * but it will have a different key and searchable. + */ + fun replace( + key: String, + newSearchable: SavableSearchable, + ) + /** * Touch a searchable to update its weight and launch counter **/ @@ -328,6 +339,19 @@ internal class SavableSearchableRepositoryImpl( } } + override fun replace(key: String, newSearchable: SavableSearchable) { + scope.launch { + database.searchableDao().replace( + key, + SavedSearchableUpdateContentEntity( + key = newSearchable.key, + type = newSearchable.domain, + serializedSearchable = newSearchable.serialize() ?: return@launch + ) + ) + } + } + override fun updateFavorites( manuallySorted: List, automaticallySorted: List diff --git a/services/tags/src/main/java/de/mm20/launcher2/services/tags/impl/TagsServiceImpl.kt b/services/tags/src/main/java/de/mm20/launcher2/services/tags/impl/TagsServiceImpl.kt index 46b732f2e..354e84cf0 100644 --- a/services/tags/src/main/java/de/mm20/launcher2/services/tags/impl/TagsServiceImpl.kt +++ b/services/tags/src/main/java/de/mm20/launcher2/services/tags/impl/TagsServiceImpl.kt @@ -51,8 +51,7 @@ internal class TagsServiceImpl( ).first() val oldTag = Tag(tag) if (pinnedTags.any { it.key == oldTag.key }) { - searchableRepository.update(oldTag, pinned = false) - searchableRepository.update(Tag(newName), pinned = true) + searchableRepository.replace(oldTag.key, Tag(newName)) } }