Mario Danic 8 жил өмнө
parent
commit
705ea85060

+ 1 - 1
build.gradle

@@ -173,7 +173,7 @@ dependencies {
     compile name: 'touch-image-view'
     compile 'com.android.support:multidex:1.0.1'
 
-    compile 'com.github.nextcloud:android-library:search-SNAPSHOT'
+    compile 'com.github.nextcloud:android-library:toggle-favourite-SNAPSHOT'
     compile "com.android.support:support-v4:${supportLibraryVersion}"
     compile "com.android.support:design:${supportLibraryVersion}"
     compile 'com.jakewharton:disklrucache:2.0.2'

+ 35 - 1
src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -54,12 +54,15 @@ import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.resources.files.RemoteFile;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.activity.ComponentsGetter;
+import com.owncloud.android.ui.events.FavoriteEvent;
 import com.owncloud.android.ui.fragment.ExtendedListFragment;
 import com.owncloud.android.ui.interfaces.OCFileListFragmentInterface;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.MimeTypeUtil;
 
+import org.greenrobot.eventbus.EventBus;
+
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Vector;
@@ -134,6 +137,29 @@ public class FileListListAdapter extends BaseAdapter {
         return mFiles.get(position);
     }
 
+    public void setFavoriteAttributeForItemID(String fileId, boolean favorite) {
+        for (int i = 0; i < mFiles.size(); i++) {
+            if (mFiles.get(i).getRemoteId().equals(fileId)) {
+                mFiles.get(i).setFavorite(favorite);
+                break;
+            }
+        }
+
+        for (int i = 0; i < mFilesAll.size(); i++) {
+            if (mFilesAll.get(i).getRemoteId().equals(fileId)) {
+                mFilesAll.get(i).setFavorite(favorite);
+                break;
+            }
+        }
+
+        new Handler(Looper.getMainLooper()).post(new Runnable() {
+            @Override
+            public void run() {
+                notifyDataSetChanged();
+            }
+        });
+    }
+
     @Override
     public long getItemId(int position) {
         if (mFiles == null || mFiles.size() <= position) {
@@ -218,7 +244,6 @@ public class FileListListAdapter extends BaseAdapter {
                     lastModV.setText(DisplayUtils.getRelativeTimestamp(mContext, file.getModificationTimestamp()));
 
 
-
                     fileSizeSeparatorV.setVisibility(View.VISIBLE);
                     fileSizeV.setVisibility(View.VISIBLE);
                     fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
@@ -326,6 +351,15 @@ public class FileListListAdapter extends BaseAdapter {
                 view.findViewById(R.id.favorite_action).setSelected(false);
             }
 
+            final OCFile finalFile = file;
+            view.findViewById(R.id.favorite_action).setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    EventBus.getDefault().post(new FavoriteEvent(finalFile.getRemotePath(),
+                            !finalFile.getIsFavorite(), finalFile.getRemoteId()));
+                }
+            });
+
             // No Folder
             if (!file.isFolder()) {
                 if ((MimeTypeUtil.isImage(file) || MimeTypeUtil.isVideo(file)) && file.getRemoteId() != null) {

+ 36 - 0
src/main/java/com/owncloud/android/ui/events/FavoriteEvent.java

@@ -0,0 +1,36 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.ui.events;
+
+/**
+ * Event for making favoriting work
+ */
+
+public class FavoriteEvent {
+    public final String remotePath;
+    public final boolean shouldFavorite;
+    public final String remoteId;
+
+    public FavoriteEvent(String remotePath, boolean shouldFavorite, String remoteId) {
+        this.remotePath = remotePath;
+        this.shouldFavorite = shouldFavorite;
+        this.remoteId = remoteId;
+    }
+}

+ 39 - 0
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -63,6 +63,7 @@ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.SearchOperation;
+import com.owncloud.android.lib.resources.files.ToggleFavoriteOperation;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
@@ -75,6 +76,7 @@ import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
 import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
 import com.owncloud.android.ui.events.DummyDrawerEvent;
+import com.owncloud.android.ui.events.FavoriteEvent;
 import com.owncloud.android.ui.events.MenuItemClickEvent;
 import com.owncloud.android.ui.events.SearchEvent;
 import com.owncloud.android.ui.helpers.SparseBooleanArrayParcelable;
@@ -1146,6 +1148,43 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
 
     }
 
+    @Subscribe(threadMode = ThreadMode.BACKGROUND)
+    public void onMessageEvent(FavoriteEvent event) {
+        Account currentAccount = com.owncloud.android.authentication.AccountUtils.
+                getCurrentOwnCloudAccount(MainApp.getAppContext());
+
+        OwnCloudAccount ocAccount = null;
+        try {
+            ocAccount = new OwnCloudAccount(
+                        currentAccount,
+                        MainApp.getAppContext()
+                );
+
+            OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
+                    getClientFor(ocAccount, MainApp.getAppContext());
+
+            ToggleFavoriteOperation toggleFavoriteOperation = new ToggleFavoriteOperation(event.shouldFavorite,
+                    event.remotePath);
+            RemoteOperationResult remoteOperationResult = toggleFavoriteOperation.execute(mClient);
+
+            if (remoteOperationResult.isSuccess()) {
+                mAdapter.setFavoriteAttributeForItemID(event.remoteId, event.shouldFavorite);
+            }
+
+        } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
+            e.printStackTrace();
+        } catch (AuthenticatorException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (OperationCanceledException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
+
     @Subscribe(threadMode = ThreadMode.BACKGROUND)
     public void onMessageEvent(SearchEvent event) {
         setEmptyListLoadingMessage();

+ 1 - 1
src/main/res/layout/grid_image.xml

@@ -44,7 +44,7 @@
             android:layout_height="24dp"
             android:layout_marginStart="6dp"
             android:layout_marginEnd="6dp"
-            android:layout_gravity="bottom|left"
+            android:layout_gravity="start|left"
             android:background="@drawable/favorite_button_selector"
             android:focusable="false"
             android:focusableInTouchMode="false"/>

+ 1 - 1
src/main/res/layout/grid_item.xml

@@ -43,7 +43,7 @@
             android:id="@+id/favorite_action"
             android:layout_width="24dp"
             android:layout_height="24dp"
-            android:layout_gravity="bottom|left"
+            android:layout_gravity="start|left"
             android:layout_marginEnd="6dp"
             android:layout_marginStart="6dp"
             android:background="@drawable/favorite_button_selector"