Skip to content

Commit e70e1fe

Browse files
authored
Merge branch 'issue/WOOMOB-282-fix-split-shipment-move-action' into fix/add-shipping-labels-remove-shipment-sheet
2 parents 8c9d485 + 7a8147d commit e70e1fe

File tree

8 files changed

+65
-96
lines changed

8 files changed

+65
-96
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/ShippableItemsMapper.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ fun List<ShippableItemModel>.toSelectableUIModel(
8585

8686
return SelectableShippableItemsUI(
8787
shippableItems = shippableItemsUI,
88-
totalItemQuantity = sumByFloat { it.quantity }.toInt(),
8988
formattedTotalWeight = formattedTotalWeight,
9089
formattedTotalPrice = formattedTotalPrice
9190
)

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/WooShippingLabelCreationScreen.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.woocommerce.android.ui.orders.wooshippinglabels
22

33
import android.content.res.Configuration
4-
import android.os.Parcelable
54
import androidx.compose.foundation.background
65
import androidx.compose.foundation.border
76
import androidx.compose.foundation.clickable
@@ -89,7 +88,6 @@ import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageDa
8988
import com.woocommerce.android.ui.orders.wooshippinglabels.rates.ui.ShippingRateUI
9089
import com.woocommerce.android.ui.orders.wooshippinglabels.rates.ui.ShippingRatesSection
9190
import com.woocommerce.android.ui.orders.wooshippinglabels.rates.ui.ShippingSortOption
92-
import kotlinx.parcelize.Parcelize
9391

9492
@Composable
9593
fun WooShippingLabelCreationScreen(viewModel: WooShippingLabelCreationViewModel) {
@@ -792,25 +790,6 @@ internal fun ErrorScreen(
792790
ErrorMessageWithButton(modifier = modifier.padding(padding), onRetryClick = onRetryClick)
793791
}
794792

795-
@Parcelize
796-
data class ShippableItemUI(
797-
val itemId: Long,
798-
val productId: Long,
799-
val title: String,
800-
val formattedSize: String,
801-
val formattedWeight: String,
802-
val formattedPrice: String,
803-
val quantity: Float,
804-
val imageUrl: String? = null
805-
) : Parcelable
806-
807-
@Parcelize
808-
data class ShippableItemsUI(
809-
val shippableItems: List<ShippableItemUI>,
810-
val formattedTotalWeight: String,
811-
val formattedTotalPrice: String
812-
) : Parcelable
813-
814793
@Preview(name = "dark", uiMode = Configuration.UI_MODE_NIGHT_YES, device = Devices.PIXEL)
815794
@Preview(name = "light", uiMode = Configuration.UI_MODE_NIGHT_NO, device = Devices.PIXEL)
816795
@Composable

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/WooShippingLabelCreationViewModel.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,4 +857,24 @@ data class WooShippingAddresses(
857857
}
858858
}
859859

860-
fun ShippableItemsUI.totalItemQuantity() = shippableItems.sumByFloat { it.quantity }.toInt()
860+
@Parcelize
861+
data class ShippableItemUI(
862+
val itemId: Long,
863+
val productId: Long,
864+
val title: String,
865+
val formattedSize: String,
866+
val formattedWeight: String,
867+
val formattedPrice: String,
868+
val quantity: Float,
869+
val imageUrl: String? = null
870+
) : Parcelable
871+
872+
@Parcelize
873+
data class ShippableItemsUI(
874+
val shippableItems: List<ShippableItemUI>,
875+
val formattedTotalWeight: String,
876+
val formattedTotalPrice: String
877+
) : Parcelable {
878+
val totalItemQuantity
879+
get() = shippableItems.sumByFloat { it.quantity }.toInt()
880+
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/WooShippingProductsCard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private fun ShippingProductsCardHeader(
138138
) {
139139
Row(verticalAlignment = Alignment.CenterVertically) {
140140
ProductsSummary(
141-
totalItems = shippableItems.totalItemQuantity(),
141+
totalItems = shippableItems.totalItemQuantity,
142142
totalWeight = shippableItems.formattedTotalWeight,
143143
totalPrice = shippableItems.formattedTotalPrice,
144144
modifier = Modifier

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/split/WooShippingSplitShipmentScreen.kt

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fun WooShippingSplitShipmentScreen(
9292
viewState: SplitShipmentViewState,
9393
onBack: () -> Unit,
9494
onDismissInstructions: () -> Unit,
95-
onUpdateSelection: (shipmentKey: Int, index: Int, selectedIndexes: Set<Int>?) -> Unit,
95+
onUpdateSelection: (index: Int, selectedIndexes: Set<Int>?) -> Unit,
9696
onUpdateShipment: (splitMovement: SplitMovement) -> Unit,
9797
onRemoveShipment: (removingShipmentKey: Int, movingToShipmentKey: Int) -> Unit,
9898
onUpdateSelectedShipment: (shipmentKey: Int) -> Unit,
@@ -175,7 +175,6 @@ fun WooShippingSplitShipmentScreen(
175175
)
176176
} else {
177177
SelectableProductsSection(
178-
shipmentKey = viewState.selectableItems.keys.first(),
179178
shipment = viewState.selectableItems.values.first(),
180179
onUpdateSelection = onUpdateSelection,
181180
extraBottomPadding = productsExtraPadding,
@@ -228,7 +227,7 @@ private fun MultipleShipments(
228227
shipments: List<Int>,
229228
productsExtraPadding: Dp,
230229
onUpdateSelectedShipment: (shipmentKey: Int) -> Unit,
231-
onUpdateSelection: (shipmentKey: Int, index: Int, selectedIndexes: Set<Int>?) -> Unit,
230+
onUpdateSelection: (index: Int, selectedIndexes: Set<Int>?) -> Unit,
232231
onRemoveShipmentMenuTapped: (shipmentKey: Int) -> Unit,
233232
modifier: Modifier
234233
) {
@@ -310,7 +309,6 @@ private fun MultipleShipments(
310309
) { page ->
311310
viewState.selectableItems.getValue(shipments[page]).let {
312311
SelectableProductsSection(
313-
shipmentKey = shipments[page],
314312
shipment = it,
315313
onUpdateSelection = onUpdateSelection,
316314
modifier = modifier.padding(top = 16.dp, start = 16.dp, end = 16.dp),
@@ -473,9 +471,8 @@ private fun SplitMovements(
473471

474472
@Composable
475473
fun SelectableProductsSection(
476-
shipmentKey: Int,
477474
shipment: SelectableShippableItemsUI,
478-
onUpdateSelection: (shipmentKey: Int, index: Int, selectedIndexes: Set<Int>?) -> Unit,
475+
onUpdateSelection: (index: Int, selectedIndexes: Set<Int>?) -> Unit,
479476
extraBottomPadding: Dp,
480477
modifier: Modifier = Modifier
481478
) {
@@ -501,13 +498,7 @@ fun SelectableProductsSection(
501498
quantity = shippableItem.shippableItem.quantity,
502499
imageUrl = shippableItem.shippableItem.imageUrl,
503500
isSelected = shippableItem.isSelected,
504-
onSelectionChange = {
505-
onUpdateSelection(
506-
shipmentKey,
507-
index,
508-
null
509-
)
510-
},
501+
onSelectionChange = { onUpdateSelection(index, null) },
511502
modifier = Modifier.padding(vertical = 8.dp)
512503
)
513504
}
@@ -522,13 +513,7 @@ fun SelectableProductsSection(
522513
quantity = shippableItem.shippableItem.quantity,
523514
imageUrl = shippableItem.shippableItem.imageUrl,
524515
isSelected = shippableItem.isSelected,
525-
onSelectionChange = {
526-
onUpdateSelection(
527-
shipmentKey,
528-
index,
529-
null
530-
)
531-
},
516+
onSelectionChange = { onUpdateSelection(index, null) },
532517
isExpanded = expanded,
533518
onExpand = { expanded = !expanded },
534519
singleWeight = shippableItem.innerShippableItem.formattedWeight,
@@ -538,11 +523,7 @@ fun SelectableProductsSection(
538523
val indexes = shippableItem.selectedIndexes.toMutableSet()
539524
if (isSelected) indexes.remove(innerIndex) else indexes.add(innerIndex)
540525

541-
onUpdateSelection(
542-
shipmentKey,
543-
index,
544-
indexes
545-
)
526+
onUpdateSelection(index, indexes)
546527
},
547528
modifier = Modifier.padding(vertical = 8.dp)
548529
)
@@ -567,21 +548,19 @@ private fun WooShippingSplitShipmentScreenPreview() = WooThemeWithBackground {
567548
selectableItems = mapOf(
568549
0 to SelectableShippableItemsUI(
569550
shippableItems = emptyList(),
570-
totalItemQuantity = 1,
571551
formattedTotalWeight = "",
572552
formattedTotalPrice = ""
573553
),
574554
1 to SelectableShippableItemsUI(
575555
shippableItems = emptyList(),
576-
totalItemQuantity = 1,
577556
formattedTotalWeight = "",
578557
formattedTotalPrice = ""
579558
)
580559
)
581560
),
582561
onBack = {},
583562
onDismissInstructions = {},
584-
onUpdateSelection = { _, _, _ -> },
563+
onUpdateSelection = { _, _ -> },
585564
onUpdateShipment = {},
586565
onRemoveShipment = { _, _ -> },
587566
onUpdateSelectedShipment = {},

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/split/WooShippingSplitShipmentViewModel.kt

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.woocommerce.android.ui.orders.wooshippinglabels.split
33
import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.asLiveData
55
import com.woocommerce.android.R
6+
import com.woocommerce.android.extensions.sumByFloat
67
import com.woocommerce.android.ui.orders.wooshippinglabels.ShippableItemUI
78
import com.woocommerce.android.ui.orders.wooshippinglabels.components.ShippingLabelsSnackbarData
89
import com.woocommerce.android.ui.orders.wooshippinglabels.models.ShippableItemModel
@@ -104,13 +105,9 @@ class WooShippingSplitShipmentViewModel @Inject constructor(
104105
removeShipmentSheet.value = null
105106
}
106107

107-
fun onUpdateSelection(
108-
shipmentKey: Int,
109-
shippableItemIndex: Int,
110-
selectedIndexes: Set<Int>? = null
111-
) {
112-
val shipmentsMap = shipmentsUIMap.value?.toMutableMap() ?: return
113-
val items = shipmentsMap.getValue(shipmentKey)
108+
fun onUpdateSelection(shippableItemIndex: Int, selectedIndexes: Set<Int>? = null) {
109+
val shipmentsMap = selectableItems.value?.toMutableMap() ?: return
110+
val items = shipmentsMap.getValue(shipmentSelected.value)
114111
val item = items.shippableItems[shippableItemIndex]
115112
val updatedItem = when (item) {
116113
is SelectableShippableItemUI.SingleSelectableShippableItemUI -> {
@@ -128,8 +125,8 @@ class WooShippingSplitShipmentViewModel @Inject constructor(
128125
}
129126
val updatedList = items.shippableItems.toMutableList()
130127
updatedList[shippableItemIndex] = updatedItem
131-
shipmentsMap[shipmentKey] = items.copy(shippableItems = updatedList)
132-
shipmentsUIMap.value = shipmentsMap
128+
shipmentsMap[shipmentSelected.value] = items.copy(shippableItems = updatedList)
129+
selectableItems.value = shipmentsMap
133130
}
134131

135132
fun onUpdateShipment(splitMovement: SplitMovement) {
@@ -193,23 +190,27 @@ class WooShippingSplitShipmentViewModel @Inject constructor(
193190

194191
data class SelectableShippableItemsUI(
195192
val shippableItems: List<SelectableShippableItemUI>,
196-
val totalItemQuantity: Int,
197193
val formattedTotalWeight: String,
198194
val formattedTotalPrice: String
199-
)
195+
) {
196+
val totalItemQuantity: Int
197+
get() = shippableItems.sumByFloat { it.shippableItem.quantity }.toInt()
198+
}
199+
200+
sealed interface SelectableShippableItemUI {
201+
val shippableItem: ShippableItemUI
200202

201-
sealed class SelectableShippableItemUI {
202203
data class SingleSelectableShippableItemUI(
203-
val shippableItem: ShippableItemUI,
204+
override val shippableItem: ShippableItemUI,
204205
val isSelected: Boolean = false
205-
) : SelectableShippableItemUI()
206+
) : SelectableShippableItemUI
206207

207208
data class ExpandableSelectableShippableItemUI(
208-
val shippableItem: ShippableItemUI,
209+
override val shippableItem: ShippableItemUI,
209210
val innerShippableItem: ShippableItemUI,
210211
val isExpanded: Boolean = false,
211212
val selectedIndexes: Set<Int> = emptySet(),
212-
) : SelectableShippableItemUI() {
213+
) : SelectableShippableItemUI {
213214
val isSelected: Boolean
214215
get() = selectedIndexes.size == shippableItem.quantity.toInt()
215216
}
@@ -226,15 +227,6 @@ data class RemoveShipmentSheet(
226227
val otherShipments: Map<Int, SelectableShippableItemsUI>
227228
)
228229

229-
fun List<ShippableItemModel>.combine(other: List<ShippableItemModel>): List<ShippableItemModel> {
230-
val combinedMap = this.associateBy { it.productId }.toMutableMap()
231-
other.forEach { otherItem ->
232-
val existingItem = combinedMap[otherItem.productId]
233-
if (existingItem != null) {
234-
combinedMap[otherItem.productId] = existingItem.copy(quantity = existingItem.quantity + otherItem.quantity)
235-
} else {
236-
combinedMap[otherItem.productId] = otherItem
237-
}
238-
}
239-
return combinedMap.values.toList()
240-
}
230+
private fun List<ShippableItemModel>.combine(other: List<ShippableItemModel>) = (this + other)
231+
.groupBy { it.itemId }
232+
.map { (_, itemsWithSameId) -> itemsWithSameId.first().copy(quantity = itemsWithSameId.sumByFloat { it.quantity }) }

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/WooShippingLabelCreationViewModelTest.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,21 +1102,22 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
11021102
}
11031103

11041104
@Test
1105-
fun `when StartHazmatFormEdit is triggered with a selected category, the event contains the expected category value`() = testBlocking {
1106-
var event: MultiLiveEvent.Event? = null
1107-
whenever(orderDetailRepository.getOrderById(any())) doReturn null
1108-
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
1109-
whenever(observeStoreOptions()) doReturn flowOf(defaultStoreOptions)
1105+
fun `when StartHazmatFormEdit is triggered with a selected category, the event contains the expected category value`() =
1106+
testBlocking {
1107+
var event: MultiLiveEvent.Event? = null
1108+
whenever(orderDetailRepository.getOrderById(any())) doReturn null
1109+
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
1110+
whenever(observeStoreOptions()) doReturn flowOf(defaultStoreOptions)
11101111

1111-
createViewModel()
1112+
createViewModel()
11121113

1113-
sut.onHazmatCategorySelected(ShippingLabelHazmatCategory.CLASS_1)
1114-
sut.onHazmatNoticeClick()
1114+
sut.onHazmatCategorySelected(ShippingLabelHazmatCategory.CLASS_1)
1115+
sut.onHazmatNoticeClick()
11151116

1116-
sut.event.observeForever { event = it }
1117+
sut.event.observeForever { event = it }
11171118

1118-
assertThat(event).isEqualTo(StartHazmatFormEdit(ShippingLabelHazmatCategory.CLASS_1))
1119-
}
1119+
assertThat(event).isEqualTo(StartHazmatFormEdit(ShippingLabelHazmatCategory.CLASS_1))
1120+
}
11201121

11211122
@Test
11221123
fun `when initialized, show expected item quantity`() = testBlocking {
@@ -1136,6 +1137,6 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
11361137
val currentViewState = sut.viewState.value
11371138
assert(currentViewState is DataState)
11381139
val dataState = currentViewState as DataState
1139-
assertThat(dataState.shippableItems.totalItemQuantity()).isEqualTo(expectedItemQuantity)
1140+
assertThat(dataState.shippableItems.totalItemQuantity).isEqualTo(expectedItemQuantity)
11401141
}
11411142
}

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/wooshippinglabels/split/WooShippingSplitShipmentViewModelTest.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import kotlin.test.Test
1717

1818
@OptIn(ExperimentalCoroutinesApi::class)
1919
class WooShippingSplitShipmentViewModelTest : BaseUnitTest() {
20-
2120
private val currencyFormatter: CurrencyFormatter = org.mockito.kotlin.mock {
2221
on { formatCurrency(amount = any(), any(), any()) }.doAnswer { it.getArgument<BigDecimal>(0).toString() }
2322
}
@@ -87,7 +86,7 @@ class WooShippingSplitShipmentViewModelTest : BaseUnitTest() {
8786

8887
createViewModel(shipmentArgs)
8988

90-
sut.onUpdateSelection(1, shippableItemIndex, List(3) { it }.toSet())
89+
sut.onUpdateSelection(shippableItemIndex, List(3) { it }.toSet())
9190

9291
sut.viewState.observeForTesting { }
9392

@@ -112,7 +111,7 @@ class WooShippingSplitShipmentViewModelTest : BaseUnitTest() {
112111

113112
createViewModel(shipmentArgs)
114113

115-
sut.onUpdateSelection(1, shippableItemIndex, null)
114+
sut.onUpdateSelection(shippableItemIndex, null)
116115

117116
sut.viewState.observeForTesting { }
118117

@@ -137,7 +136,7 @@ class WooShippingSplitShipmentViewModelTest : BaseUnitTest() {
137136

138137
createViewModel(shipmentArgs)
139138

140-
sut.onUpdateSelection(1, shippableItemIndex, null)
139+
sut.onUpdateSelection(shippableItemIndex, null)
141140

142141
sut.viewState.observeForTesting { }
143142

0 commit comments

Comments
 (0)