From 3d6ce645b5ba5cd7a1af1b4547da3067a09dcc30 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Wed, 12 Dec 2018 21:30:27 -0600 Subject: [PATCH] Make proper configurations to enable the Navigation component and make use of it to add the navigation to the Merchants fragment and back to the Balances fragment. --- .../activities/MainActivity.kt | 45 +++++++++++++++++++ .../fragments/BalancesFragment.kt | 10 +++-- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/menu/menu_balances.xml | 17 +++++++ app/src/main/res/menu/navigation.xml | 24 ---------- .../main/res/navigation/mobile_navigation.xml | 5 ++- 6 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 app/src/main/res/menu/menu_balances.xml delete mode 100644 app/src/main/res/menu/navigation.xml diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt index 3e08d8b..52160f9 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt @@ -1,16 +1,61 @@ package cy.agorise.bitsybitshareswallet.activities +import android.content.res.Resources import android.os.Bundle +import android.view.MenuItem +import androidx.navigation.findNavController +import androidx.navigation.fragment.NavHostFragment +import androidx.navigation.ui.AppBarConfiguration +import androidx.navigation.ui.navigateUp +import androidx.navigation.ui.onNavDestinationSelected +import androidx.navigation.ui.setupActionBarWithNavController import cy.agorise.bitsybitshareswallet.R import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.models.JsonRpcResponse +import kotlinx.android.synthetic.main.activity_main.* class MainActivity : ConnectedActivity() { private val TAG = this.javaClass.simpleName + private lateinit var appBarConfiguration : AppBarConfiguration + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + setSupportActionBar(toolbar) + + val host: NavHostFragment = supportFragmentManager + .findFragmentById(R.id.navHostFragment) as NavHostFragment? ?: return + + // Set up Action Bar + val navController = host.navController + + appBarConfiguration = AppBarConfiguration(navController.graph) + + setupActionBarWithNavController(navController, appBarConfiguration) + + navController.addOnDestinationChangedListener { _, destination, _ -> + val dest: String = try { + resources.getResourceName(destination.id) + } catch (e: Resources.NotFoundException) { + Integer.toString(destination.id) + } + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + // Have the NavigationUI look for an action or destination matching the menu + // item id and navigate there if found. + // Otherwise, bubble up to the parent. + return item.onNavDestinationSelected(findNavController(R.id.navHostFragment)) + || super.onOptionsItemSelected(item) + } + + override fun onSupportNavigateUp(): Boolean { + // Allows NavigationUI to support proper up navigation or the drawer layout + // drawer menu, depending on the situation + return findNavController(R.id.navHostFragment).navigateUp(appBarConfiguration) } override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt index 3d27ddf..290fd58 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt @@ -3,10 +3,8 @@ 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 android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import androidx.lifecycle.Observer import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -33,6 +31,8 @@ class BalancesFragment : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { + setHasOptionsMenu(true) + return inflater.inflate(R.layout.fragment_balances, container, false) } @@ -71,4 +71,8 @@ class BalancesFragment : Fragment() { transfersDetailsAdapter.replaceAll(transfersDetails) }) } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.menu_balances, menu) + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9411d90..cbd06d5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,7 +16,7 @@ android:theme="@style/ThemeOverlay.AppCompat.Dark" /> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/navigation.xml b/app/src/main/res/menu/navigation.xml deleted file mode 100644 index ddf6911..0000000 --- a/app/src/main/res/menu/navigation.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - diff --git a/app/src/main/res/navigation/mobile_navigation.xml b/app/src/main/res/navigation/mobile_navigation.xml index c362807..966e57e 100644 --- a/app/src/main/res/navigation/mobile_navigation.xml +++ b/app/src/main/res/navigation/mobile_navigation.xml @@ -9,7 +9,10 @@ + \ No newline at end of file