]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java
switch new transaction UI to FabManager
[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",
79 //                        absoluteY));
80
81                         if (absoluteY > absoluteAnchor + triggerAbsolutePixels) {
82                             // swipe down
83 //                            Logger.debug("touch", "SHOW");
84                             activity.showManagedFab();
85                             absoluteAnchor = absoluteY;
86                         }
87                         else if (absoluteY < absoluteAnchor - triggerAbsolutePixels) {
88                             // swipe up
89 //                            Logger.debug("touch", "HIDE");
90                             activity.hideManagedFab();
91                             absoluteAnchor = absoluteY;
92                         }
93
94                         break;
95                 }
96                 return false;
97             }
98         });
99     }
100     private void slideFabTo(int target, long duration, TimeInterpolator interpolator) {
101         fabSlideAnimator = fab.animate()
102                               .translationY((float) target)
103                               .setInterpolator(interpolator)
104                               .setDuration(duration)
105                               .setListener(new AnimatorListenerAdapter() {
106                                   public void onAnimationEnd(Animator animation) {
107                                       fabSlideAnimator = null;
108                                   }
109                               });
110     }
111     public void showFab() {
112         if (wantedFabState == FAB_SHOWN)
113             return;
114
115 //        b.btnAddTransaction.show();
116         if (this.fabSlideAnimator != null) {
117             this.fabSlideAnimator.cancel();
118             fab.clearAnimation();
119         }
120
121         wantedFabState = FAB_SHOWN;
122         slideFabTo(0, 200L,
123                 com.google.android.material.animation.AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
124     }
125     public void hideFab() {
126         if (wantedFabState == FAB_HIDDEN)
127             return;
128
129         calcVerticalFabOffset();
130
131 //        b.btnAddTransaction.hide();
132         if (this.fabSlideAnimator != null) {
133             this.fabSlideAnimator.cancel();
134             fab.clearAnimation();
135         }
136
137         wantedFabState = FAB_HIDDEN;
138         slideFabTo(fabVerticalOffset, 150L,
139                 com.google.android.material.animation.AnimationUtils.FAST_OUT_LINEAR_IN_INTERPOLATOR);
140     }
141     private void calcVerticalFabOffset() {
142         if (fabVerticalOffset > 0)
143             return;// already calculated
144         fab.measure(0, 0);
145
146         int height = fab.getMeasuredHeight();
147
148         int bottomMargin;
149
150         ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
151         if (layoutParams instanceof ViewGroup.MarginLayoutParams)
152             bottomMargin = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
153         else
154             throw new RuntimeException("Unsupported layout params " + layoutParams.getClass()
155                                                                                   .getCanonicalName());
156
157         fabVerticalOffset = height + bottomMargin;
158     }
159     public interface FabHandler {
160         Context getContext();
161
162         void showManagedFab();
163
164         void hideManagedFab();
165     }
166 }