Skip to content

Commit 0c8291f

Browse files
Merge pull request nextcloud#15947 from nextcloud/fix/search-task-screen-state-handling
fix: search task screen state handling
2 parents 04689b9 + e99a434 commit 0c8291f

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListSearchTask.kt

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ package com.owncloud.android.ui.fragment
1111
import android.annotation.SuppressLint
1212
import androidx.lifecycle.lifecycleScope
1313
import com.nextcloud.client.account.User
14+
import com.owncloud.android.R
1415
import com.owncloud.android.datamodel.FileDataStorageManager
1516
import com.owncloud.android.lib.common.operations.RemoteOperation
1617
import com.owncloud.android.lib.common.operations.RemoteOperationResult
18+
import com.owncloud.android.lib.common.utils.Log_OC
1719
import com.owncloud.android.ui.events.SearchEvent
20+
import com.owncloud.android.utils.DisplayUtils
1821
import kotlinx.coroutines.Dispatchers
1922
import kotlinx.coroutines.Job
20-
import kotlinx.coroutines.isActive
2123
import kotlinx.coroutines.launch
2224
import kotlinx.coroutines.withContext
2325
import 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

Comments
 (0)