From 1fed84f948bbfc13ca459545c06c106eb0fc24d6 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 7 Aug 2022 22:18:53 +0200 Subject: [PATCH] fix caching of instance defaults and emojis (#2643) * fix caching of instance defaults and emojis * use correct OnConflictStrategy * rename dao methods --- .../instanceinfo/InstanceInfoRepository.kt | 4 +-- .../com/keylesspalace/tusky/db/InstanceDao.kt | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/instanceinfo/InstanceInfoRepository.kt b/app/src/main/java/com/keylesspalace/tusky/components/instanceinfo/InstanceInfoRepository.kt index 170cff2b..bfc5a9b8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/instanceinfo/InstanceInfoRepository.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/instanceinfo/InstanceInfoRepository.kt @@ -45,7 +45,7 @@ class InstanceInfoRepository @Inject constructor( */ suspend fun getEmojis(): List = withContext(Dispatchers.IO) { api.getCustomEmojis() - .onSuccess { emojiList -> dao.insertOrReplace(EmojisEntity(instanceName, emojiList)) } + .onSuccess { emojiList -> dao.upsert(EmojisEntity(instanceName, emojiList)) } .getOrElse { throwable -> Log.w(TAG, "failed to load custom emojis, falling back to cache", throwable) dao.getEmojiInfo(instanceName)?.emojiList.orEmpty() @@ -78,7 +78,7 @@ class InstanceInfoRepository @Inject constructor( maxFieldNameLength = instance.pleroma?.metadata?.fieldLimits?.nameLength, maxFieldValueLength = instance.pleroma?.metadata?.fieldLimits?.valueLength ) - dao.insertOrReplace(instanceEntity) + dao.upsert(instanceEntity) instanceEntity }, { throwable -> diff --git a/app/src/main/java/com/keylesspalace/tusky/db/InstanceDao.kt b/app/src/main/java/com/keylesspalace/tusky/db/InstanceDao.kt index 3687da09..0bf1dc32 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/InstanceDao.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/InstanceDao.kt @@ -20,15 +20,37 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import androidx.room.RewriteQueriesToDropUnusedColumns +import androidx.room.Transaction +import androidx.room.Update @Dao interface InstanceDao { - @Insert(onConflict = OnConflictStrategy.REPLACE, entity = InstanceEntity::class) - suspend fun insertOrReplace(instance: InstanceInfoEntity) + @Insert(onConflict = OnConflictStrategy.IGNORE, entity = InstanceEntity::class) + suspend fun insertOrIgnore(instance: InstanceInfoEntity): Long - @Insert(onConflict = OnConflictStrategy.REPLACE, entity = InstanceEntity::class) - suspend fun insertOrReplace(emojis: EmojisEntity) + @Update(onConflict = OnConflictStrategy.IGNORE, entity = InstanceEntity::class) + suspend fun updateOrIgnore(instance: InstanceInfoEntity) + + @Transaction + suspend fun upsert(instance: InstanceInfoEntity) { + if (insertOrIgnore(instance) == -1L) { + updateOrIgnore(instance) + } + } + + @Insert(onConflict = OnConflictStrategy.IGNORE, entity = InstanceEntity::class) + suspend fun insertOrIgnore(emojis: EmojisEntity): Long + + @Update(onConflict = OnConflictStrategy.IGNORE, entity = InstanceEntity::class) + suspend fun updateOrIgnore(emojis: EmojisEntity) + + @Transaction + suspend fun upsert(emojis: EmojisEntity) { + if (insertOrIgnore(emojis) == -1L) { + updateOrIgnore(emojis) + } + } @RewriteQueriesToDropUnusedColumns @Query("SELECT * FROM InstanceEntity WHERE instance = :instance LIMIT 1")