Fix tagged item in pinned tags not being refreshed

This commit is contained in:
MM20
2024-06-15 23:04:40 +02:00
parent 13ae92eafe
commit 56578b238d
5 changed files with 51 additions and 28 deletions
@@ -13,6 +13,7 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import org.json.JSONArray
@@ -137,7 +138,7 @@ internal class CustomAttributesRepositoryImpl(
override fun getItemsForTag(tag: String): Flow<List<SavableSearchable>> {
val dao = appDatabase.customAttrsDao()
return dao.getItemsWithTag(tag).map {
return dao.getItemsWithTag(tag).flatMapLatest {
searchableRepository.getByKeys(it)
}
}
@@ -181,8 +182,10 @@ internal class CustomAttributesRepositoryImpl(
}
}
val dao = appDatabase.customAttrsDao()
return dao.search("%$query%").map {
searchableRepository.getByKeys(it).toImmutableList()
return dao.search("%$query%").flatMapLatest {
searchableRepository.getByKeys(it).map {
it.toImmutableList()
}
}
}
@@ -141,7 +141,7 @@ interface SearchableDao {
): Flow<List<String>>
@Query("SELECT * FROM Searchable WHERE `key` IN (:keys)")
suspend fun getByKeys(keys: List<String>): List<SavedSearchableEntity>
fun getByKeys(keys: List<String>): Flow<List<SavedSearchableEntity>>
@Query("SELECT * FROM Searchable WHERE `key` = :key")
fun getByKey(key: String): Flow<SavedSearchableEntity?>
@@ -16,9 +16,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONArray
@@ -105,7 +107,7 @@ interface SavableSearchableRepository: Backupable {
* Get items with the given keys from the favorites database.
* Items that don't exist in the database will not be returned.
*/
suspend fun getByKeys(keys: List<String>): List<SavableSearchable>
fun getByKeys(keys: List<String>): Flow<List<SavableSearchable>>
/**
* Remove database entries that are invalid. This includes
@@ -376,16 +378,20 @@ internal class SavableSearchableRepositoryImpl(
}
}
override suspend fun getByKeys(keys: List<String>): List<SavableSearchable> {
override fun getByKeys(keys: List<String>): Flow<List<SavableSearchable>> {
val dao = database.searchableDao()
if (keys.size > 999) {
return keys.chunked(999).flatMap {
return combine(keys.chunked(999).map {
dao.getByKeys(it)
.mapNotNull { fromDatabaseEntity(it).searchable }
.map {
it.mapNotNull { fromDatabaseEntity(it).searchable }
}
}) { results ->
results.flatMap { it }
}
}
return dao.getByKeys(keys)
.mapNotNull { fromDatabaseEntity(it).searchable }
.map { it.mapNotNull { fromDatabaseEntity(it).searchable } }
}
override suspend fun backup(toDir: File) = withContext(Dispatchers.IO) {