Add API to control online search

This commit is contained in:
MM20
2024-01-28 21:35:36 +01:00
parent d7340c485b
commit 426473e397
20 changed files with 44 additions and 36 deletions
@@ -32,6 +32,7 @@ internal class FileRepository(
override fun search(
query: String,
allowNetwork: Boolean,
) = channelFlow {
if (query.isBlank()) {
send(persistentListOf())
@@ -55,7 +56,7 @@ internal class FileRepository(
}
val results = mutableListOf<File>()
for (provider in providers) {
results.addAll(provider.search(query))
results.addAll(provider.search(query, allowNetwork))
send(results.toImmutableList())
}
}
@@ -3,5 +3,5 @@ package de.mm20.launcher2.files.providers
import de.mm20.launcher2.search.File
internal interface FileProvider {
suspend fun search(query: String): List<File>
suspend fun search(query: String, allowNetwork: Boolean): List<File>
}
@@ -12,8 +12,8 @@ import kotlinx.collections.immutable.toImmutableMap
internal class GDriveFileProvider(
private val context: Context
) : FileProvider {
override suspend fun search(query: String): List<File> {
if (query.length < 4) return emptyList()
override suspend fun search(query: String, allowNetwork: Boolean): List<File> {
if (query.length < 4 || !allowNetwork) return emptyList()
val driveFiles = GoogleApiHelper.getInstance(context).queryGDriveFiles(query)
return driveFiles.map {
GDriveFile(
@@ -13,7 +13,7 @@ internal class LocalFileProvider(
private val context: Context,
private val permissionsManager: PermissionsManager
): FileProvider {
override suspend fun search(query: String): List<File> = withContext(Dispatchers.IO) {
override suspend fun search(query: String, allowNetwork: Boolean): List<File> = withContext(Dispatchers.IO) {
if (!permissionsManager.checkPermissionOnce(PermissionGroup.ExternalStorage)) {
return@withContext emptyList()
}
@@ -12,8 +12,8 @@ import kotlin.math.min
internal class NextcloudFileProvider(
private val nextcloudClient: NextcloudApiHelper
) : FileProvider {
override suspend fun search(query: String): List<File> {
if (query.length < 4) return emptyList()
override suspend fun search(query: String, allowNetwork: Boolean): List<File> {
if (query.length < 4 || !allowNetwork) return emptyList()
val server = nextcloudClient.getServer() ?: return emptyList()
return withContext(Dispatchers.IO) {
nextcloudClient.files.search(query).let { it.subList(0, min(10, it.size)) }.map {
@@ -9,8 +9,8 @@ import kotlinx.collections.immutable.persistentMapOf
internal class OwncloudFileProvider(
private val owncloudClient: OwncloudClient
) : FileProvider {
override suspend fun search(query: String): List<File> {
if (query.length < 4) return emptyList()
override suspend fun search(query: String, allowNetwork: Boolean): List<File> {
if (query.length < 4 || !allowNetwork) return emptyList()
val server = owncloudClient.getServer() ?: return emptyList()
return owncloudClient.files.query(query).map {
OwncloudFile(
@@ -26,12 +26,13 @@ class PluginFileProvider(
private val context: Context,
private val pluginAuthority: String,
) : FileProvider {
override suspend fun search(query: String): List<File> = withContext(Dispatchers.IO) {
override suspend fun search(query: String, allowNetwork: Boolean): List<File> = withContext(Dispatchers.IO) {
val uri = Uri.Builder()
.scheme("content")
.authority(pluginAuthority)
.path(SearchPluginContract.Paths.Search)
.appendQueryParameter(SearchPluginContract.Paths.QueryParam, query)
.appendQueryParameter(SearchPluginContract.Paths.AllowNetworkParam, allowNetwork.toString())
.build()
val cancellationSignal = CancellationSignal()