Skip to content

Commit

Permalink
Merge pull request #13584 from woocommerce/issue/13289-fix-multiple-n…
Browse files Browse the repository at this point in the history
…avigation-issues

Fix crashes related to calling `onBackPressed` directly
  • Loading branch information
JorgeMucientes authored Feb 19, 2025
2 parents df52bd2 + e70d165 commit 33418b2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 52 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [*] Fixed a rare crash on the site picker screen [https://github.com/woocommerce/woocommerce-android/pull/13536]
- [*] Updated bottom sheet in Scan to update inventory to Material 3 component [https://github.com/woocommerce/woocommerce-android/pull/13554]
- [*] Improved the Authenticated WebView implementation, and it will now handle opening the Analytics reports directly from the app without requiring additional authentication [https://github.com/woocommerce/woocommerce-android/pull/13487]
- [*] Fixed a crash that occurred when the app was backgrounded during bulk variations update [https://github.com/woocommerce/woocommerce-android/pull/13584]

21.7
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.woocommerce.android.ui.dialog.WooDialog
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.ui.main.MainActivity.Companion.BackPressListener
import com.woocommerce.android.ui.products.details.ProductDetailViewModel
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowDialog
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import com.woocommerce.android.viewmodel.fixedHiltNavGraphViewModels
Expand Down Expand Up @@ -46,7 +45,6 @@ abstract class BaseProductFragment : BaseFragment, BackPressListener {
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
is ShowSnackbar -> uiMessageResolver.showSnack(event.message)
is Exit -> requireActivity().onBackPressedDispatcher.onBackPressed()
is ShowDialog -> WooDialog.showDialog(
requireActivity(),
event.positiveBtnAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class AddProductCategoryFragment :
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
is ShowSnackbar -> uiMessageResolver.showSnack(event.message)
is Exit -> requireActivity().onBackPressedDispatcher.onBackPressed()
is Exit -> findNavController().navigateUp()
is ShowDialog -> event.showDialog()
is ExitWithResult<*> -> navigateBackWithResult(ARG_CATEGORY_UPDATE_RESULT, event.data)
else -> event.isHandled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ class VariationDetailFragment :
onMenuItemSelected = ::onMenuItemSelected,
onCreateMenu = { toolbar ->
toolbar.setNavigationOnClickListener {
if (onRequestAllowBackPress()) {
findNavController().navigateUp()
}
viewModel.onExit()
}
onCreateMenu(toolbar)
}
Expand Down Expand Up @@ -320,12 +318,7 @@ class VariationDetailFragment :
is ShowDialog -> event.showDialog()
is ShowDialogFragment -> event.showIn(parentFragmentManager, this)
is VariationDetailViewModel.ShowUpdateVariationError -> showUpdateVariationError(event.message)
is Exit -> {
// Ensure subsequent Exit events are ignored to avoid IllegalStateException
viewModel.event.removeObservers(viewLifecycleOwner)
requireActivity().onBackPressedDispatcher.onBackPressed()
}

is Exit -> findNavController().navigateUp()
else -> event.isHandled = false
}
}
Expand Down Expand Up @@ -437,11 +430,7 @@ class VariationDetailFragment :
}

override fun onRequestAllowBackPress(): Boolean {
return if (viewModel.event.value == Exit) {
true
} else {
viewModel.onExit()
false
}
viewModel.onExit()
return false
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.woocommerce.android.ui.products.variations

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.core.view.MenuProvider
import androidx.navigation.fragment.findNavController
import com.woocommerce.android.R
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.base.UIMessageResolver
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.util.setupTabletSecondPaneToolbar
import com.woocommerce.android.viewmodel.MultiLiveEvent
import com.woocommerce.android.widgets.CustomProgressDialog
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -23,7 +23,11 @@ import javax.inject.Inject
*/
@AndroidEntryPoint
abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : BaseFragment(layoutId) {
@Inject lateinit var uiMessageResolver: UIMessageResolver
@Inject
lateinit var uiMessageResolver: UIMessageResolver

override val activityAppBarStatus: AppBarStatus
get() = AppBarStatus.Hidden

private var doneMenuItem: MenuItem? = null
private var progressDialog: CustomProgressDialog? = null
Expand All @@ -35,31 +39,16 @@ abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : Base

@CallSuper
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setupMenu()
observeEvents()
}

private fun setupMenu() {
requireActivity().addMenuProvider(
object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu_variations_bulk_update, menu)
doneMenuItem = menu.findItem(R.id.done)
}

override fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.done -> {
viewModel.onDoneClicked()
true
}

else -> false
}
}
},
viewLifecycleOwner
setupTabletSecondPaneToolbar(
title = getFragmentTitle(),
onMenuItemSelected = ::onMenuItemSelected,
onCreateMenu = { toolbar ->
toolbar.inflateMenu(R.menu.menu_variations_bulk_update)
doneMenuItem = toolbar.menu.findItem(R.id.done)
}
)

observeEvents()
}

private fun observeEvents() {
Expand All @@ -70,12 +59,19 @@ abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : Base
uiMessageResolver.showSnack(event.message)
}

is MultiLiveEvent.Event.Exit -> {
// Ensure subsequent Exit events are ignored to avoid IllegalStateException
viewModel.event.removeObservers(viewLifecycleOwner)
requireActivity().onBackPressedDispatcher.onBackPressed()
}
is MultiLiveEvent.Event.Exit -> findNavController().navigateUp()
}
}
}

private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.done -> {
viewModel.onDoneClicked()
true
}

else -> false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class VariationsBulkUpdatePriceFragment :
private fun observeViewStateChanges() {
viewModel.viewStateData.observe(viewLifecycleOwner) { old, new ->
new.priceType.takeIfNotEqualTo(old?.priceType) {
requireActivity().title = when (new.priceType) {
binding.toolbar.title = when (new.priceType) {
Regular -> getString(R.string.variations_bulk_update_regular_price)
Sale -> getString(R.string.variations_bulk_update_sale_price)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.Woo.Toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:elevation="@dimen/minor_50"
tools:title="@string/app_name" />

<com.google.android.material.card.MaterialCardView
style="@style/Woo.Card"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.Woo.Toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:elevation="@dimen/minor_50"
tools:title="@string/app_name" />

<com.google.android.material.card.MaterialCardView
style="@style/Woo.Card"
android:layout_width="match_parent"
Expand Down

0 comments on commit 33418b2

Please sign in to comment.