nice side-swipe switch between account summary and transaction list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 19:24:50 +0000 (19:24 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 19:24:50 +0000 (19:24 +0000)
app/src/main/AndroidManifest.xml
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/TransactionListFragment.java
app/src/main/res/layout/activity_main.xml

index 5f324207c644e721f4eb91db090df304b7a8d0a6..7171718a08c95f4d3ca86d74ec0989c2f5746508 100644 (file)
@@ -1,5 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
+<?xml version="1.0" encoding="utf-8"?><!--
   ~ Copyright © 2019 Damyan Ivanov.
   ~ This file is part of Mobile-Ledger.
   ~ Mobile-Ledger is free software: you can distribute it and/or modify it
   ~
   ~ You should have received a copy of the GNU General Public License
   ~ along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
-  -->
-
+-->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="net.ktnx.mobileledger">
 
     <uses-permission android:name="android.permission.INTERNET" />
 
     <application
+        android:name=".MobileLedgerApplication"
         android:allowBackup="true"
         android:fullBackupContent="@xml/backup_descriptor"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
-        android:name="net.ktnx.mobileledger.MobileLedgerApplication"
         android:theme="@style/AppTheme">
         <activity
             android:name=".ui.activity.MainActivity"
@@ -36,6 +34,7 @@
             android:theme="@style/AppTheme.NoActionBar">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
+
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
index 82709138207aaf553b51f29b4f893a8d1c409895..830bf6367f0f2b65850e952b1a5af7a528648abd 100644 (file)
@@ -95,24 +95,24 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
+        Log.d("flow", "AccountSummaryFragment.onCreateView()");
         return inflater.inflate(R.layout.account_summary_fragment, container, false);
     }
 
     @Override
+
     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+        Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
         super.onActivityCreated(savedInstanceState);
 
-        mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
-
         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
         modelAdapter = new AccountSummaryAdapter();
 
-        RecyclerView root = mActivity.findViewById(R.id.account_root);
-        root.setAdapter(modelAdapter);
-
+        root = mActivity.findViewById(R.id.account_root);
         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
         llm.setOrientation(LinearLayoutManager.VERTICAL);
         root.setLayoutManager(llm);
+        root.setAdapter(modelAdapter);
 
         fab = mActivity.findViewById(R.id.btn_add_transaction);
 
index 0ed837197df4950e228cc90f30f073a96a3c7f49..e245a250ee47238958b7e774e86891df4410d6c2 100644 (file)
@@ -23,9 +23,11 @@ import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.annotation.ColorInt;
+import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
-import android.support.v4.app.FragmentTransaction;
+import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.GravityCompat;
+import android.support.v4.view.ViewPager;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
@@ -65,7 +67,27 @@ public class MainActivity extends AppCompatActivity {
     private View bTransactionListCancelDownload;
     private ProgressBar progressBar;
     private LinearLayout progressLayout;
+    private SectionsPagerAdapter mSectionsPagerAdapter;
+    private ViewPager mViewPager;
 
+    @Override
+    protected void onStart() {
+        super.onStart();
+
+        Data.lastUpdateDate.set(null);
+        updateLastUpdateTextFromDB();
+        Date lastUpdate = Data.lastUpdateDate.get();
+
+        long now = new Date().getTime();
+        if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
+            if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
+            else Log.d("db",
+                    String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
+                            lastUpdate.getTime() / 1000f, now / 1000f));
+
+            scheduleTransactionListRetrieval();
+        }
+    }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -102,8 +124,27 @@ public class MainActivity extends AppCompatActivity {
                 "Can't get hold on the transaction value progress bar layout");
 
         fragmentManager = getSupportFragmentManager();
+        mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
 
-        onAccountSummaryClicked(null);
+        mViewPager = findViewById(R.id.root_frame);
+        mViewPager.setAdapter(mSectionsPagerAdapter);
+        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
+            @Override
+            public void onPageSelected(int position) {
+                switch (position) {
+                    case 0:
+                        markDrawerItemCurrent(R.id.nav_account_summary);
+                        break;
+                    case 1:
+                        markDrawerItemCurrent(R.id.nav_latest_transactions);
+                        break;
+                    default:
+                        Log.e("MainActivity", String.format("Unexpected page index %d", position));
+                }
+
+                super.onPageSelected(position);
+            }
+        });
 
         Data.lastUpdateDate.addObserver(new Observer() {
             @Override
@@ -127,28 +168,13 @@ public class MainActivity extends AppCompatActivity {
             }
         });
 
-        updateLastUpdateTextFromDB();
-        Date lastUpdate = Data.lastUpdateDate.get();
-
-        long now = new Date().getTime();
-        if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
-            if (lastUpdate == null) Log.d("db", "WEB data never fetched. scheduling a fetch");
-            else Log.d("db",
-                    String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
-                            lastUpdate.getTime() / 1000f, now / 1000f));
-
-            scheduleTransactionListRetrieval();
-        }
-
         Data.ledgerTitle.addObserver(new Observer() {
             @Override
             public void update(Observable o, Object arg) {
                 runOnUiThread(() -> {
                     String title = Data.ledgerTitle.get();
-                    if (title == null)
-                        toolbar.setSubtitle("");
-                    else
-                        toolbar.setSubtitle(title);
+                    if (title == null) toolbar.setSubtitle("");
+                    else toolbar.setSubtitle(title);
                 });
             }
         });
