|
@@ -24,8 +24,12 @@ package com.owncloud.android.ui.unifiedsearch
|
|
|
import com.nextcloud.client.account.CurrentAccountProvider
|
|
|
import com.nextcloud.client.core.AsyncRunner
|
|
|
import com.nextcloud.client.network.ClientFactory
|
|
|
+import com.nextcloud.common.NextcloudClient
|
|
|
import com.owncloud.android.lib.common.SearchProviders
|
|
|
import com.owncloud.android.lib.common.utils.Log_OC
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
|
class UnifiedSearchRemoteRepository(
|
|
|
private val clientFactory: ClientFactory,
|
|
@@ -35,6 +39,12 @@ class UnifiedSearchRemoteRepository(
|
|
|
|
|
|
private var providers: SearchProviders? = null
|
|
|
|
|
|
+ private fun runAsyncWithNcClient(callback: (client: NextcloudClient) -> Unit) =
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
|
|
|
+ callback(client)
|
|
|
+ }
|
|
|
+
|
|
|
override fun queryAll(
|
|
|
query: String,
|
|
|
onResult: (UnifiedSearchResult) -> Unit,
|
|
@@ -47,30 +57,31 @@ class UnifiedSearchRemoteRepository(
|
|
|
val providerIds = result.providers.map { it.id }
|
|
|
var openRequests = providerIds.size
|
|
|
var anyError = false
|
|
|
- val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
|
|
|
- providerIds
|
|
|
- .forEach { provider ->
|
|
|
- val task = SearchOnProviderTask(query, provider, client)
|
|
|
- asyncRunner.postQuickTask(
|
|
|
- task = task,
|
|
|
- onResult = {
|
|
|
- openRequests--
|
|
|
- anyError = anyError || !it.success
|
|
|
- onResult(UnifiedSearchResult(provider, it.success, it.searchResult))
|
|
|
- if (openRequests == 0) {
|
|
|
- onFinished(!anyError)
|
|
|
- }
|
|
|
- },
|
|
|
- onError = {
|
|
|
- openRequests--
|
|
|
- anyError = true
|
|
|
- onError(it)
|
|
|
- if (openRequests == 0) {
|
|
|
- onFinished(!anyError)
|
|
|
+ runAsyncWithNcClient { client ->
|
|
|
+ providerIds
|
|
|
+ .forEach { provider ->
|
|
|
+ val task = SearchOnProviderTask(query, provider, client)
|
|
|
+ asyncRunner.postQuickTask(
|
|
|
+ task = task,
|
|
|
+ onResult = {
|
|
|
+ openRequests--
|
|
|
+ anyError = anyError || !it.success
|
|
|
+ onResult(UnifiedSearchResult(provider, it.success, it.searchResult))
|
|
|
+ if (openRequests == 0) {
|
|
|
+ onFinished(!anyError)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onError = {
|
|
|
+ openRequests--
|
|
|
+ anyError = true
|
|
|
+ onError(it)
|
|
|
+ if (openRequests == 0) {
|
|
|
+ onFinished(!anyError)
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- )
|
|
|
- }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
onError = onError
|
|
|
)
|
|
@@ -88,16 +99,17 @@ class UnifiedSearchRemoteRepository(
|
|
|
this,
|
|
|
"queryProvider() called with: query = $query, provider = $provider, cursor = $cursor"
|
|
|
)
|
|
|
- val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
|
|
|
- val task = SearchOnProviderTask(query, provider, client, cursor)
|
|
|
- asyncRunner.postQuickTask(
|
|
|
- task,
|
|
|
- onResult = {
|
|
|
- onResult(UnifiedSearchResult(provider, it.success, it.searchResult))
|
|
|
- onFinished(it.success)
|
|
|
- },
|
|
|
- onError
|
|
|
- )
|
|
|
+ runAsyncWithNcClient { client ->
|
|
|
+ val task = SearchOnProviderTask(query, provider, client, cursor)
|
|
|
+ asyncRunner.postQuickTask(
|
|
|
+ task,
|
|
|
+ onResult = {
|
|
|
+ onResult(UnifiedSearchResult(provider, it.success, it.searchResult))
|
|
|
+ onFinished(it.success)
|
|
|
+ },
|
|
|
+ onError
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
fun fetchProviders(onResult: (SearchProviders) -> Unit, onError: (Throwable) -> Unit) {
|
|
@@ -105,13 +117,14 @@ class UnifiedSearchRemoteRepository(
|
|
|
if (this.providers != null) {
|
|
|
onResult(this.providers!!)
|
|
|
} else {
|
|
|
- val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
|
|
|
- val task = GetSearchProvidersTask(client)
|
|
|
- asyncRunner.postQuickTask(
|
|
|
- task,
|
|
|
- onResult = { onResult(it.providers) },
|
|
|
- onError = onError
|
|
|
- )
|
|
|
+ runAsyncWithNcClient { client ->
|
|
|
+ val task = GetSearchProvidersTask(client)
|
|
|
+ asyncRunner.postQuickTask(
|
|
|
+ task,
|
|
|
+ onResult = { onResult(it.providers) },
|
|
|
+ onError = onError
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|