Run file search providers in parallel
This commit is contained in:
parent
8593758f41
commit
87f91aee74
@ -8,14 +8,25 @@ import de.mm20.launcher2.files.providers.OwncloudFileProvider
|
|||||||
import de.mm20.launcher2.files.providers.PluginFileProvider
|
import de.mm20.launcher2.files.providers.PluginFileProvider
|
||||||
import de.mm20.launcher2.nextcloud.NextcloudApiHelper
|
import de.mm20.launcher2.nextcloud.NextcloudApiHelper
|
||||||
import de.mm20.launcher2.owncloud.OwncloudClient
|
import de.mm20.launcher2.owncloud.OwncloudClient
|
||||||
|
import de.mm20.launcher2.permissions.PermissionGroup
|
||||||
import de.mm20.launcher2.permissions.PermissionsManager
|
import de.mm20.launcher2.permissions.PermissionsManager
|
||||||
import de.mm20.launcher2.preferences.search.FileSearchSettings
|
import de.mm20.launcher2.preferences.search.FileSearchSettings
|
||||||
import de.mm20.launcher2.search.File
|
import de.mm20.launcher2.search.File
|
||||||
|
import de.mm20.launcher2.search.Location
|
||||||
import de.mm20.launcher2.search.SearchableRepository
|
import de.mm20.launcher2.search.SearchableRepository
|
||||||
|
import kotlinx.collections.immutable.ImmutableList
|
||||||
import kotlinx.collections.immutable.persistentListOf
|
import kotlinx.collections.immutable.persistentListOf
|
||||||
import kotlinx.collections.immutable.toImmutableList
|
import kotlinx.collections.immutable.toImmutableList
|
||||||
import kotlinx.coroutines.flow.channelFlow
|
import kotlinx.collections.immutable.toPersistentList
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.flow.combineTransform
|
||||||
|
import kotlinx.coroutines.flow.emitAll
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.supervisorScope
|
||||||
|
|
||||||
internal class FileRepository(
|
internal class FileRepository(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@ -33,32 +44,50 @@ internal class FileRepository(
|
|||||||
override fun search(
|
override fun search(
|
||||||
query: String,
|
query: String,
|
||||||
allowNetwork: Boolean,
|
allowNetwork: Boolean,
|
||||||
) = channelFlow {
|
): Flow<ImmutableList<File>> {
|
||||||
if (query.isBlank()) {
|
if (query.isBlank()) {
|
||||||
send(persistentListOf())
|
return flowOf(persistentListOf())
|
||||||
return@channelFlow
|
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.enabledProviders.collectLatest { providerIds ->
|
val hasPermission = permissionsManager.hasPermission(PermissionGroup.ExternalStorage)
|
||||||
val providers = providerIds.map {
|
|
||||||
when (it) {
|
|
||||||
"local" -> LocalFileProvider(context, permissionsManager)
|
|
||||||
"gdrive" -> GDriveFileProvider(context)
|
|
||||||
"nextcloud" -> NextcloudFileProvider(nextcloudClient)
|
|
||||||
"owncloud" -> OwncloudFileProvider(owncloudClient)
|
|
||||||
else -> PluginFileProvider(context, it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (providers.isEmpty()) {
|
|
||||||
send(persistentListOf())
|
return combineTransform(settings.enabledProviders, hasPermission) { providerIds, permission ->
|
||||||
return@collectLatest
|
emit(persistentListOf())
|
||||||
}
|
if (providerIds.isEmpty()) {
|
||||||
val results = mutableListOf<File>()
|
return@combineTransform
|
||||||
for (provider in providers) {
|
}
|
||||||
results.addAll(provider.search(query, allowNetwork))
|
val providers = providerIds.mapNotNull {
|
||||||
send(results.toImmutableList())
|
when (it) {
|
||||||
|
"local" -> if (permission) LocalFileProvider(
|
||||||
|
context,
|
||||||
|
permissionsManager
|
||||||
|
) else null
|
||||||
|
|
||||||
|
"gdrive" -> GDriveFileProvider(context)
|
||||||
|
"nextcloud" -> NextcloudFileProvider(nextcloudClient)
|
||||||
|
"owncloud" -> OwncloudFileProvider(owncloudClient)
|
||||||
|
else -> PluginFileProvider(context, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
supervisorScope {
|
||||||
|
val result = MutableStateFlow(persistentListOf<File>())
|
||||||
|
|
||||||
|
for (provider in providers) {
|
||||||
|
launch {
|
||||||
|
val r = provider.search(
|
||||||
|
query,
|
||||||
|
allowNetwork,
|
||||||
|
)
|
||||||
|
result.update {
|
||||||
|
(it + r).toPersistentList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emitAll(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user