Skip to content

Commit

Permalink
Fix pinned tags changing positions when renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
MM2-0 committed Oct 20, 2024
1 parent 52bba7f commit bbaba3a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
**/
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit bbaba3a

Please sign in to comment.