Skip to content

Commit

Permalink
Merge pull request #12936 from woocommerce/issue/add-package-ui-impro…
Browse files Browse the repository at this point in the history
…vements

[Shipping Labels Revamp] Add Improvements and bug fixes to Packages UI
  • Loading branch information
ThomazFB authored Nov 15, 2024
2 parents 66014e5 + 5bdaf8b commit 1f22542
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ data class CustomPackageCreationData(
val isValid: Boolean
get() = height.isNotEmpty() && length.isNotEmpty() && width.isNotEmpty()

val asPackageData: PackageData
get() = PackageData(
type = type,
name = "",
description = "",
length = length,
width = width,
height = height,
isSelected = true
)

companion object {
val EMPTY = CustomPackageCreationData(
type = PackageType.BOX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.woocommerce.android.R
import com.woocommerce.android.extensions.handleDialogResult
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.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageSelected
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageType
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -25,4 +33,54 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() {
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindEventListener()
bindResultHandlers()
}

private fun bindEventListener() {
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
is ShowPackageTypeDialog -> handlePackageTypeSelection(event.currentSelection)
is PackageSelected -> handlePackageDataAsResult(event.packageData)
}
}
}

private fun bindResultHandlers() {
handleDialogResult<String>(
key = SELECTOR_REQUEST_KEY,
entryId = R.id.wooShippingLabelPackageCreationFragment
) { result ->
PackageType.entries
.firstOrNull { it.toString() == result }
?.let { viewModel.onPackageTypeSelected(it) }
}
}

private fun handlePackageTypeSelection(currentSelection: PackageType) {
WooShippingLabelPackageCreationFragmentDirections
.actionWooShippingLabelPackageCreationFragmentToItemSelectorDialog(
requestKey = SELECTOR_REQUEST_KEY,
selectedItem = currentSelection.name,
values = PackageType.entries.map { it.name }.toTypedArray(),
keys = PackageType.entries
.map { getString(it.resourceId) }
.toTypedArray()
).let { findNavController().navigateSafely(it) }
}

private fun handlePackageDataAsResult(packageData: PackageData) {
setFragmentResult(
PACKAGE_SELECTION_RESULT,
Bundle().apply { putParcelable(PACKAGE_SELECTION_RESULT, packageData) }
)
}

companion object {
const val SELECTOR_REQUEST_KEY = "package_type"
const val PACKAGE_SELECTION_RESULT = "package_selection"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
viewState.savedPackageSelection.packages
.map { it.copy(isSelected = false) }
.toMutableList()
.apply { set(indexOf(selectedPackage), selectedPackage.copy(isSelected = isSelected)) }
.safelyUpdate(selectedPackage, selectedPackage.copy(isSelected = isSelected))
.let { SavedPackageSelection(it) }
.let { viewState.copy(savedPackageSelection = it) }
}
Expand All @@ -77,19 +77,21 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
fun onAddCarrierPackageClick() {
_viewState.value.carrierPackageSection.carrierPackages
.asSequence()
.map { it.value }.flatten()
.map { it.packages }.flatten()
.flatMap { it.value }
.flatMap { it.packages }
.find { it.isSelected }
?.let { triggerEvent(CarrierPackageSelected(it)) }
?.let { triggerEvent(PackageSelected(it)) }
}

fun onAddSavedPackageClick() {
_viewState.value.savedPackageSelection.packages.find { it.isSelected }
?.let { triggerEvent(SavedPackageSelected(it)) }
?.let { triggerEvent(PackageSelected(it)) }
}

fun onAddCustomPackageClick() {
triggerEvent(CustomPackageCreated(_viewState.value.customPackageCreationData))
_viewState.value.customPackageCreationData
.asPackageData
.let { triggerEvent(PackageSelected(it)) }
}

fun onPackageTypeSpinnerClick() {
Expand Down Expand Up @@ -139,15 +141,19 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
packageGroup.packages
.map { it.copy(isSelected = false) }
.toMutableList()
.apply {
indexOf(packageData)
.takeIf { it != -1 }
?.let { set(it, packageData.copy(isSelected = isSelected)) }
?: this
}
.safelyUpdate(packageData, packageData.copy(isSelected = isSelected))
.let { packageGroup.copy(packages = it) }
}.let { carrierPackages.key to it }

private fun MutableList<PackageData>.safelyUpdate(
originalPackage: PackageData,
updatedPackage: PackageData
) = apply {
indexOf(originalPackage)
.takeIf { it != -1 }
?.let { set(it, updatedPackage) }
}

@Parcelize
data class ViewState(
val pageTabs: List<PageTab> = emptyList(),
Expand All @@ -173,8 +179,6 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
ENVELOPE(R.string.woo_shipping_labels_package_creation_envelope_type)
}

data class SavedPackageSelected(val packageData: PackageData) : MultiLiveEvent.Event()
data class CarrierPackageSelected(val packageData: PackageData) : MultiLiveEvent.Event()
data class CustomPackageCreated(val packageData: CustomPackageCreationData) : MultiLiveEvent.Event()
data class PackageSelected(val packageData: PackageData) : MultiLiveEvent.Event()
data class ShowPackageTypeDialog(val currentSelection: PackageType) : MultiLiveEvent.Event()
}
8 changes: 7 additions & 1 deletion WooCommerce/src/main/res/navigation/nav_graph_orders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,13 @@
<fragment
android:id="@+id/wooShippingLabelPackageCreationFragment"
android:name="com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationFragment"
android:label="WooShippingLabelPackageCreationFragment" />
android:label="WooShippingLabelPackageCreationFragment" >

<action
android:id="@+id/action_wooShippingLabelPackageCreationFragment_to_itemSelectorDialog"
app:destination="@id/itemSelectorDialog" />

</fragment>
<fragment
android:id="@+id/wooShippingLabelPurchasedFragment"
android:name="com.woocommerce.android.ui.orders.wooshippinglabels.purchased.WooShippingLabelPurchasedFragment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.woocommerce.android.ui.orders.wooshippinglabels.packages

import androidx.lifecycle.SavedStateHandle
import com.woocommerce.android.R
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.CustomPackageCreated
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageSelected
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageType
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ViewState
Expand Down Expand Up @@ -68,7 +68,7 @@ class WooShippingLabelPackageCreationViewModelTest : BaseUnitTest() {

sut.onAddCustomPackageClick()

assertThat(lastEvent).isEqualTo(CustomPackageCreated(customPackageData))
assertThat(lastEvent).isEqualTo(PackageSelected(customPackageData.asPackageData))
}

@Test
Expand Down

0 comments on commit 1f22542

Please sign in to comment.