|
| 1 | +package com.smarttoolfactory.propertyfindar |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.view.View |
| 5 | +import androidx.core.os.bundleOf |
| 6 | +import androidx.fragment.app.activityViewModels |
| 7 | +import androidx.navigation.dynamicfeatures.fragment.DynamicNavHostFragment |
| 8 | +import androidx.navigation.fragment.findNavController |
| 9 | +import com.google.android.material.bottomnavigation.BottomNavigationView |
| 10 | +import com.smarttoolfactory.core.ui.fragment.DynamicNavigationFragment |
| 11 | +import com.smarttoolfactory.core.ui.fragment.navhost.BaseDynamicNavHostFragment |
| 12 | +import com.smarttoolfactory.core.util.observe |
| 13 | +import com.smarttoolfactory.core.util.setupWithNavController |
| 14 | +import com.smarttoolfactory.core.viewmodel.PropertyDetailNavigationVM |
| 15 | +import com.smarttoolfactory.propertyfindar.databinding.FragmentMainBottomNavBinding |
| 16 | + |
| 17 | +/** |
| 18 | + * Alternative of MainFragment with only [BottomNavigationView] |
| 19 | + * that has [DynamicNavHostFragment]s as root fragment of each |
| 20 | + * tab with [BaseDynamicNavHostFragment]s that extend [DynamicNavHostFragment]. |
| 21 | + * |
| 22 | + * |
| 23 | + */ |
| 24 | +class MainFragmentBottomNav : DynamicNavigationFragment<FragmentMainBottomNavBinding>() { |
| 25 | + |
| 26 | + /** |
| 27 | + * ViewModel for navigating to property detail screen from Main Fragment |
| 28 | + */ |
| 29 | + private val propertyDetailNavigationVM by activityViewModels<PropertyDetailNavigationVM>() |
| 30 | + |
| 31 | + override fun getLayoutRes(): Int = R.layout.fragment_main_bottom_nav |
| 32 | + |
| 33 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 34 | + super.onViewCreated(view, savedInstanceState) |
| 35 | + |
| 36 | + if (savedInstanceState == null) { |
| 37 | + setupBottomNavigationBar() |
| 38 | + } // Else, need to wait for onRestoreInstanceState |
| 39 | + |
| 40 | + subscribePropertyDetailNavigation() |
| 41 | + } |
| 42 | + |
| 43 | + override fun onViewStateRestored(savedInstanceState: Bundle?) { |
| 44 | + super.onViewStateRestored(savedInstanceState) |
| 45 | + // Now that BottomNavigationBar has restored its instance state |
| 46 | + // and its selectedItemId, we can proceed with setting up the |
| 47 | + // BottomNavigationBar with Navigation |
| 48 | + setupBottomNavigationBar() |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Called on first creation and when restoring state. |
| 53 | + */ |
| 54 | + private fun setupBottomNavigationBar() { |
| 55 | + |
| 56 | + val bottomNavigationView = dataBinding!!.bottomNav |
| 57 | + |
| 58 | + val navGraphIds = listOf( |
| 59 | + R.navigation.nav_graph_dfm_home_start, |
| 60 | + R.navigation.nav_graph_dfm_favorites_start, |
| 61 | + R.navigation.nav_graph_dfm_notification_start, |
| 62 | + R.navigation.nav_graph_dfm_account_start |
| 63 | + ) |
| 64 | + |
| 65 | + // Setup the bottom navigation view with a list of navigation graphs |
| 66 | + val controller = bottomNavigationView.setupWithNavController( |
| 67 | + navGraphIds = navGraphIds, |
| 68 | + fragmentManager = childFragmentManager, |
| 69 | + containerId = R.id.nav_host_container, |
| 70 | + intent = requireActivity().intent |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Navigates to Property Detail fragment from this fragment that replacing main fragment |
| 76 | + * that contains [BottomNavigationView] |
| 77 | + */ |
| 78 | + private fun subscribePropertyDetailNavigation() { |
| 79 | + |
| 80 | + viewLifecycleOwner.observe(propertyDetailNavigationVM.goToPropertyDetailFromMain) { |
| 81 | + |
| 82 | + it.getContentIfNotHandled()?.let { propertyItem -> |
| 83 | + val bundle = bundleOf("property" to propertyItem) |
| 84 | + |
| 85 | + findNavController() |
| 86 | + .navigate( |
| 87 | + R.id.action_mainFragment_to_propertyDetailFragment, |
| 88 | + bundle |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments