Forráskód Böngészése

OCFileListFragment: extract seach async task and convert it to kotlin

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
Álvaro Brey Vilas 3 éve
szülő
commit
769816978a

+ 3 - 85
app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -113,7 +113,6 @@ import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
 
 import java.io.File;
-import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -1384,7 +1383,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
         return mAdapter;
     }
 
-    private void setTitle() {
+    protected void setTitle() {
         // set title
 
         if (getActivity() instanceof FileDisplayActivity && currentSearchType != null) {
@@ -1428,7 +1427,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
         }
     }
 
-    private void setEmptyView(SearchEvent event) {
+    protected void setEmptyView(SearchEvent event) {
         if (event != null) {
             switch (event.getSearchType()) {
                 case FILE_SEARCH:
@@ -1510,87 +1509,6 @@ public class OCFileListFragment extends ExtendedListFragment implements
         handleSearchEvent(event);
     }
 
-    private static class SearchAsyncTask extends AsyncTask<Void, Void, Boolean> {
-
-        private final WeakReference<FileFragment.ContainerActivity> activityReference;
-        private final WeakReference<OCFileListFragment> fragmentReference;
-        private final RemoteOperation remoteOperation;
-        private final User currentUser;
-        private final SearchEvent event;
-
-        private SearchAsyncTask(final FileFragment.ContainerActivity containerActivity, final OCFileListFragment fragment, final RemoteOperation remoteOperation, final User currentUser, final SearchEvent event) {
-            this.activityReference = new WeakReference<>(containerActivity);
-            this.fragmentReference = new WeakReference<>(fragment);
-            this.remoteOperation = remoteOperation;
-            this.currentUser = currentUser;
-            this.event = event;
-        }
-
-        @Override
-        protected void onPreExecute() {
-            final OCFileListFragment fragment = fragmentReference.get();
-            if (fragment != null) {
-                new Handler(Looper.getMainLooper()).post(() -> {
-                    fragment.setLoading(true);
-                    fragment.setEmptyListLoadingMessage();
-                });
-            }
-        }
-
-        @Override
-        protected Boolean doInBackground(Void... voids) {
-            final OCFileListFragment fragment = fragmentReference.get();
-            if (fragment == null || isCancelled() || fragment.getContext() == null) {
-                return Boolean.FALSE;
-            }
-
-            fragment.setTitle();
-            RemoteOperationResult remoteOperationResult = remoteOperation.execute(
-                currentUser.toPlatformAccount(), fragment.getContext());
-
-            FileDataStorageManager storageManager = getFileDataStorageManager();
-
-            if (remoteOperationResult.isSuccess() && remoteOperationResult.getResultData() != null
-                && !isCancelled() && fragment.searchFragment) {
-                fragment.searchEvent = event;
-
-                if (remoteOperationResult.getResultData() == null || ((List) remoteOperationResult.getResultData()).isEmpty()) {
-                    fragment.setEmptyView(event);
-                } else {
-                    fragment.mAdapter.setData(((RemoteOperationResult<List>) remoteOperationResult).getResultData(),
-                                              fragment.currentSearchType,
-                                              storageManager,
-                                              fragment.mFile,
-                                              true);
-                }
-            }
-
-            return remoteOperationResult.isSuccess();
-        }
-
-        @Nullable
-        private FileDataStorageManager getFileDataStorageManager() {
-            FileDataStorageManager storageManager = null;
-            final FileFragment.ContainerActivity containerActivity = activityReference.get();
-            if (containerActivity != null && containerActivity.getStorageManager() != null) {
-                storageManager = containerActivity.getStorageManager();
-            }
-            return storageManager;
-        }
-
-        @Override
-        protected void onPostExecute(Boolean bool) {
-            final OCFileListFragment fragment = fragmentReference.get();
-            if (fragment != null) {
-                fragment.setLoading(false);
-                if (!isCancelled()) {
-                    fragment.mAdapter.notifyDataSetChanged();
-                }
-            }
-
-        }
-    }
-
     private void handleSearchEvent(SearchEvent event) {
         if (SearchRemoteOperation.SearchType.PHOTO_SEARCH == event.getSearchType()) {
             return;
@@ -1638,7 +1556,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
         }
 
 
-        remoteOperationAsyncTask = new SearchAsyncTask(mContainerActivity, this, remoteOperation, currentUser, event);
+        remoteOperationAsyncTask = new OCFileListSearchAsyncTask(mContainerActivity, this, remoteOperation, currentUser, event);
 
         remoteOperationAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     }

+ 92 - 0
app/src/main/java/com/owncloud/android/ui/fragment/OCFileListSearchAsyncTask.kt

@@ -0,0 +1,92 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Álvaro Brey Vilas
+ * Copyright (C) 2022 Álvaro Brey Vilas
+ * Copyright (C) 2022 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.ui.fragment
+
+import android.os.AsyncTask
+import android.os.Handler
+import android.os.Looper
+import com.nextcloud.client.account.User
+import com.owncloud.android.datamodel.FileDataStorageManager
+import com.owncloud.android.lib.common.operations.RemoteOperation
+import com.owncloud.android.lib.common.operations.RemoteOperationResult
+import com.owncloud.android.ui.events.SearchEvent
+import java.lang.ref.WeakReference
+
+class OCFileListSearchAsyncTask(
+    containerActivity: FileFragment.ContainerActivity,
+    fragment: OCFileListFragment,
+    private val remoteOperation: RemoteOperation<List<Any>>,
+    private val currentUser: User,
+    private val event: SearchEvent
+) : AsyncTask<Void, Void, Boolean>() {
+    private val activityReference: WeakReference<FileFragment.ContainerActivity> = WeakReference(containerActivity)
+    private val fragmentReference: WeakReference<OCFileListFragment> = WeakReference(fragment)
+
+    private val fileDataStorageManager: FileDataStorageManager?
+        get() = activityReference.get()?.storageManager
+
+    private fun RemoteOperationResult<out Any>.hasSuccessfulResult() = this.isSuccess && this.resultData != null
+
+    override fun onPreExecute() {
+        fragmentReference.get()?.let { fragment ->
+            Handler(Looper.getMainLooper()).post {
+                fragment.isLoading = true
+                fragment.setEmptyListLoadingMessage()
+            }
+        }
+    }
+
+    override fun doInBackground(vararg voids: Void): Boolean {
+        val fragment = fragmentReference.get()
+        if (fragment?.context == null || isCancelled) {
+            return java.lang.Boolean.FALSE
+        }
+
+        fragment.setTitle()
+        val remoteOperationResult = remoteOperation.execute(
+            currentUser.toPlatformAccount(), fragment.context
+        )
+        if (remoteOperationResult.hasSuccessfulResult() && !isCancelled && fragment.searchFragment) {
+            fragment.searchEvent = event
+            if (remoteOperationResult.resultData.isNullOrEmpty()) {
+                fragment.setEmptyView(event)
+            } else {
+                fragment.mAdapter.setData(
+                    remoteOperationResult.resultData,
+                    fragment.currentSearchType,
+                    fileDataStorageManager,
+                    fragment.mFile,
+                    true
+                )
+            }
+        }
+        return remoteOperationResult.isSuccess
+    }
+
+    override fun onPostExecute(bool: Boolean) {
+        fragmentReference.get()?.let { fragment ->
+            fragment.isLoading = false
+            if (!isCancelled) {
+                fragment.mAdapter.notifyDataSetChanged()
+            }
+        }
+    }
+}