From 8e1f4946ffdcb6315f7498507a527fd594463c69 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Tue, 12 Nov 2024 17:55:07 -0300 Subject: [PATCH 01/15] Adjust carrier package selection finder logic --- .../packages/WooShippingLabelPackageCreationViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt index 12666f57607..ea78117cd69 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt @@ -77,8 +77,8 @@ 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)) } } From 790354103124f9678fd062ce622943a64b571e18 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Tue, 12 Nov 2024 17:56:59 -0300 Subject: [PATCH 02/15] Add event handling scaffold to WooShippingLabelPackageCreationFragment --- .../WooShippingLabelPackageCreationFragment.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index 25676ccff35..0db6b247c36 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -25,4 +25,17 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { } } } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + bindEventListener() + } + + private fun bindEventListener() { + viewModel.event.observe(viewLifecycleOwner) { event -> + when (event) { + + } + } + } } From 811cd3cebbe532449671090d86a4d243094ea9cf Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Tue, 12 Nov 2024 17:59:37 -0300 Subject: [PATCH 03/15] Switch between all ViewModel events --- ...WooShippingLabelPackageCreationFragment.kt | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index 0db6b247c36..d54f03e0f9e 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -9,6 +9,10 @@ import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnViewTreeLif import androidx.fragment.app.viewModels import com.woocommerce.android.ui.base.BaseFragment import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.CarrierPackageSelected +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.CustomPackageCreated +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.SavedPackageSelected +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -34,8 +38,27 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { private fun bindEventListener() { viewModel.event.observe(viewLifecycleOwner) { event -> when (event) { - + is ShowPackageTypeDialog -> handlePackageTypeSelection() + is CarrierPackageSelected -> handleCarrierPackageSelection() + is CustomPackageCreated -> handleCustomPackageCreation() + is SavedPackageSelected -> handleSavedPackageSelection() } } } + + private fun handlePackageTypeSelection() { + + } + + private fun handleCarrierPackageSelection() { + + } + + private fun handleCustomPackageCreation() { + + } + + private fun handleSavedPackageSelection() { + + } } From c53a35bd236866e7cadef9d23c848ed444446a7a Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Wed, 13 Nov 2024 21:05:32 -0300 Subject: [PATCH 04/15] Add route from Package creation fragment to item selector dialog --- WooCommerce/src/main/res/navigation/nav_graph_orders.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/res/navigation/nav_graph_orders.xml b/WooCommerce/src/main/res/navigation/nav_graph_orders.xml index 222ce166634..ab8fa8d16f0 100644 --- a/WooCommerce/src/main/res/navigation/nav_graph_orders.xml +++ b/WooCommerce/src/main/res/navigation/nav_graph_orders.xml @@ -760,7 +760,13 @@ + android:label="WooShippingLabelPackageCreationFragment" > + + + + Date: Fri, 15 Nov 2024 10:47:08 -0300 Subject: [PATCH 05/15] Trigger navigation to Item Selector Dialog --- .../WooShippingLabelPackageCreationFragment.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index d54f03e0f9e..e14d17312ec 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -7,10 +7,13 @@ import android.view.ViewGroup import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed import androidx.fragment.app.viewModels +import androidx.navigation.fragment.findNavController +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.CarrierPackageSelected import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.CustomPackageCreated +import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageType import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.SavedPackageSelected import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog import dagger.hilt.android.AndroidEntryPoint @@ -47,7 +50,15 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { } private fun handlePackageTypeSelection() { - + WooShippingLabelPackageCreationFragmentDirections + .actionWooShippingLabelPackageCreationFragmentToItemSelectorDialog( + selectedItem = "", + keys = PackageType.entries.map { it.name }.toTypedArray(), + values = PackageType.entries + .map { getString(it.resourceId) } + .toTypedArray(), + requestKey = "package_type" + ).let { findNavController().navigateSafely(it) } } private fun handleCarrierPackageSelection() { From ad1d298e812d1bc7fe20214a9411e41ad30a7332 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 10:49:51 -0300 Subject: [PATCH 06/15] Map current package selection to Selector Dialog --- .../packages/WooShippingLabelPackageCreationFragment.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index e14d17312ec..8bd5771a1a5 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -41,7 +41,7 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { private fun bindEventListener() { viewModel.event.observe(viewLifecycleOwner) { event -> when (event) { - is ShowPackageTypeDialog -> handlePackageTypeSelection() + is ShowPackageTypeDialog -> handlePackageTypeSelection(event.currentSelection) is CarrierPackageSelected -> handleCarrierPackageSelection() is CustomPackageCreated -> handleCustomPackageCreation() is SavedPackageSelected -> handleSavedPackageSelection() @@ -49,10 +49,10 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { } } - private fun handlePackageTypeSelection() { + private fun handlePackageTypeSelection(currentSelection: PackageType) { WooShippingLabelPackageCreationFragmentDirections .actionWooShippingLabelPackageCreationFragmentToItemSelectorDialog( - selectedItem = "", + selectedItem = currentSelection.name, keys = PackageType.entries.map { it.name }.toTypedArray(), values = PackageType.entries .map { getString(it.resourceId) } From 238104f7879f8f78eeb862f3a2eb7a8c50dd8874 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 10:50:09 -0300 Subject: [PATCH 07/15] Add proper Request Key to Package Type selector --- .../packages/WooShippingLabelPackageCreationFragment.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index 8bd5771a1a5..2fa9c6414dd 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -57,7 +57,7 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { values = PackageType.entries .map { getString(it.resourceId) } .toTypedArray(), - requestKey = "package_type" + requestKey = SELECTOR_REQUEST_KEY ).let { findNavController().navigateSafely(it) } } @@ -72,4 +72,8 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { private fun handleSavedPackageSelection() { } + + companion object { + const val SELECTOR_REQUEST_KEY = "package_type" + } } From 10e23afee1dd8e732bdda4078d7418d72318a5d0 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 11:24:48 -0300 Subject: [PATCH 08/15] Handle Package Type result from Item Selector Dialog --- .../WooShippingLabelPackageCreationFragment.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index 2fa9c6414dd..e1794886e34 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -8,6 +8,8 @@ import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed 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 @@ -36,6 +38,7 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) bindEventListener() + bindResultHandlers() } private fun bindEventListener() { @@ -49,15 +52,24 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { } } + private fun bindResultHandlers() { + handleDialogResult( + key = SELECTOR_REQUEST_KEY, + entryId = R.id.wooShippingLabelPackageCreationFragment + ) { result -> + viewModel.onPackageTypeSelected(PackageType.valueOf(result)) + } + } + private fun handlePackageTypeSelection(currentSelection: PackageType) { WooShippingLabelPackageCreationFragmentDirections .actionWooShippingLabelPackageCreationFragmentToItemSelectorDialog( + requestKey = SELECTOR_REQUEST_KEY, selectedItem = currentSelection.name, keys = PackageType.entries.map { it.name }.toTypedArray(), values = PackageType.entries .map { getString(it.resourceId) } - .toTypedArray(), - requestKey = SELECTOR_REQUEST_KEY + .toTypedArray() ).let { findNavController().navigateSafely(it) } } From 6aa64e4fb35bf3e24c53417684ba78a3b1bb46be Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:21:24 -0300 Subject: [PATCH 09/15] Finish the package type selection logic --- .../packages/WooShippingLabelPackageCreationFragment.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index e1794886e34..f6709416a03 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -57,7 +57,9 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { key = SELECTOR_REQUEST_KEY, entryId = R.id.wooShippingLabelPackageCreationFragment ) { result -> - viewModel.onPackageTypeSelected(PackageType.valueOf(result)) + PackageType.entries + .firstOrNull { it.toString() == result } + ?.let { viewModel.onPackageTypeSelected(it) } } } @@ -66,8 +68,8 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { .actionWooShippingLabelPackageCreationFragmentToItemSelectorDialog( requestKey = SELECTOR_REQUEST_KEY, selectedItem = currentSelection.name, - keys = PackageType.entries.map { it.name }.toTypedArray(), - values = PackageType.entries + values = PackageType.entries.map { it.name }.toTypedArray(), + keys = PackageType.entries .map { getString(it.resourceId) } .toTypedArray() ).let { findNavController().navigateSafely(it) } From 952a4ba7a12594764238a8bdd19cef30aaed4e7b Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:26:12 -0300 Subject: [PATCH 10/15] Simplify all Package selection events into one --- ...ooShippingLabelPackageCreationViewModel.kt | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt index ea78117cd69..e382b746389 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt @@ -80,16 +80,27 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor( .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 + .let { + PackageData( + type = it.type, + name = "", + description = "", + length = it.length, + width = it.width, + height = it.height, + isSelected = true + ) + }.let { triggerEvent(PackageSelected(it)) } } fun onPackageTypeSpinnerClick() { @@ -173,8 +184,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() } From df6b89def4b33c1fd4c6dbb3ad93e78de01aadf0 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:27:27 -0300 Subject: [PATCH 11/15] Introduce asPackageData to CustomPackageCreationData --- .../ui/orders/wooshippinglabels/packages/Models.kt | 11 +++++++++++ .../WooShippingLabelPackageCreationViewModel.kt | 13 ++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/Models.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/Models.kt index d1af561760d..0e6336662b6 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/Models.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/Models.kt @@ -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, diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt index e382b746389..3235392925d 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt @@ -90,17 +90,8 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor( fun onAddCustomPackageClick() { _viewState.value.customPackageCreationData - .let { - PackageData( - type = it.type, - name = "", - description = "", - length = it.length, - width = it.width, - height = it.height, - isSelected = true - ) - }.let { triggerEvent(PackageSelected(it)) } + .asPackageData + .let { triggerEvent(PackageSelected(it)) } } fun onPackageTypeSpinnerClick() { From 39d25b212996f0c51caeb13ce0a05a247a245af1 Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:31:22 -0300 Subject: [PATCH 12/15] Update WooShippingLabelPackageCreationViewModelTest with new event structure --- .../packages/WooShippingLabelPackageCreationViewModelTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModelTest.kt index decadc2a6c4..8784cef1117 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModelTest.kt @@ -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 @@ -68,7 +68,7 @@ class WooShippingLabelPackageCreationViewModelTest : BaseUnitTest() { sut.onAddCustomPackageClick() - assertThat(lastEvent).isEqualTo(CustomPackageCreated(customPackageData)) + assertThat(lastEvent).isEqualTo(PackageSelected(customPackageData.asPackageData)) } @Test From 239844e3f94f5826082eab213353eaef44064d3b Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:31:42 -0300 Subject: [PATCH 13/15] Update WooShippingLabelPackageCreationFragment with new event structure and add result handling code --- ...WooShippingLabelPackageCreationFragment.kt | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index f6709416a03..f54deaf8e10 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -6,6 +6,7 @@ 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 @@ -13,10 +14,8 @@ 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.CarrierPackageSelected -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.SavedPackageSelected import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog import dagger.hilt.android.AndroidEntryPoint @@ -45,9 +44,7 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { viewModel.event.observe(viewLifecycleOwner) { event -> when (event) { is ShowPackageTypeDialog -> handlePackageTypeSelection(event.currentSelection) - is CarrierPackageSelected -> handleCarrierPackageSelection() - is CustomPackageCreated -> handleCustomPackageCreation() - is SavedPackageSelected -> handleSavedPackageSelection() + is PackageSelected -> handlePackageDataAsResult(event.packageData) } } } @@ -75,19 +72,14 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { ).let { findNavController().navigateSafely(it) } } - private fun handleCarrierPackageSelection() { - - } - - private fun handleCustomPackageCreation() { - - } - - private fun handleSavedPackageSelection() { - + 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" } } From c721fe7d8c4ed73e92cbe11a62d2a288e310207b Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 12:38:24 -0300 Subject: [PATCH 14/15] Introduce more resilient package selection update logic --- ...WooShippingLabelPackageCreationViewModel.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt index 3235392925d..5b90808d7e3 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationViewModel.kt @@ -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) } } @@ -141,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.safelyUpdate( + originalPackage: PackageData, + updatedPackage: PackageData + ) = apply { + indexOf(originalPackage) + .takeIf { it != -1 } + ?.let { set(it, updatedPackage) } + } + @Parcelize data class ViewState( val pageTabs: List = emptyList(), From 5bdaf8b6c2f38c25828415b3453851aaa10a0b0a Mon Sep 17 00:00:00 2001 From: ThomazFB Date: Fri, 15 Nov 2024 13:07:48 -0300 Subject: [PATCH 15/15] Fix lint issues --- .../packages/WooShippingLabelPackageCreationFragment.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt index f54deaf8e10..8f6391eab0c 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/packages/WooShippingLabelPackageCreationFragment.kt @@ -73,9 +73,10 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() { } private fun handlePackageDataAsResult(packageData: PackageData) { - setFragmentResult(PACKAGE_SELECTION_RESULT, Bundle().apply { - putParcelable(PACKAGE_SELECTION_RESULT, packageData) - }) + setFragmentResult( + PACKAGE_SELECTION_RESULT, + Bundle().apply { putParcelable(PACKAGE_SELECTION_RESULT, packageData) } + ) } companion object {