From: Damyan Ivanov Date: Sat, 5 Jan 2019 19:24:50 +0000 (+0000) Subject: nice side-swipe switch between account summary and transaction list X-Git-Tag: v0.3~108 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=0628b3f6f00212726a03842dff2b169adb64c64c;p=mobile-ledger.git nice side-swipe switch between account summary and transaction list --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5f324207..7171718a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - - - +--> + diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index 82709138..830bf636 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 0ed83719..e245a250 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -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; + } + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 79c34b89..b9a3b838 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -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 diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2b26944d..dacbcea8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -122,14 +122,16 @@ - + app:layout_constraintTop_toBottomOf="@+id/main_header" + app:layout_behavior="@string/appbar_scrolling_view_behavior" + > - +