From 2f2c94cd03323b99e923c123e1e9d2687a86f278 Mon Sep 17 00:00:00 2001 From: Kevin Gorham Date: Sun, 2 Dec 2018 23:45:59 -0500 Subject: [PATCH] cleanup the fab logic and the generated fragment remnants --- .../wallet/ui/fragment/HomeFragment.kt | 183 +++++++----------- 1 file changed, 69 insertions(+), 114 deletions(-) diff --git a/zcash-android-wallet-app/app/src/main/java/cash/z/android/wallet/ui/fragment/HomeFragment.kt b/zcash-android-wallet-app/app/src/main/java/cash/z/android/wallet/ui/fragment/HomeFragment.kt index 5ce32f3..7db26f6 100644 --- a/zcash-android-wallet-app/app/src/main/java/cash/z/android/wallet/ui/fragment/HomeFragment.kt +++ b/zcash-android-wallet-app/app/src/main/java/cash/z/android/wallet/ui/fragment/HomeFragment.kt @@ -1,11 +1,14 @@ package cash.z.android.wallet.ui.fragment -import android.graphics.Color -import android.net.Uri +import android.app.Activity import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.annotation.ColorRes +import androidx.annotation.DrawableRes +import androidx.annotation.IdRes +import androidx.annotation.StringRes import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.Fragment import cash.z.android.wallet.R @@ -13,40 +16,17 @@ import cash.z.android.wallet.ui.activity.MainActivity import cash.z.wallet.sdk.jni.JniConverter import com.leinardi.android.speeddial.SpeedDialActionItem import kotlinx.android.synthetic.main.fragment_home.* -import com.leinardi.android.speeddial.SpeedDialView -import android.widget.Toast -// TODO: Rename parameter arguments, choose names that match -// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER -private const val ARG_PARAM1 = "param1" -private const val ARG_PARAM2 = "param2" - /** - * A simple [Fragment] subclass. - * Activities that contain this fragment must implement the - * [HomeFragment.OnFragmentInteractionListener] interface - * to handle interaction events. - * Use the [HomeFragment.newInstance] factory method to - * create an instance of this fragment. - * + * Fragment representing the home screen of the app. This is the screen most often seen by the user when launching the + * application. */ class HomeFragment : Fragment() { - // TODO: Rename and change types of parameters - private var param1: String? = null - private var param2: String? = null - private var listener: OnFragmentInteractionListener? = null + // TODO: remove this test object. it is currently just used to exercise the rust code var converter: JniConverter = JniConverter() - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - arguments?.let { - param1 = it.getString(ARG_PARAM1) - param2 = it.getString(ARG_PARAM2) - } - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -57,10 +37,13 @@ class HomeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - (activity as MainActivity).setSupportActionBar(toolbar) - (activity as MainActivity).setupNavigation() - (activity as MainActivity).supportActionBar?.setTitle(R.string.destination_title_home) + (activity as MainActivity).let { mainActivity -> + mainActivity.setSupportActionBar(toolbar) + mainActivity.setupNavigation() + mainActivity.supportActionBar?.setTitle(R.string.destination_title_home) + } + // TODO remove this test code val seed = byteArrayOf(0x77, 0x78, 0x79) val result = converter.getAddress(seed) text_wallet_message.text = "Your address:\n$result" @@ -68,100 +51,72 @@ class HomeFragment : Fragment() { override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - initFab() + initFab(activity!!) } - private fun initFab() { - val theme = activity?.theme + /** + * Initialize the Fab button and all its action items + * + * @param activity a helper parameter that forces this method to be called after the activity is created and not null + */ + private fun initFab(activity: Activity) { val speedDial = sd_fab - speedDial.addActionItem( - SpeedDialActionItem.Builder(R.id.fab_request, R.drawable.ic_receipt_24dp) - .setFabBackgroundColor(ResourcesCompat.getColor(resources, R.color.icon_request, theme)) - .setFabImageTintColor(ResourcesCompat.getColor(resources, R.color.zcashWhite, theme)) - .setLabel(getString(R.string.destination_menu_label_request)) - .setLabelClickable(true) - .create() - ) - speedDial.addActionItem( - SpeedDialActionItem.Builder(R.id.fab_receive, R.drawable.ic_qrcode_24dp) - .setFabBackgroundColor(ResourcesCompat.getColor(resources, R.color.icon_receive, theme)) - .setFabImageTintColor(ResourcesCompat.getColor(resources, R.color.zcashWhite, theme)) - .setLabel(getString(R.string.destination_menu_label_receive)) - .setLabelClickable(true) - .create() - ) - speedDial.addActionItem( - SpeedDialActionItem.Builder(R.id.fab_send, R.drawable.ic_menu_send) - .setFabBackgroundColor(ResourcesCompat.getColor(resources, R.color.icon_send, theme)) - .setFabImageTintColor(ResourcesCompat.getColor(resources, R.color.zcashWhite, theme)) - .setLabel(getString(R.string.destination_menu_label_send)) - .setLabelClickable(true) - .create() - ) - val nav = (activity as MainActivity).navController + + HomeFab.values().forEach { + speedDial.addActionItem(it.createItem(activity)) + } + speedDial.setOnActionSelectedListener { item -> - when (item.id) { - R.id.fab_send -> { - nav.navigate(R.id.nav_send_fragment) - } - R.id.fab_receive -> { - nav.navigate(R.id.nav_receive_fragment) - } - R.id.fab_request -> { - nav.navigate(R.id.nav_request_fragment) - } - else -> { - // TODO: do we need an else - } - } + HomeFab.fromId(item.id)?.destination?.apply { nav.navigate(this) } false } } - // TODO: Rename method, update argument and hook method into UI event - fun onButtonPressed(uri: Uri) { - listener?.onFragmentInteraction(uri) - } - - override fun onDetach() { - super.onDetach() - listener = null - } - /** - * This interface must be implemented by activities that contain this - * fragment to allow an interaction in this fragment to be communicated - * to the activity and potentially other fragments contained in that - * activity. - * - * - * See the Android Training lesson [Communicating with Other Fragments] - * (http://developer.android.com/training/basics/fragments/communicating.html) - * for more information. + * Defines the basic properties of each FAB button for use while initializing the FAB */ - interface OnFragmentInteractionListener { - // TODO: Update argument type and name - fun onFragmentInteraction(uri: Uri) + enum class HomeFab( + @IdRes val id:Int, + @DrawableRes val icon:Int, + @ColorRes val bgColor:Int, + @StringRes val label:Int, + @IdRes val destination:Int + ) { + /* ordered by when they need to be added to the speed dial (i.e. reverse display order) */ + REQUEST( + R.id.fab_request, + R.drawable.ic_receipt_24dp, + R.color.icon_request, + R.string.destination_menu_label_request, + R.id.nav_request_fragment + ), + RECEIVE( + R.id.fab_receive, + R.drawable.ic_qrcode_24dp, + R.color.icon_receive, + R.string.destination_menu_label_receive, + R.id.nav_receive_fragment + ), + SEND( + R.id.fab_send, + R.drawable.ic_menu_send, + R.color.icon_send, + R.string.destination_menu_label_send, + R.id.nav_send_fragment + ); + + fun createItem(activity: Activity): SpeedDialActionItem = + SpeedDialActionItem.Builder(id, icon) + .setFabBackgroundColor(ResourcesCompat.getColor(activity.resources, bgColor, activity.theme)) + .setFabImageTintColor(ResourcesCompat.getColor(activity.resources, R.color.zcashWhite, activity.theme)) + .setLabel(activity.getString(label)) + .setLabelClickable(true) + .create() + + companion object { + fun fromId(id: Int): HomeFab? = values().firstOrNull { it.id == id } + } } - companion object { - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment HomeFragment. - */ - // TODO: Rename and change types and number of parameters - @JvmStatic - fun newInstance(param1: String, param2: String) = - HomeFragment().apply { - arguments = Bundle().apply { - putString(ARG_PARAM1, param1) - putString(ARG_PARAM2, param2) - } - } - } }