Skip to content

Commit

Permalink
Merge pull request #13291 from woocommerce/issue/12439-improve-naviga…
Browse files Browse the repository at this point in the history
…tion-with-bottom-sheets

Improve navigation with bottom sheets
  • Loading branch information
atorresveiga authored Jan 14, 2025
2 parents 68bcda6 + 3ad85bb commit 7479676
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import com.woocommerce.android.extensions.navigateSafely
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.ui.main.MainActivity.Companion.BackPressListener
import com.woocommerce.android.ui.orders.wooshippinglabels.WooShippingLabelCreationViewModel.StartPackageSelection
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationFragment.Companion.PACKAGE_SELECTION_RESULT
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import com.woocommerce.android.viewmodel.MultiLiveEvent
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WooShippingLabelCreationFragment : BaseFragment() {
class WooShippingLabelCreationFragment : BaseFragment(), BackPressListener {
private val viewModel: WooShippingLabelCreationViewModel by viewModels()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
Expand Down Expand Up @@ -69,4 +70,6 @@ class WooShippingLabelCreationFragment : BaseFragment() {
viewModel.onPackageSelected(it)
}
}

override fun onRequestAllowBackPress(): Boolean = viewModel.onNavigateBack()
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,19 @@ class WooShippingLabelCreationViewModel @Inject constructor(
customWeight = input
}

fun onNavigateBack() {
triggerEvent(Event.Exit)
fun onNavigateBack(): Boolean {
val state = uiState.value
return when {
state.isAddressSelectionExpanded -> {
uiState.update { it.copy(isAddressSelectionExpanded = false) }
false
}
state.isShipmentDetailsExpanded -> {
uiState.update { it.copy(isShipmentDetailsExpanded = false) }
false
}
else -> true
}
}

data object StartPackageSelection : Event()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,4 +721,71 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
changeAccepted = sut.onShipmentDetailsExpandedChange(true)
assertThat(changeAccepted).isFalse()
}

@Test
fun `when a bottom sheet is expanded then the back gesture closes the sheet`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId)

whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(observeStoreOptions()) doReturn flowOf(null)

createViewModel()

advanceUntilIdle()
// Expand shipment details and select address
sut.onShipmentDetailsExpandedChange(true)
sut.onSelectAddressExpandedChange(true)

// Close address selection
var shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isFalse()

// Close shipment details
shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isFalse()

// Navigate back
shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isTrue()
}

@Test
fun `when shipment details is expanded then the back gesture closes the sheet`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId)

whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(observeStoreOptions()) doReturn flowOf(null)

createViewModel()

advanceUntilIdle()
sut.onShipmentDetailsExpandedChange(true)

// Close shipment details
var shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isFalse()

// Navigate back
shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isTrue()
}

@Test
fun `when there is no bottom sheet expanded, then on back navigates to the previous screen`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId)

whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(observeStoreOptions()) doReturn flowOf(null)

createViewModel()

advanceUntilIdle()

// Navigate back
val shouldNavigateBack = sut.onNavigateBack()
assertThat(shouldNavigateBack).isTrue()
}
}

0 comments on commit 7479676

Please sign in to comment.