]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
rework main activity fab show/hide to slide up/down
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
index da39640ad45ab12bd31c637f4dbbf7887bc98748..da86d3ae1402c2f503a8e8bdd78ba386053aa73c 100644 (file)
@@ -17,6 +17,9 @@
 
 package net.ktnx.mobileledger.ui.activity;
 
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.TimeInterpolator;
 import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.content.pm.ShortcutInfo;
@@ -29,6 +32,7 @@ import android.os.Bundle;
 import android.text.format.DateUtils;
 import android.util.Log;
 import android.view.View;
+import android.view.ViewPropertyAnimator;
 import android.view.animation.AnimationUtils;
 import android.widget.TextView;
 
@@ -54,7 +58,9 @@ import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
+import net.ktnx.mobileledger.ui.new_transaction.NewTransactionActivity;
 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
+import net.ktnx.mobileledger.ui.templates.TemplatesActivity;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
 import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Logger;
@@ -76,6 +82,8 @@ public class MainActivity extends ProfileThemedActivity {
     public static final String STATE_CURRENT_PAGE = "current_page";
     public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
     public static final String STATE_ACC_FILTER = "account_filter";
+    private static final boolean FAB_HIDDEN = false;
+    private static final boolean FAB_SHOWN = true;
     private SectionsPagerAdapter mSectionsPagerAdapter;
     private ProfilesRecyclerViewAdapter mProfileListAdapter;
     private int mCurrentPage;
@@ -86,6 +94,9 @@ public class MainActivity extends ProfileThemedActivity {
     private MobileLedgerProfile profile;
     private MainModel mainModel;
     private ActivityMainBinding b;
+    private int fabVerticalOffset;
+    private ViewPropertyAnimator fabSlideAnimator;
+    private boolean wantedFabState = FAB_SHOWN;
     @Override
     protected void onStart() {
         super.onStart();
@@ -146,7 +157,8 @@ public class MainActivity extends ProfileThemedActivity {
         Data.backgroundTasksRunning.observe(this, this::onRetrieveRunningChanged);
 
         if (barDrawerToggle == null) {
-            barDrawerToggle = new ActionBarDrawerToggle(this, b.drawerLayout, b.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
+            barDrawerToggle = new ActionBarDrawerToggle(this, b.drawerLayout, b.toolbar,
+                    R.string.navigation_drawer_open, R.string.navigation_drawer_close);
             b.drawerLayout.addDrawerListener(barDrawerToggle);
         }
         barDrawerToggle.syncState();
@@ -155,7 +167,8 @@ public class MainActivity extends ProfileThemedActivity {
             PackageInfo pi = getApplicationContext().getPackageManager()
                                                     .getPackageInfo(getPackageName(), 0);
             ((TextView) b.navUpper.findViewById(R.id.drawer_version_text)).setText(pi.versionName);
-            ((TextView) b.noProfilesLayout.findViewById(R.id.drawer_version_text)).setText(pi.versionName);
+            ((TextView) b.noProfilesLayout.findViewById(R.id.drawer_version_text)).setText(
+                    pi.versionName);
         }
         catch (Exception e) {
             e.printStackTrace();
@@ -198,11 +211,13 @@ public class MainActivity extends ProfileThemedActivity {
                      .setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
         }
 
-        b.btnNoProfilesAdd.setOnClickListener(v -> MobileLedgerProfile.startEditProfileActivity(this, null));
+        b.btnNoProfilesAdd.setOnClickListener(
+                v -> MobileLedgerProfile.startEditProfileActivity(this, null));
 
         b.btnAddTransaction.setOnClickListener(this::fabNewTransactionClicked);
 
-        b.navNewProfileButton.setOnClickListener(v -> MobileLedgerProfile.startEditProfileActivity(this, null));
+        b.navNewProfileButton.setOnClickListener(
+                v -> MobileLedgerProfile.startEditProfileActivity(this, null));
 
         b.transactionListCancelDownload.setOnClickListener(this::onStopTransactionRefreshClick);
 
@@ -247,8 +262,10 @@ public class MainActivity extends ProfileThemedActivity {
         b.navProfileList.setLayoutManager(llm);
 
         b.navProfilesStartEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
-        b.navProfilesCancelEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
-        b.navProfileListHeadButtons.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
+        b.navProfilesCancelEdit.setOnClickListener(
+                (v) -> mProfileListAdapter.flipEditingProfiles());
+        b.navProfileListHeadButtons.setOnClickListener(
+                (v) -> mProfileListAdapter.flipEditingProfiles());
         if (drawerListener == null) {
             drawerListener = new DrawerLayout.SimpleDrawerListener() {
                 @Override
@@ -300,7 +317,7 @@ public class MainActivity extends ProfileThemedActivity {
         b.navPatterns.setOnClickListener(this::onPatternsClick);
     }
     private void onPatternsClick(View view) {
-        Intent intent = new Intent(this, PatternsActivity.class);
+        Intent intent = new Intent(this, TemplatesActivity.class);
         startActivity(intent);
     }
     private void scheduleDataRetrievalIfStale(long lastUpdate) {
@@ -622,13 +639,58 @@ public class MainActivity extends ProfileThemedActivity {
         }
     }
     public void fabShouldShow() {
-        if ((profile != null) && profile.isPostingPermitted() && !b.drawerLayout.isOpen())
-            b.btnAddTransaction.show();
+        if (fabVerticalOffset <= 0) {
+            int top = b.btnAddTransaction.getTop();
+            int parentHeight = b.mainAppLayout.getHeight();
+            fabVerticalOffset = parentHeight - top;
+        }
+        if ((profile != null) && profile.isPostingPermitted() && !b.drawerLayout.isOpen()) {
+            fabShow();
+        }
         else
             fabHide();
     }
+    private void fabShow() {
+        if (wantedFabState == FAB_SHOWN)
+            return;
+
+//        b.btnAddTransaction.show();
+        if (this.fabSlideAnimator != null) {
+            this.fabSlideAnimator.cancel();
+            b.btnAddTransaction.clearAnimation();
+        }
+
+        wantedFabState = FAB_SHOWN;
+        slideFabTo(b.btnAddTransaction, 0, 200L,
+                com.google.android.material.animation.AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
+    }
     public void fabHide() {
-        b.btnAddTransaction.hide();
+        if (wantedFabState == FAB_HIDDEN)
+            return;
+
+        if (fabVerticalOffset <= 0)
+            return;
+
+//        b.btnAddTransaction.hide();
+        if (this.fabSlideAnimator != null) {
+            this.fabSlideAnimator.cancel();
+            b.btnAddTransaction.clearAnimation();
+        }
+
+        wantedFabState = FAB_HIDDEN;
+        slideFabTo(b.btnAddTransaction, fabVerticalOffset, 150L,
+                com.google.android.material.animation.AnimationUtils.FAST_OUT_LINEAR_IN_INTERPOLATOR);
+    }
+    private void slideFabTo(View view, int target, long duration, TimeInterpolator interpolator) {
+        fabSlideAnimator = view.animate()
+                               .translationY((float) target)
+                               .setInterpolator(interpolator)
+                               .setDuration(duration)
+                               .setListener(new AnimatorListenerAdapter() {
+                                   public void onAnimationEnd(Animator animation) {
+                                       fabSlideAnimator = null;
+                                   }
+                               });
     }
 
     public static class SectionsPagerAdapter extends FragmentStateAdapter {