From a608ec162abf1c1ca4ab7067822d7f120d4c16dd Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 13 Dec 2018 20:21:03 -0600 Subject: [PATCH] Create the menus for Balances and Net Worth that make use of a customized TabLayout and a ViewPager to obtain the desired effect. - Created custom themed attribute colorBackgroundFocusable to assign the correct background color to the balances and net worth tabs. - Renamed BalancesFragment to HomeFragment because, the BalancesFragment will now only be used to display the Balances in the above mentioned ViewPager. --- .../fragments/BalancesFragment.kt | 83 ----------- .../fragments/HomeFragment.kt | 139 ++++++++++++++++++ ...ragment_balances.xml => fragment_home.xml} | 48 ++++-- .../main/res/navigation/mobile_navigation.xml | 8 +- app/src/main/res/values/attr.xml | 5 + app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/styles.xml | 4 + 7 files changed, 187 insertions(+), 103 deletions(-) delete mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt create mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt rename app/src/main/res/layout/{fragment_balances.xml => fragment_home.xml} (81%) create mode 100644 app/src/main/res/values/attr.xml diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt deleted file mode 100644 index 2d3fed9..0000000 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt +++ /dev/null @@ -1,83 +0,0 @@ -package cy.agorise.bitsybitshareswallet.fragments - -import androidx.lifecycle.ViewModelProviders -import android.os.Bundle -import android.preference.PreferenceManager -import android.view.* -import androidx.fragment.app.Fragment -import androidx.lifecycle.Observer -import androidx.navigation.Navigation -import androidx.navigation.fragment.findNavController -import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.LinearLayoutManager - -import cy.agorise.bitsybitshareswallet.R -import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter -import cy.agorise.bitsybitshareswallet.database.entities.UserAccount -import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail -import cy.agorise.bitsybitshareswallet.utils.Constants -import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel -import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel -import kotlinx.android.synthetic.main.fragment_balances.* - -class BalancesFragment : Fragment() { - - private lateinit var mUserAccountViewModel: UserAccountViewModel - private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - setHasOptionsMenu(true) - - return inflater.inflate(R.layout.fragment_balances, container, false) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - // Configure UserAccountViewModel to show the current account - mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java) - - val userId = PreferenceManager.getDefaultSharedPreferences(context) - .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") - - mUserAccountViewModel.getUserAccount(userId!!).observe(this, Observer{ user -> - tvAccountName.text = user.name - }) - - // Configure BalanceDetailViewModel to show the current balances - mBalanceDetailViewModel = ViewModelProviders.of(this).get(BalanceDetailViewModel::class.java) - - val balancesAdapter = BalancesAdapter(context!!) - rvBalances.adapter = balancesAdapter - rvBalances.layoutManager = LinearLayoutManager(context!!) - rvBalances.addItemDecoration(DividerItemDecoration(context!!, DividerItemDecoration.VERTICAL)) - - mBalanceDetailViewModel.getAll().observe(this, Observer> { balancesDetails -> - balancesAdapter.replaceAll(balancesDetails) - }) - - // Navigate to the Receive Transaction Fragment - fabReceiveTransaction.setOnClickListener ( - Navigation.createNavigateOnClickListener(R.id.receive_action) - ) - - // Navigate to the Send Transaction Fragment without activating the camera - fabSendTransaction.setOnClickListener( - Navigation.createNavigateOnClickListener(R.id.send_action) - ) - - // Navigate to the Send Transaction Fragment using Navigation's SafeArgs to activate the camera - fabSendTransactionCamera.setOnClickListener { - val action = BalancesFragmentDirections.sendActionCamera() - action.setOpenCamera(true) - findNavController().navigate(action) - } - } - - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.menu_balances, menu) - } -} diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt new file mode 100644 index 0000000..bb113ae --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt @@ -0,0 +1,139 @@ +package cy.agorise.bitsybitshareswallet.fragments + +import androidx.lifecycle.ViewModelProviders +import android.os.Bundle +import android.preference.PreferenceManager +import android.view.* +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentPagerAdapter +import androidx.lifecycle.Observer +import androidx.navigation.Navigation +import androidx.navigation.fragment.findNavController + +import cy.agorise.bitsybitshareswallet.R +import cy.agorise.bitsybitshareswallet.database.entities.UserAccount +import cy.agorise.bitsybitshareswallet.utils.Constants +import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel +import kotlinx.android.synthetic.main.fragment_home.* +import kotlinx.android.synthetic.main.item_balance.view.* + +class HomeFragment : Fragment() { + + private lateinit var mUserAccountViewModel: UserAccountViewModel +// private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + setHasOptionsMenu(true) + + return inflater.inflate(R.layout.fragment_home, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + // Configure UserAccountViewModel to show the current account + mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java) + + val userId = PreferenceManager.getDefaultSharedPreferences(context) + .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") + + mUserAccountViewModel.getUserAccount(userId!!).observe(this, Observer{ user -> + tvAccountName.text = user.name + }) + +// // Configure BalanceDetailViewModel to show the current balances +// mBalanceDetailViewModel = ViewModelProviders.of(this).get(BalanceDetailViewModel::class.java) +// +// val balancesAdapter = BalancesAdapter(context!!) +// rvBalances.adapter = balancesAdapter +// rvBalances.layoutManager = LinearLayoutManager(context!!) +// rvBalances.addItemDecoration(DividerItemDecoration(context!!, DividerItemDecoration.VERTICAL)) +// +// mBalanceDetailViewModel.getAll().observe(this, Observer> { balancesDetails -> +// balancesAdapter.replaceAll(balancesDetails) +// }) + + // Navigate to the Receive Transaction Fragment + fabReceiveTransaction.setOnClickListener ( + Navigation.createNavigateOnClickListener(R.id.receive_action) + ) + + // Navigate to the Send Transaction Fragment without activating the camera + fabSendTransaction.setOnClickListener( + Navigation.createNavigateOnClickListener(R.id.send_action) + ) + + // Navigate to the Send Transaction Fragment using Navigation's SafeArgs to activate the camera + fabSendTransactionCamera.setOnClickListener { + val action = HomeFragmentDirections.sendActionCamera() + action.setOpenCamera(true) + findNavController().navigate(action) + } + + val pagerAdapter = PagerAdapter(fragmentManager!!) + viewPager.adapter = pagerAdapter + tabLayout.setupWithViewPager(viewPager) + } + + /** + * Pager adapter to create the placeholder fragments + */ + private inner class PagerAdapter internal constructor(fm: FragmentManager) : FragmentPagerAdapter(fm) { + + override fun getItem(position: Int): Fragment { + // getItem is called to instantiate the fragment for the given page. + // Return a PlaceholderFragment (defined as a static inner class below). + return PlaceholderFragment.newInstance(position + 1) + } + + override fun getPageTitle(position: Int): CharSequence? { + return listOf(getString(R.string.title_balances), "Net Worth")[position] + } + + override fun getCount(): Int { + return 2 + } + } + + /** + * A placeholder fragment containing a simple view. + */ + class PlaceholderFragment : Fragment() { + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + val rootView = inflater.inflate(R.layout.item_balance, container, false) + val text = "Hello World from section ${arguments?.getInt(ARG_SECTION_NUMBER)}" + rootView.tvBalance.text = text + return rootView + } + + companion object { + /** + * The fragment argument representing the section number for this + * fragment. + */ + private const val ARG_SECTION_NUMBER = "section_number" + + /** + * Returns a new instance of this fragment for the given section + * number. + */ + fun newInstance(sectionNumber: Int): PlaceholderFragment { + val fragment = PlaceholderFragment() + val args = Bundle() + args.putInt(ARG_SECTION_NUMBER, sectionNumber) + fragment.arguments = args + return fragment + } + } + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.menu_balances, menu) + } +} diff --git a/app/src/main/res/layout/fragment_balances.xml b/app/src/main/res/layout/fragment_home.xml similarity index 81% rename from app/src/main/res/layout/fragment_balances.xml rename to app/src/main/res/layout/fragment_home.xml index e810658..839c71a 100644 --- a/app/src/main/res/layout/fragment_balances.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" - tools:context=".fragments.BalancesFragment"> + tools:context=".fragments.HomeFragment"> - - - + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:background="?attr/themedColorBackgroundFloating" + app:tabSelectedTextColor="?android:textColorPrimary" + app:tabIndicatorColor="?android:colorControlHighlight" + app:tabIndicatorHeight="50dp" + app:tabMode="scrollable" /> + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/navigation/mobile_navigation.xml b/app/src/main/res/navigation/mobile_navigation.xml index 0779d6c..b6a9619 100644 --- a/app/src/main/res/navigation/mobile_navigation.xml +++ b/app/src/main/res/navigation/mobile_navigation.xml @@ -4,13 +4,13 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mobile_navigation" - app:startDestination="@id/balancesFragment"> + app:startDestination="@id/home_dest"> + tools:layout="@layout/fragment_home"> + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index cce4962..05a6f26 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -4,6 +4,9 @@ #006ba4 #0099d6 + + #424242 + #000 #888 #e0e0e0 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c8b8039..62b84b9 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -6,6 +6,8 @@ @color/colorPrimary @color/colorPrimaryDark @color/colorAccent + + @android:color/white