Skip to content

Commit

Permalink
Merge issue/10568-nav-host-fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
samiuelson committed Feb 22, 2024
2 parents 2f6df6f + d28d798 commit 8fed59b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavOptions
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.NavHostFragment
import com.automattic.android.tracks.crashlogging.CrashLogging
Expand Down Expand Up @@ -1206,7 +1207,11 @@ class MainActivity :
remoteNoteId,
startPaymentsFlow
).toBundle()
navController.navigate(R.id.orderDetailFragment, bundle)
navController.navigate(
R.id.orderDetailFragment,
bundle,
navOptions = NavOptions.Builder().setLaunchSingleTop(true).build()
)
} ?: run {
navController.navigateSafely(action)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class OrderDetailFragment :
} else {
binding.toolbar.navigationIcon = AppCompatResources.getDrawable(requireActivity(), R.drawable.ic_back_24dp)
binding.toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
if (!findNavController().popBackStack()) requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
val menuEditOrder = menu.findItem(R.id.menu_edit_order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.MenuItem
import android.view.MenuItem.OnActionExpandListener
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
Expand All @@ -19,6 +18,7 @@ import androidx.core.view.doOnPreDraw
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.paging.PagedList
Expand Down Expand Up @@ -110,8 +110,6 @@ class OrderListFragment :
private val viewModel: OrderListViewModel by viewModels()
private var snackBar: Snackbar? = null

private var savedDestinationId: Int = -1

override fun onStop() {
snackBar?.dismiss()
super.onStop()
Expand Down Expand Up @@ -155,7 +153,6 @@ class OrderListFragment :
savedInstanceState?.let { bundle ->
isSearching = bundle.getBoolean(STATE_KEY_IS_SEARCHING)
searchQuery = bundle.getString(STATE_KEY_SEARCH_QUERY, "")
savedDestinationId = savedInstanceState.getInt(CURRENT_NAV_DESTINATION, -1)
}
requireActivity().onBackPressedDispatcher.addCallback(
this,
Expand All @@ -167,7 +164,16 @@ class OrderListFragment :
} else if (isSearching) {
handleSearchViewCollapse()
} else {
findNavController().navigateUp()
val result =
_binding?.detailNavContainer?.findNavController()?.navigateUp() ?: false
if (!result && _binding?.orderRefreshLayout?.isVisible != true && !isTablet()) {
// There are no more fragments in the back stack, UI used to be a two pane layout (tablet)
// and now it's a single pane layout (phone), e.g. due to a configuration change.
// In this case we need to switch panes – show the list pane instead of details pane.
adjustUiForDeviceType(savedInstanceState)
} else {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
}
Expand Down Expand Up @@ -218,7 +224,7 @@ class OrderListFragment :

initObservers()
initializeResultHandlers()
displayTwoPaneLayoutIfTablet(savedInstanceState)
adjustUiForDeviceType(savedInstanceState)
binding.orderFiltersCard.setClickListener { viewModel.onFiltersButtonTapped() }
initCreateOrderFAB(binding.createOrderButton)
initSwipeBehaviour()
Expand Down Expand Up @@ -274,11 +280,11 @@ class OrderListFragment :
return true // Return true to collapse the action view
}

private fun displayTwoPaneLayoutIfTablet(savedInstanceState: Bundle?) {
private fun adjustUiForDeviceType(savedInstanceState: Bundle?) {
if (isTablet()) {
adjustLayoutForTablet()
} else {
adjustLayoutForNonTablet()
adjustLayoutForNonTablet(savedInstanceState)
savedInstanceState?.putInt(CURRENT_NAV_DESTINATION, -1)
}
}
Expand All @@ -292,36 +298,30 @@ class OrderListFragment :
} else {
binding.twoPaneLayoutGuideline.setGuidelinePercent(TABLET_LANDSCAPE_WIDTH_RATIO)
}
binding.orderRefreshLayout.visibility = View.VISIBLE
binding.detailNavContainer.visibility = View.VISIBLE
}

private fun adjustLayoutForNonTablet() {
if (savedDestinationId != -1) {
adjustLayoutForSinglePane()
private fun adjustLayoutForNonTablet(savedInstanceState: Bundle?) {
if (savedInstanceState != null && savedInstanceState.getInt(CURRENT_NAV_DESTINATION, -1) != -1) {
displayDetailPaneOnly()
} else {
_binding?.detailNavContainer?.visibility = View.GONE
_binding?.orderRefreshLayout?.visibility = View.VISIBLE
_binding?.twoPaneLayoutGuideline?.setGuidelinePercent(1f)
displayListPaneOnly()
}
}

private fun adjustLayoutForSinglePane() {
// Adjust the detail container to occupy the full width in single-pane mode (e.g., phone)
private fun displayListPaneOnly() {
_binding?.detailNavContainer?.visibility = View.GONE
_binding?.orderRefreshLayout?.visibility = View.VISIBLE
_binding?.twoPaneLayoutGuideline?.setGuidelinePercent(1f)
}

private fun displayDetailPaneOnly() {
_binding?.detailNavContainer?.visibility = View.VISIBLE
_binding?.twoPaneLayoutGuideline?.setGuidelinePercent(0.0f)

// Adjust the order list view to be hidden in single-pane mode
_binding?.orderRefreshLayout?.visibility = View.GONE
}

private fun hideDetailPane(
detailContainer: NavHostFragment,
orderListViewLayoutParams: LinearLayout.LayoutParams
) {
detailContainer.view?.visibility = View.GONE
orderListViewLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
orderListViewLayoutParams.weight = 0f
}

private fun initSwipeBehaviour() {
val swipeToComplete = SwipeToComplete(requireContext(), this)
val swipeHelper = ItemTouchHelper(swipeToComplete)
Expand Down Expand Up @@ -569,12 +569,6 @@ class OrderListFragment :
emptyView.show(emptyViewType) {
ChromeCustomTabUtils.launchUrl(requireActivity(), AppUrls.URL_LEARN_MORE_ORDERS)
}
val detailContainer = childFragmentManager.findFragmentById(
R.id.detail_nav_container
) as NavHostFragment
val orderListViewLayoutParams = binding.orderRefreshLayout.layoutParams
as LinearLayout.LayoutParams
hideDetailPane(detailContainer, orderListViewLayoutParams)
}

EmptyViewType.ORDER_LIST_FILTERED -> {
Expand Down

0 comments on commit 8fed59b

Please sign in to comment.