@@ -11,13 +11,15 @@ package com.owncloud.android.ui.fragment
1111import android.annotation.SuppressLint
1212import androidx.lifecycle.lifecycleScope
1313import com.nextcloud.client.account.User
14+ import com.owncloud.android.R
1415import com.owncloud.android.datamodel.FileDataStorageManager
1516import com.owncloud.android.lib.common.operations.RemoteOperation
1617import com.owncloud.android.lib.common.operations.RemoteOperationResult
18+ import com.owncloud.android.lib.common.utils.Log_OC
1719import com.owncloud.android.ui.events.SearchEvent
20+ import com.owncloud.android.utils.DisplayUtils
1821import kotlinx.coroutines.Dispatchers
1922import kotlinx.coroutines.Job
20- import kotlinx.coroutines.isActive
2123import kotlinx.coroutines.launch
2224import kotlinx.coroutines.withContext
2325import kotlinx.coroutines.withTimeoutOrNull
@@ -33,57 +35,62 @@ class OCFileListSearchTask(
3335 private val event : SearchEvent ,
3436 private val taskTimeout : Long
3537) {
38+ companion object {
39+ private const val TAG = " OCFileListSearchTask"
40+ }
41+
3642 private val activityReference: WeakReference <FileFragment .ContainerActivity > = WeakReference (containerActivity)
3743 private val fragmentReference: WeakReference <OCFileListFragment > = WeakReference (fragment)
3844
3945 private val fileDataStorageManager: FileDataStorageManager ?
4046 get() = activityReference.get()?.storageManager
4147
42- private fun RemoteOperationResult <out Any >.hasSuccessfulResult () = this .isSuccess && this .resultData != null
43-
4448 private var job: Job ? = null
4549
46- @Suppress(" TooGenericExceptionCaught" , " DEPRECATION" )
50+ @Suppress(" TooGenericExceptionCaught" , " DEPRECATION" , " ReturnCount " )
4751 fun execute () {
48- fragmentReference.get()?.let { fragment ->
49- job = fragment.lifecycleScope.launch(Dispatchers .IO ) {
50- val result = withTimeoutOrNull(taskTimeout) {
51- if (! isActive) {
52- false
53- } else {
54- fragment.setTitle()
55- lateinit var remoteOperationResult: RemoteOperationResult <List <Any >>
56- try {
57- remoteOperationResult = remoteOperation.execute(currentUser, fragment.requireContext())
58- } catch (_: Exception ) {
59- remoteOperationResult =
60- remoteOperation.executeNextcloudClient(currentUser, fragment.requireContext())
61- }
52+ Log_OC .d(TAG , " search task running, query: ${event.searchType} " )
53+ val fragment = fragmentReference.get() ? : return
54+ val context = fragment.context ? : return
6255
63- if (remoteOperationResult.hasSuccessfulResult() && isActive && fragment.searchFragment) {
64- fragment.searchEvent = event
65- if (remoteOperationResult.resultData.isNullOrEmpty()) {
66- fragment.setEmptyView(event)
67- } else {
68- fragment.adapter.setData(
69- remoteOperationResult.resultData,
70- fragment.currentSearchType,
71- fileDataStorageManager,
72- fragment.mFile,
73- true
74- )
75- }
76- }
77- remoteOperationResult.isSuccess
78- }
79- } ? : false
56+ job = fragment.lifecycleScope.launch {
57+ val result: RemoteOperationResult <List <Any >>? = withContext(Dispatchers .IO ) {
58+ try {
59+ withTimeoutOrNull(taskTimeout) {
60+ remoteOperation.execute(currentUser, context)
61+ } ? : remoteOperation.executeNextcloudClient(currentUser, context)
62+ } catch (e: Exception ) {
63+ Log_OC .e(TAG , " exception execute: " , e)
64+ null
65+ }
66+ }
8067
81- withContext(Dispatchers .Main ) {
82- if (result && isActive) {
83- fragment.adapter.notifyDataSetChanged()
84- } else {
85- fragment.setEmptyListMessage(EmptyListState .ERROR )
68+ withContext(Dispatchers .Main ) {
69+ if (! fragment.isAdded || ! fragment.searchFragment) {
70+ Log_OC .e(TAG , " cannot fetch sharees fragment is not ready" )
71+ return @withContext
72+ }
73+
74+ if (result?.isSuccess == true ) {
75+ if (result.resultData.isEmpty()) {
76+ fragment.setEmptyListMessage(SearchType .NO_SEARCH )
77+ return @withContext
8678 }
79+
80+ fragment.searchEvent = event
81+ fragment.adapter.setData(
82+ result.resultData,
83+ fragment.currentSearchType,
84+ fileDataStorageManager,
85+ fragment.mFile,
86+ true
87+ )
88+
89+ return @withContext
90+ }
91+
92+ fragment.activity?.let {
93+ DisplayUtils .showSnackMessage(it, R .string.error_fetching_sharees)
8794 }
8895 }
8996 }
0 commit comments