@@ -225,45 +251,43 @@ public class MainActivity extends AppCompatActivity {
     public void onAccountSummaryClicked(View view) {
         drawer.closeDrawers();
 
-        resetFragmentBackStack();
-
         showAccountSummaryFragment();
     }
     private void showAccountSummaryFragment() {
-        FragmentTransaction ft = fragmentManager.beginTransaction();
-        accountSummaryFragment = new AccountSummaryFragment();
-        ft.replace(R.id.root_frame, accountSummaryFragment);
-        ft.commit();
-        currentFragment = accountSummaryFragment;
+        mViewPager.setCurrentItem(0, true);
+//        FragmentTransaction ft = fragmentManager.beginTransaction();
+//        accountSummaryFragment = new AccountSummaryFragment();
+//        ft.replace(R.id.root_frame, accountSummaryFragment);
+//        ft.commit();
+//        currentFragment = accountSummaryFragment;
     }
     public void onLatestTransactionsClicked(View view) {
         drawer.closeDrawers();
 
-        resetFragmentBackStack();
-
         showTransactionsFragment(null);
     }
     private void resetFragmentBackStack() {
 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
     }
     private void showTransactionsFragment(LedgerAccount account) {
-        FragmentTransaction ft = fragmentManager.beginTransaction();
-        if (transactionListFragment == null) {
-            Log.d("flow", "MainActivity creating TransactionListFragment");
-            transactionListFragment = new TransactionListFragment();
-        }
-        Bundle bundle = new Bundle();
-        if (account != null) {
-            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
-                    account.getName());
-        }
-        transactionListFragment.setArguments(bundle);
-        ft.replace(R.id.root_frame, transactionListFragment);
-        if (account != null)
-            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
-        ft.commit();
-
-        currentFragment = transactionListFragment;
+        mViewPager.setCurrentItem(1, true);
+//        FragmentTransaction ft = fragmentManager.beginTransaction();
+//        if (transactionListFragment == null) {
+//            Log.d("flow", "MainActivity creating TransactionListFragment");
+//            transactionListFragment = new TransactionListFragment();
+//        }
+//        Bundle bundle = new Bundle();
+//        if (account != null) {
+//            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
+//                    account.getName());
+//        }
+//        transactionListFragment.setArguments(bundle);
+//        ft.replace(R.id.root_frame, transactionListFragment);
+//        if (account != null)
+//            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
+//        ft.commit();
+//
+//        currentFragment = transactionListFragment;
     }
     public void showAccountTransactions(LedgerAccount account) {
         showTransactionsFragment(account);
@@ -336,5 +360,30 @@ public class MainActivity extends AppCompatActivity {
             progressBar.setIndeterminate(false);
         }
     }
+    public class SectionsPagerAdapter extends FragmentPagerAdapter {
+
+        public SectionsPagerAdapter(FragmentManager fm) {
+            super(fm);
+        }
+
+        @Override
+        public Fragment getItem(int position) {
+            Log.d("main", String.format("Switching to gragment %d", position));
+            switch (position) {
+                case 0:
+                    return new AccountSummaryFragment();
+                case 1:
+                    return new TransactionListFragment();
+                default:
+                    throw new IllegalStateException(
+                            String.format("Unexpected fragment index: " + "%d", position));
+            }
+        }
+
+        @Override
+        public int getCount() {
+            return 2;
+        }
+    }
 
 }
index 79c34b8950ca8d8f86a6d19704de1d1a06a67c38..b9a3b838be08a64d894484ed2730ac839357f43e 100644 (file)
@@ -120,8 +120,6 @@ public class TransactionListFragment extends MobileLedgerListFragment {
         Log.d("flow", "TransactionListFragment.onActivityCreated called");
         super.onActivityCreated(savedInstanceState);
 
-        mActivity.markDrawerItemCurrent(R.id.nav_latest_transactions);
-
         swiper = mActivity.findViewById(R.id.transaction_swipe);
         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
         root = mActivity.findViewById(R.id.transaction_root);
@@ -131,12 +129,10 @@ public class TransactionListFragment extends MobileLedgerListFragment {
         modelAdapter = new TransactionListAdapter();
 
         modelAdapter.setBoldAccountName(mShowOnlyAccountName);
+        root.setAdapter(modelAdapter);
 
         FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
 
-        RecyclerView root = mActivity.findViewById(R.id.transaction_root);
-        root.setAdapter(modelAdapter);
-
         fab.show();
         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
             @Override
index 2b26944de57588953bea7673f3b6faa1cc73f404..dacbcea8866eb46eb52c174ec736c6270f294035 100644 (file)
 
             </LinearLayout>
 
-            <FrameLayout
+            <android.support.v4.view.ViewPager
                 android:id="@+id/root_frame"
                 android:layout_width="match_parent"
                 android:layout_height="0dp"
                 app:layout_constraintBottom_toBottomOf="parent"
-                app:layout_constraintTop_toBottomOf="@+id/main_header">
+                app:layout_constraintTop_toBottomOf="@+id/main_header"
+                app:layout_behavior="@string/appbar_scrolling_view_behavior"
+                >
 
-            </FrameLayout>
+            </android.support.v4.view.ViewPager>
 
             <View
                 android:layout_width="0dp"