فهرست منبع

Merge pull request #4475 from nextcloud/photoSearchTask

Enhance photo search task
Andy Scherzinger 5 سال پیش
والد
کامیت
863261b3a1

+ 5 - 1
src/main/java/com/owncloud/android/ui/asynctasks/PhotoSearchTask.java

@@ -87,7 +87,11 @@ public class PhotoSearchTask extends AsyncTask<Void, Void, RemoteOperationResult
             searchRemoteOperation.setLimit(limit);
             searchRemoteOperation.setTimestamp(timestamp);
 
-            return searchRemoteOperation.execute(account, photoFragment.requireContext());
+            if (photoFragment.getContext() != null) {
+                return searchRemoteOperation.execute(account, photoFragment.getContext());
+            } else {
+                return new RemoteOperationResult(new IllegalStateException("No context available"));
+            }
         }
     }
 

+ 16 - 5
src/main/java/com/owncloud/android/ui/fragment/PhotoFragment.java

@@ -21,6 +21,7 @@
 
 package com.owncloud.android.ui.fragment;
 
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -50,12 +51,22 @@ public class PhotoFragment extends OCFileListFragment {
     private boolean photoSearchQueryRunning = false;
     private boolean photoSearchNoNew = false;
     private SearchRemoteOperation searchRemoteOperation;
+    private AsyncTask photoSearchTask;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
 
+    @Override
+    public void onPause() {
+        super.onPause();
+
+        if (photoSearchTask != null) {
+            photoSearchTask.cancel(true);
+        }
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -138,11 +149,11 @@ public class PhotoFragment extends OCFileListFragment {
 
     private void searchAndDisplay() {
         if (!photoSearchQueryRunning && !photoSearchNoNew) {
-            new PhotoSearchTask(getColumnsCount(),
-                                this,
-                                accountManager.getCurrentAccount(),
-                                searchRemoteOperation,
-                                mContainerActivity.getStorageManager())
+            photoSearchTask = new PhotoSearchTask(getColumnsCount(),
+                                                  this,
+                                                  accountManager.getCurrentAccount(),
+                                                  searchRemoteOperation,
+                                                  mContainerActivity.getStorageManager())
                 .execute();
         }
     }