From 856eb479e4bb36aad1d0c4f50f1df27b295af8ba Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 10 Nov 2016 00:22:01 +0800 Subject: [PATCH] Add FAB menu --- .../com/topjohnwu/magisk/FABBehavior.java | 113 ++++++++++++++++++ .../com/topjohnwu/magisk/ModulesFragment.java | 2 +- app/src/main/res/drawable/ic_archive.xml | 9 ++ app/src/main/res/layout/modules_fragment.xml | 44 +++++-- app/src/main/res/menu/drawer.xml | 10 +- app/src/main/res/values/strings.xml | 1 + 6 files changed, 164 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/FABBehavior.java create mode 100644 app/src/main/res/drawable/ic_archive.xml diff --git a/app/src/main/java/com/topjohnwu/magisk/FABBehavior.java b/app/src/main/java/com/topjohnwu/magisk/FABBehavior.java new file mode 100644 index 000000000..3c2542725 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/FABBehavior.java @@ -0,0 +1,113 @@ +package com.topjohnwu.magisk; + + +import android.content.Context; +import android.support.design.widget.CoordinatorLayout; +import android.support.design.widget.Snackbar; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.ViewPropertyAnimatorCompat; +import android.util.AttributeSet; +import android.view.View; + +import com.github.clans.fab.FloatingActionMenu; + +import java.util.List; + +/** + * Created by Matteo on 08/08/2015. + * + * Floating Action Menu Behavior for Clans.FloatingActionButton + * https://github.com/Clans/FloatingActionButton/ + * + * Use this behavior as your app:layout_behavior attribute in your Floating Action Menu to use the + * FabMenu in a Coordinator Layout. + * + * Remember to use the correct namespace for the fab: + * xmlns:fab="http://schemas.android.com/apk/res-auto" + */ +public class FABBehavior extends CoordinatorLayout.Behavior { + private float mTranslationY; + + public FABBehavior(Context context, AttributeSet attrs) { + super(); + } + + @Override + public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { + return dependency instanceof Snackbar.SnackbarLayout; + } + + @Override + public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { + if (dependency instanceof Snackbar.SnackbarLayout) { + updateTranslation(parent, child); + } + + return false; + } + + @Override + public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) { + if (dependency instanceof Snackbar.SnackbarLayout) { + revertTranslation(child); + } + } + + private void updateTranslation(CoordinatorLayout parent, View child) { + float translationY = getTranslationY(parent, child); + if (translationY != mTranslationY) { + ViewPropertyAnimatorCompat anim = ViewCompat.animate(child); + anim.cancel(); + anim.translationY(translationY).setDuration(100); + mTranslationY = translationY; + } + } + + private void revertTranslation(View child) { + if (mTranslationY != 0) { + ViewPropertyAnimatorCompat anim = ViewCompat.animate(child); + anim.cancel(); + anim.translationY(0).setDuration(100); + mTranslationY = 0; + } + } + + private float getTranslationY(CoordinatorLayout parent, View child) { + float minOffset = 0.0F; + List dependencies = parent.getDependencies(child); + int i = 0; + + for (int z = dependencies.size(); i < z; ++i) { + View view = (View) dependencies.get(i); + if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(child, view)) { + minOffset = Math.min(minOffset, ViewCompat.getTranslationY(view) - (float) view.getHeight()); + } + } + + return minOffset; + } + + /** + * onStartNestedScroll and onNestedScroll will hide/show the FabMenu when a scroll is detected. + */ + @Override + public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, + View directTargetChild, View target, int nestedScrollAxes) { + return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || + super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, + nestedScrollAxes); + } + + @Override + public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, + int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { + super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, + dyUnconsumed); + FloatingActionMenu fabMenu = (FloatingActionMenu) child; + if (dyConsumed > 0 && !fabMenu.isMenuButtonHidden()) { + fabMenu.hideMenuButton(true); + } else if (dyConsumed < 0 && fabMenu.isMenuButtonHidden()) { + fabMenu.showMenuButton(true); + } + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index 612630105..5fad2c3af 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -7,7 +7,6 @@ import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; -import android.support.design.widget.FloatingActionButton; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -15,6 +14,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import com.github.clans.fab.FloatingActionButton; import com.ipaulpro.afilechooser.utils.FileUtils; import com.topjohnwu.magisk.adapters.ModulesAdapter; import com.topjohnwu.magisk.module.Module; diff --git a/app/src/main/res/drawable/ic_archive.xml b/app/src/main/res/drawable/ic_archive.xml new file mode 100644 index 000000000..8b18a9d56 --- /dev/null +++ b/app/src/main/res/drawable/ic_archive.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/modules_fragment.xml b/app/src/main/res/layout/modules_fragment.xml index 94b4a08a4..d0158cf4b 100644 --- a/app/src/main/res/layout/modules_fragment.xml +++ b/app/src/main/res/layout/modules_fragment.xml @@ -2,6 +2,7 @@ - + + + + + + - \ No newline at end of file diff --git a/app/src/main/res/menu/drawer.xml b/app/src/main/res/menu/drawer.xml index 2d83d30e3..f0cab2d82 100644 --- a/app/src/main/res/menu/drawer.xml +++ b/app/src/main/res/menu/drawer.xml @@ -1,15 +1,15 @@ - + - - - + Module will be disabled at next reboot Module will be enabled at next reboot Created by %1$s + Flash Module Zip Update Available