From 56578b238daff001459aef847afe4002f4911811 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sat, 15 Jun 2024 23:04:40 +0200 Subject: [PATCH] Fix tagged item in pinned tags not being refreshed --- .../ui/launcher/LauncherScaffoldVM.kt | 2 +- .../gestures/GestureSettingsScreenVM.kt | 50 ++++++++++++------- .../customattrs/CustomAttributesRepository.kt | 9 ++-- .../mm20/launcher2/database/SearchableDao.kt | 2 +- .../searchable/SavableSearchableRepository.kt | 16 ++++-- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherScaffoldVM.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherScaffoldVM.kt index b66da209..0053db3a 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherScaffoldVM.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherScaffoldVM.kt @@ -168,7 +168,7 @@ class LauncherScaffoldVM : ViewModel(), KoinComponent { longPressAppKey, doubleTapAppKey, homeButtonAppKey, - ).let { searchableRepository.getByKeys(it) } + ).let { searchableRepository.getByKeys(it).first() } GestureState( swipeLeftAction = swipeLeftAction, diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/gestures/GestureSettingsScreenVM.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/gestures/GestureSettingsScreenVM.kt index 1c1f775f..a53c4c0a 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/gestures/GestureSettingsScreenVM.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/gestures/GestureSettingsScreenVM.kt @@ -16,6 +16,8 @@ import de.mm20.launcher2.searchable.SavableSearchableRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.emptyFlow +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch @@ -78,9 +80,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val swipeLeftApp: Flow = swipeLeft - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) @@ -90,9 +94,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val swipeRightApp: Flow = swipeRight - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) @@ -102,9 +108,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val swipeDownApp: Flow = swipeDown - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) @@ -114,9 +122,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val longPressApp: Flow = longPress - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) @@ -126,9 +136,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val doubleTapApp: Flow = doubleTap - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) @@ -138,9 +150,11 @@ class GestureSettingsScreenVM : ViewModel(), KoinComponent { } val homeButtonApp: Flow = homeButton - .map { - if (it !is GestureAction.Launch || it.key == null) null - else searchableRepository.getByKeys(listOf(it.key!!)).firstOrNull() + .flatMapLatest { + if (it !is GestureAction.Launch || it.key == null) flowOf(null) + else searchableRepository.getByKeys(listOf(it.key!!)).map { + it.firstOrNull() + } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null) diff --git a/data/customattrs/src/main/java/de/mm20/launcher2/data/customattrs/CustomAttributesRepository.kt b/data/customattrs/src/main/java/de/mm20/launcher2/data/customattrs/CustomAttributesRepository.kt index 736c2b92..155d5f48 100644 --- a/data/customattrs/src/main/java/de/mm20/launcher2/data/customattrs/CustomAttributesRepository.kt +++ b/data/customattrs/src/main/java/de/mm20/launcher2/data/customattrs/CustomAttributesRepository.kt @@ -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> { 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() + } } } 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 b1ad60d8..3840f9ec 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 @@ -141,7 +141,7 @@ interface SearchableDao { ): Flow> @Query("SELECT * FROM Searchable WHERE `key` IN (:keys)") - suspend fun getByKeys(keys: List): List + fun getByKeys(keys: List): Flow> @Query("SELECT * FROM Searchable WHERE `key` = :key") fun getByKey(key: String): Flow 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 66e285ad..a2b426ae 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 @@ -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): List + fun getByKeys(keys: List): Flow> /** * Remove database entries that are invalid. This includes @@ -376,16 +378,20 @@ internal class SavableSearchableRepositoryImpl( } } - override suspend fun getByKeys(keys: List): List { + override fun getByKeys(keys: List): Flow> { 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) {