Browse Source

refactored bottom sheet menu to make use of Butterknife

AndyScherzinger 7 years ago
parent
commit
567e6c0757

+ 42 - 0
src/main/java/com/owncloud/android/ui/fragment/OCFileListBottomSheetActions.java

@@ -0,0 +1,42 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2018 Andy Scherzinger
+ *
+ * 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.fragment;
+
+/**
+ * Actions interface to be implemented by any class that makes use of
+ * {@link com.owncloud.android.ui.fragment.OCFileListBottomSheetDialog}.
+ */
+public interface OCFileListBottomSheetActions {
+    /**
+     * creates a folde rwithin the actual folder.
+     */
+    void createFolder();
+
+    /**
+     * offers a file upload with the Android OS file picker to the current folder.
+     */
+    void uploadFromApp();
+
+    /**
+     * offers a file upload with the app file picker to the current folder.
+     */
+    void uploadFiles();
+}

+ 124 - 0
src/main/java/com/owncloud/android/ui/fragment/OCFileListBottomSheetDialog.java

@@ -0,0 +1,124 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2018 Andy Scherzinger
+ *
+ * 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.fragment;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.design.widget.BottomSheetBehavior;
+import android.support.design.widget.BottomSheetDialog;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import com.owncloud.android.R;
+import com.owncloud.android.utils.ThemeUtils;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import butterknife.Unbinder;
+
+/**
+ * FAB menu {@link android.app.Dialog} styled as a bottom sheet for main actions.
+ */
+public class OCFileListBottomSheetDialog extends BottomSheetDialog {
+    @BindView(R.id.menu_icon_upload_files)
+    ImageView mIconUploadFiles;
+    @BindView(R.id.menu_icon_upload_from_app)
+    ImageView mIconUploadFromApp;
+    @BindView(R.id.menu_icon_mkdir)
+    ImageView mIconMakeDir;
+
+    @BindView(R.id.add_to_cloud)
+    TextView mHeadline;
+
+    @BindView(R.id.menu_upload_files)
+    LinearLayout mMenuUploadFiles;
+    @BindView(R.id.menu_upload_from_app)
+    LinearLayout mMenuUploadFromApp;
+    @BindView(R.id.menu_mkdir)
+    LinearLayout mMenuMakeDir;
+
+    private Unbinder unbinder;
+
+    private OCFileListBottomSheetActions mActions;
+
+
+    public OCFileListBottomSheetDialog(@NonNull Context context, OCFileListBottomSheetActions
+            actions) {
+        super(context);
+        mActions = actions;
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        final View view = getLayoutInflater()
+                .inflate(R.layout.file_list_actions_bottom_sheet_fragment, null);
+        setContentView(view);
+        if (getWindow() != null) {
+            getWindow().setLayout(
+                    ViewGroup.LayoutParams.MATCH_PARENT,
+                    ViewGroup.LayoutParams.WRAP_CONTENT
+            );
+        }
+
+        unbinder = ButterKnife.bind(this, view);
+        ThemeUtils.tintDrawable(mIconUploadFiles.getDrawable(), ThemeUtils.primaryColor());
+        ThemeUtils.tintDrawable(mIconUploadFromApp.getDrawable(), ThemeUtils.primaryColor());
+        ThemeUtils.tintDrawable(mIconMakeDir.getDrawable(), ThemeUtils.primaryColor());
+
+        mHeadline.setText(getContext().getResources().getString(R.string.add_to_cloud,
+                ThemeUtils.getDefaultDisplayNameForRootFolder()));
+
+        mMenuUploadFiles.setOnClickListener(v -> uploadFiles());
+        mMenuUploadFromApp.setOnClickListener(v -> uploadFromApp());
+        mMenuMakeDir.setOnClickListener(v -> createFolder());
+
+        setOnShowListener(d ->
+                BottomSheetBehavior.from((View) view.getParent())
+                        .setPeekHeight(view.getMeasuredHeight())
+        );
+    }
+
+    private void createFolder() {
+        mActions.createFolder();
+        dismiss();
+    }
+
+    private void uploadFromApp() {
+        mActions.uploadFromApp();
+        dismiss();
+    }
+
+    private void uploadFiles() {
+        mActions.uploadFiles();
+        dismiss();
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+        unbinder.unbind();
+    }
+}

