]> git.ktnx.net Git - mobile-ledger.git/commitdiff
drop dummy bottom-list entries and intercept draw up/down to hide/show the Add FAB
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 20 Apr 2019 07:41:51 +0000 (10:41 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 20 Apr 2019 07:41:51 +0000 (10:41 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index 093aff396d1a0fa51e232c9f219efed03704a917..654bd4dbc49ea527fab66783bcb51b6dccbb4294 100644 (file)
 
 package net.ktnx.mobileledger.ui;
 
+import android.view.MotionEvent;
+
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListAdapter;
 import net.ktnx.mobileledger.utils.Colors;
+import net.ktnx.mobileledger.utils.DimensionUtils;
 
+import androidx.annotation.NonNull;
 import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.RecyclerView;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
 public class MobileLedgerListFragment extends Fragment {
-    protected MainActivity mActivity;
     public SwipeRefreshLayout swiper;
-    protected RecyclerView root;
     public TransactionListAdapter modelAdapter;
+    protected MainActivity mActivity;
+    protected RecyclerView root;
     protected void themeChanged(Integer counter) {
         swiper.setColorSchemeColors(Colors.primary);
     }
@@ -38,4 +42,45 @@ public class MobileLedgerListFragment extends Fragment {
         if (swiper == null) return;
         swiper.setRefreshing(isRunning);
     }
+    protected void manageFabOnScroll() {
+        int triggerPixels = DimensionUtils.dp2px(mActivity, 30f);
+        root.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
+            private float upAnchor = -1;
+            private float downAnchor = -1;
+            private float lastY;
+            @Override
+            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
+                switch (e.getActionMasked()) {
+                    case MotionEvent.ACTION_DOWN:
+                        lastY = upAnchor = downAnchor = e.getAxisValue(MotionEvent.AXIS_Y);
+                        break;
+                    case MotionEvent.ACTION_MOVE:
+                        final float currentY = e.getAxisValue(MotionEvent.AXIS_Y);
+                        if (currentY > lastY) {
+                            // swipe down
+                            upAnchor = lastY;
+
+                            mActivity.fabShouldShow();
+                        }
+                        else {
+                            // swipe up
+                            downAnchor = lastY;
+
+                            if (currentY < upAnchor - triggerPixels) mActivity.fabHide();
+                        }
+
+                        lastY = currentY;
+
+                        break;
+                }
+                return false;
+            }
+            @Override
+            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
+            }
+            @Override
+            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
+            }
+        });
+    }
 }
index ef912841b74d460ac804607636427f96aa246cd8..3ceccc15748f566394e9181b369d21677215bc2b 100644 (file)
@@ -108,7 +108,7 @@ public class AccountSummaryAdapter
 
     @Override
     public int getItemCount() {
-        return Data.accounts.size() + 1;
+        return Data.accounts.size();
     }
     public void startSelection() {
         try (LockHolder lh = Data.accounts.lockForWriting()) {
index d75577ff3d8f94c26f82a2493a960242d9605a78..86f66d8d895647921391cc6105dc0b7ec4b0e74f 100644 (file)
@@ -124,15 +124,9 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
 //                }));
 
         mActivity.fabShouldShow();
-        root.addOnScrollListener(new RecyclerView.OnScrollListener() {
-            @Override
-            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
-                if (fab != null) {
-                    if (dy < 0) mActivity.fabShouldShow();
-                    if (dy > 0) fab.hide();
-                }
-            }
-        });
+
+        manageFabOnScroll();
+
         swiper = mActivity.findViewById(R.id.account_swiper);
         Colors.themeWatch.observe(this, this::themeChanged);
         swiper.setOnRefreshListener(() -> {
index f934d4d69de330432d019619c722940516bd2ac9..8c573210d59980d08cf7d433193707fd3f3e6776 100644 (file)
@@ -631,6 +631,9 @@ public class MainActivity extends ProfileThemedActivity {
         MobileLedgerProfile profile = Data.profile.get();
         if ((profile != null) && profile.isPostingPermitted()) fab.show();
     }
+    public void fabHide() {
+        fab.hide();
+    }
     public void navProfilesHeadClicked(View view) {
         if (profileListExpanded) {
             collapseProfileList();
index 1e09ac4bd657162cc0d71b5ee7d8739b2a28c34f..a15ab0f6c68c18220db79d4ef0a3c17c2799ea5d 100644 (file)
@@ -126,7 +126,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
 
     @Override
     public int getItemCount() {
-        return Data.transactions.size() + 1;
+        return Data.transactions.size();
     }
     enum LoaderStep {HEAD, ACCOUNTS, DONE}
     private static class TransactionLoader
index c09c5c4a6e50c102073f91d6630df2a802e8739e..a82ee6350dff30a3f6363c758fae1a54e58260b6 100644 (file)
@@ -45,7 +45,6 @@ import org.jetbrains.annotations.NotNull;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.LinearLayoutManager;
-import androidx.recyclerview.widget.RecyclerView;
 
 import static android.content.Context.INPUT_METHOD_SERVICE;
 import static net.ktnx.mobileledger.utils.Logger.debug;
@@ -102,13 +101,8 @@ public class TransactionListFragment extends MobileLedgerListFragment {
         FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
 
         mActivity.fabShouldShow();
-        root.addOnScrollListener(new RecyclerView.OnScrollListener() {
-            @Override
-            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
-                if (dy < 0) mActivity.fabShouldShow();
-                if (dy > 0) fab.hide();
-            }
-        });
+
+        manageFabOnScroll();
 
         LinearLayoutManager llm = new LinearLayoutManager(mActivity);