]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java
measure FAB
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / FabManager.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.animation.Animator;
21 import android.animation.AnimatorListenerAdapter;
22 import android.animation.TimeInterpolator;
23 import android.annotation.SuppressLint;
24 import android.content.Context;
25 import android.view.MotionEvent;
26 import android.view.ViewGroup;
27 import android.view.ViewPropertyAnimator;
28
29 import androidx.annotation.NonNull;
30 import androidx.recyclerview.widget.RecyclerView;
31
32 import com.google.android.material.floatingactionbutton.FloatingActionButton;
33
34 import net.ktnx.mobileledger.utils.DimensionUtils;
35 import net.ktnx.mobileledger.utils.Logger;
36
37 public class FabManager {
38     private static final boolean FAB_SHOWN = true;
39     private static final boolean FAB_HIDDEN = false;
40     private final FloatingActionButton fab;
41     private boolean wantedFabState = FAB_SHOWN;
42     private ViewPropertyAnimator fabSlideAnimator;
43     private int fabVerticalOffset;
44     public FabManager(FloatingActionButton fab) {
45         this.fab = fab;
46     }
47     @SuppressLint("ClickableViewAccessibility")
48     public static void handle(FabHandler activity, RecyclerView recyclerView) {
49         final float triggerAbsolutePixels = DimensionUtils.dp2px(activity.getContext(), 20f);
50         final float triggerRelativePixels = triggerAbsolutePixels / 4f;
51         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
52             @Override
53             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
54                 Logger.debug("touch", "Scrolled " + dy);
55                 if (dy <= 0)
56                     activity.showManagedFab();
57                 else
58                     activity.hideManagedFab();
59
60                 super.onScrolled(recyclerView, dx, dy);
61             }
62         });
63         recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
64             private float absoluteAnchor = -1;
65             @Override
66             public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
67                 switch (e.getActionMasked()) {
68                     case MotionEvent.ACTION_DOWN:
69                         absoluteAnchor = e.getRawY();
70 //                        Logger.debug("touch",
71 //                                String.format(Locale.US, "Touch down at %4.2f", absoluteAnchor));
72                         break;
73                     case MotionEvent.ACTION_MOVE:
74                         if (absoluteAnchor < 0)
75                             break;
76
77                         final float absoluteY = e.getRawY();
78 //                        Logger.debug("touch", String.format(Locale.US, "Move to %4.2f", absoluteY));
79
80                         if (absoluteY > absoluteAnchor + triggerAbsolutePixels) {
81                             // swipe down
82 //                            Logger.debug("touch", "SHOW");
83                             activity.showManagedFab();
84                             absoluteAnchor = absoluteY;
85                         }
86                         else if (absoluteY < absoluteAnchor - triggerAbsolutePixels) {
87                             // swipe up
88 //                            Logger.debug("touch", "HIDE");
89                             activity.hideManagedFab();
90                             absoluteAnchor = absoluteY;
91                         }
92
93                         break;
94                 }
95                 return false;
96             }
97         });
98     }
99     private void slideFabTo(int target, long duration, TimeInterpolator interpolator) {
100         fabSlideAnimator = fab.animate()
101                               .translationY((float) target)
102                               .setInterpolator(interpolator)
103                               .setDuration(duration)
104                               .setListener(new AnimatorListenerAdapter() {
105                                   public void onAnimationEnd(Animator animation) {
106                                       fabSlideAnimator = null;
107                                   }
108                               });
109     }
110     public void showFab() {
111         if (wantedFabState == FAB_SHOWN)
112             return;
113
114 //        b.btnAddTransaction.show();
115         if (this.fabSlideAnimator != null) {
116             this.fabSlideAnimator.cancel();
117             fab.clearAnimation();
118         }
119
120         wantedFabState = FAB_SHOWN;
121         slideFabTo(0, 200L,
122                 com.google.android.material.animation.AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
123     }
124     public void hideFab() {
125         if (wantedFabState == FAB_HIDDEN)
126             return;
127
128         calcVerticalFabOffset();
129
130 //        b.btnAddTransaction.hide();
131         if (this.fabSlideAnimator != null) {
132             this.fabSlideAnimator.cancel();
133             fab.clearAnimation();
134         }
135
136         wantedFabState = FAB_HIDDEN;
137         slideFabTo(fabVerticalOffset, 150L,
138                 com.google.android.material.animation.AnimationUtils.FAST_OUT_LINEAR_IN_INTERPOLATOR);
139     }
140     private void calcVerticalFabOffset() {
141         if (fabVerticalOffset > 0)
142             return;// already calculated
143         fab.measure(0, 0);
144
145         int height = fab.getMeasuredHeight();
146
147         int bottomMargin;
148
149         ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
150         if (layoutParams instanceof ViewGroup.MarginLayoutParams)
151             bottomMargin = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
152         else
153             throw new RuntimeException("Unsupported layout params " + layoutParams.getClass()
154                                                                                   .getCanonicalName());
155
156         fabVerticalOffset = height + bottomMargin;
157     }
158     public interface FabHandler {
159         Context getContext();
160
161         void showManagedFab();
162
163         void hideManagedFab();
164     }
165 }