Fix crash if user has an absurd amount of apps installed
This commit is contained in:
parent
b77e2331c9
commit
6f313face7
@ -1,5 +1,6 @@
|
|||||||
package de.mm20.launcher2.data.customattrs
|
package de.mm20.launcher2.data.customattrs
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import de.mm20.launcher2.crashreporter.CrashReporter
|
import de.mm20.launcher2.crashreporter.CrashReporter
|
||||||
import de.mm20.launcher2.database.AppDatabase
|
import de.mm20.launcher2.database.AppDatabase
|
||||||
import de.mm20.launcher2.database.entities.CustomAttributeEntity
|
import de.mm20.launcher2.database.entities.CustomAttributeEntity
|
||||||
@ -11,6 +12,7 @@ import kotlinx.collections.immutable.persistentListOf
|
|||||||
import kotlinx.collections.immutable.toImmutableList
|
import kotlinx.collections.immutable.toImmutableList
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
@ -69,11 +71,22 @@ internal class CustomAttributesRepositoryImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getCustomLabels(items: List<SavableSearchable>): Flow<List<CustomLabel>> {
|
override fun getCustomLabels(items: List<SavableSearchable>): Flow<List<CustomLabel>> {
|
||||||
|
if (items.size <= 999) {
|
||||||
val dao = appDatabase.customAttrsDao()
|
val dao = appDatabase.customAttrsDao()
|
||||||
return dao.getCustomAttributes(items.map { it.key }, CustomAttributeType.Label.value)
|
return dao.getCustomAttributes(items.map { it.key }, CustomAttributeType.Label.value)
|
||||||
.map { list ->
|
.map { list ->
|
||||||
list.mapNotNull { CustomAttribute.fromDatabaseEntity(it) as? CustomLabel }
|
list.mapNotNull { CustomAttribute.fromDatabaseEntity(it) as? CustomLabel }
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
val dao = appDatabase.customAttrsDao()
|
||||||
|
return combine(items.chunked(999).map { chunk ->
|
||||||
|
dao.getCustomAttributes(chunk.map { it.key }, CustomAttributeType.Label.value)
|
||||||
|
}) { results ->
|
||||||
|
results.flatMap { list ->
|
||||||
|
list.mapNotNull { CustomAttribute.fromDatabaseEntity(it) as? CustomLabel }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setCustomLabel(searchable: SavableSearchable, label: String) {
|
override fun setCustomLabel(searchable: SavableSearchable, label: String) {
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.firstOrNull
|
import kotlinx.coroutines.flow.firstOrNull
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -338,10 +339,12 @@ internal class SearchableRepositoryImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun sortByRelevance(keys: List<String>): Flow<List<String>> {
|
override fun sortByRelevance(keys: List<String>): Flow<List<String>> {
|
||||||
|
if (keys.size > 999) return flowOf(emptyList())
|
||||||
return database.searchableDao().sortByRelevance(keys)
|
return database.searchableDao().sortByRelevance(keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sortByWeight(keys: List<String>): Flow<List<String>> {
|
override fun sortByWeight(keys: List<String>): Flow<List<String>> {
|
||||||
|
if (keys.size > 999) return flowOf(emptyList())
|
||||||
return database.searchableDao().sortByWeight(keys)
|
return database.searchableDao().sortByWeight(keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,6 +371,12 @@ internal class SearchableRepositoryImpl(
|
|||||||
|
|
||||||
override suspend fun getByKeys(keys: List<String>): List<SavableSearchable> {
|
override suspend fun getByKeys(keys: List<String>): List<SavableSearchable> {
|
||||||
val dao = database.searchableDao()
|
val dao = database.searchableDao()
|
||||||
|
if (keys.size > 999) {
|
||||||
|
return keys.chunked(999).flatMap {
|
||||||
|
dao.getByKeys(it)
|
||||||
|
.mapNotNull { fromDatabaseEntity(it).searchable }
|
||||||
|
}
|
||||||
|
}
|
||||||
return dao.getByKeys(keys)
|
return dao.getByKeys(keys)
|
||||||
.mapNotNull { fromDatabaseEntity(it).searchable }
|
.mapNotNull { fromDatabaseEntity(it).searchable }
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user