Fix pinned tags changing positions when renamed

This commit is contained in:
MM20 2024-10-20 22:16:27 +02:00
parent 52bba7f5f2
commit bbaba3aad9
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 34 additions and 2 deletions

View File

@ -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<Boolean>
@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)
}

View File

@ -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<SavableSearchable>,
automaticallySorted: List<SavableSearchable>

View File

@ -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))
}
}