+ 9 - 34
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -125,7 +125,8 @@ import java.util.Set;
  * A Fragment that lists all files and folders in a given path.
  * TODO refactor to get rid of direct dependency on FileDisplayActivity
  */
-public class OCFileListFragment extends ExtendedListFragment implements OCFileListFragmentInterface {
+public class OCFileListFragment extends ExtendedListFragment implements
+        OCFileListFragmentInterface, OCFileListBottomSheetActions {
 
     private static final String TAG = OCFileListFragment.class.getSimpleName();
 
@@ -375,43 +376,18 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
      */
     private void registerFabListener() {
         getFabMain().setOnClickListener(v -> {
-            final View fileListActionsBottomSheet = getLayoutInflater()
-                    .inflate(R.layout.file_list_actions_bottom_sheet_fragment, null);
-            final BottomSheetDialog dialog = new BottomSheetDialog(getContext());
-            dialog.setContentView(fileListActionsBottomSheet);
-
-            ThemeUtils.tintDrawable(((ImageView) fileListActionsBottomSheet.findViewById(R.id.menu_icon_upload_files))
-                    .getDrawable(),ThemeUtils.primaryColor());
-            ThemeUtils.tintDrawable(((ImageView) fileListActionsBottomSheet.findViewById(R.id.menu_icon_upload_from_app))
-                    .getDrawable(),ThemeUtils.primaryColor());
-            ThemeUtils.tintDrawable(((ImageView) fileListActionsBottomSheet.findViewById(R.id.menu_icon_mkdir))
-                    .getDrawable(),ThemeUtils.primaryColor());
-
-            ((TextView) fileListActionsBottomSheet.findViewById(R.id.add_to_cloud)).setText
-                    (getResources().getString(R.string.add_to_cloud, ThemeUtils.getDefaultDisplayNameForRootFolder()));
-
-            fileListActionsBottomSheet.findViewById(R.id.menu_upload_files)
-                    .setOnClickListener(view -> uploadFiles(dialog));
-            fileListActionsBottomSheet.findViewById(R.id.menu_upload_from_app)
-                    .setOnClickListener(view -> uploadFromApp(dialog));
-            fileListActionsBottomSheet.findViewById(R.id.menu_mkdir)
-                    .setOnClickListener(view -> createFolder(dialog));
-
-            dialog.setOnShowListener(d ->
-                    BottomSheetBehavior.from((View) fileListActionsBottomSheet.getParent())
-                            .setPeekHeight(fileListActionsBottomSheet.getMeasuredHeight())
-            );
-            dialog.show();
+            new OCFileListBottomSheetDialog(getContext(), this).show();
         });
     }
 
-    private void createFolder(BottomSheetDialog dialog) {
+    @Override
+    public void createFolder() {
         CreateFolderDialogFragment.newInstance(mFile)
                 .show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
-        dialog.dismiss();
     }
 
-    private void uploadFromApp(BottomSheetDialog dialog) {
+    @Override
+    public void uploadFromApp() {
         Intent action = new Intent(Intent.ACTION_GET_CONTENT);
         action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
         //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean
@@ -422,16 +398,15 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
                 Intent.createChooser(action, getString(R.string.upload_chooser_title)),
                 FileDisplayActivity.REQUEST_CODE__SELECT_CONTENT_FROM_APPS
         );
-        dialog.dismiss();
     }
 
-    private void uploadFiles(BottomSheetDialog dialog) {
+    @Override
+    public void uploadFiles() {
         UploadFilesActivity.startUploadActivityForResult(
                 getActivity(),
                 ((FileActivity) getActivity()).getAccount(),
                 FileDisplayActivity.REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM
         );
-        dialog.dismiss();
     }
 
     @Override