Skip to content

Commit

Permalink
Merge pull request #13028 from woocommerce/issue/wire-package-network…
Browse files Browse the repository at this point in the history
…ing-with-UI

[Shipping Labels Revamp] Wire package networking with UI
  • Loading branch information
ThomazFB authored Nov 29, 2024
2 parents 7f56f0a + 4d99c06 commit d44f104
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageType
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PageTab
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PageType.CARRIER
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PageType.CUSTOM
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PageType.SAVED
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.forms.WooShippingCarrierPackageScreen
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.forms.WooShippingCustomPackageCreationScreen
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.forms.WooShippingSavedPackageScreen
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.WooShippingCarrierPackageScreen
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.WooShippingCustomPackageCreationScreen
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.WooShippingSavedPackageScreen

@Composable
fun WooShippingLabelPackageCreationScreen(
Expand Down Expand Up @@ -126,31 +126,22 @@ fun WooShippingLabelsPackageCreationScreenPreview() {
WooShippingSavedPackageScreen(
savedPackages = listOf(
PackageData(
type = PackageType.ENVELOPE,
name = "Small Flat Rate Box",
description = "USPS Priority Mail Flat Rate Boxes",
length = "10",
width = "10",
height = "10",
isSelected = true
dimensions = "10 x 10 x 10 cm",
isSelected = true,
isLetter = true
),
PackageData(
type = PackageType.BOX,
name = "Small Flat Rate Box",
description = "Custom package",
length = "20",
width = "20",
height = "20",
isSelected = false
dimensions = "20 x 20 x 20 cm",
isSelected = false,
isLetter = false
),
PackageData(
type = PackageType.BOX,
name = "Small Flat Rate Box",
description = "DHL Express",
length = "30",
width = "30",
height = "30",
isSelected = false
dimensions = "30 x 30 x 30 cm",
isSelected = false,
isLetter = false
)
),
isAddPackageEnabled = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.woocommerce.android.R
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource.FetchCarrierPackagesFromStore
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource.FetchSavedPackagesFromStore
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource.FetchPredefinedPackagesFromStore
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.Carrier
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CarrierPackageGroup
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CarrierPackageSelection
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CustomPackageCreationData
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.SavedPackageSelection
import com.woocommerce.android.viewmodel.MultiLiveEvent
import com.woocommerce.android.viewmodel.ResourceProvider
import com.woocommerce.android.viewmodel.ScopedViewModel
import com.woocommerce.android.viewmodel.getStateFlow
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import javax.inject.Inject

@HiltViewModel
class WooShippingLabelPackageCreationViewModel @Inject constructor(
savedState: SavedStateHandle,
private val resourceProvider: ResourceProvider,
private val fetchSavedPackages: FetchSavedPackagesFromStore,
private val fetchCarrierPackages: FetchCarrierPackagesFromStore
private val fetchPredefinedPackages: FetchPredefinedPackagesFromStore
) : ScopedViewModel(savedState) {

private val _viewState = savedState.getStateFlow(
Expand All @@ -47,11 +52,15 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
)

init {
_viewState.update { viewState ->
viewState.copy(
savedPackageSelection = SavedPackageSelection(fetchSavedPackages()),
carrierPackageSection = CarrierPackageSelection(fetchCarrierPackages())
)
launch {
fetchPredefinedPackages()?.let {
_viewState.update { viewState ->
viewState.copy(
savedPackageSelection = it.savedPackageSelection,
carrierPackageSection = it.carrierPackageSelection
)
}
}
}
}

Expand Down Expand Up @@ -90,7 +99,7 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.woocommerce.android.R
import com.woocommerce.android.ui.compose.component.SelectionCheck
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.PackageData
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData

@Composable
fun WooSavedPackageListItem(
Expand All @@ -39,7 +40,7 @@ fun WooSavedPackageListItem(
)
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text(
text = packageData.description,
text = stringResource(id = packageData.descriptionResId),
style = MaterialTheme.typography.caption,
color = colorResource(id = R.color.color_on_surface_disabled)
)
Expand All @@ -48,7 +49,7 @@ fun WooSavedPackageListItem(
style = MaterialTheme.typography.body1
)
Text(
text = packageData.dimensionsForDisplay,
text = packageData.dimensions,
style = MaterialTheme.typography.body2
)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource

import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.Carrier
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CarrierPackageGroup
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.CarrierPackageSelection
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.SavedPackageSelection
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.StorePredefinedPackages
import javax.inject.Inject

class FetchPredefinedPackagesFromStore @Inject constructor(
private val selectedSite: SelectedSite,
private val packageRepository: WooShippingLabelPackageRepository
) {
suspend operator fun invoke(): StorePredefinedPackages? {
val storePackages = selectedSite.getOrNull()
?.let { packageRepository.fetchAllStorePackages(it) }
?.takeIf { it.isError.not() }
?.model
?: return null

return StorePredefinedPackages(
savedPackageSelection = storePackages
.filterSavedData()
.let { SavedPackageSelection(it) },
carrierPackageSelection = storePackages
.filterCarrierData()
.let { CarrierPackageSelection(it) }
)
}

private fun StorePackagesDAO.filterSavedData() =
savedPackages.map { packageDAO ->
PackageData(
name = packageDAO.name,
dimensions = packageDAO.dimensions,
isSelected = false,
isLetter = packageDAO.isLetter
)
}

private fun StorePackagesDAO.filterCarrierData() = mapOf(
carrierPackages
.parseCarrierData(CarrierType.USPS)
.let { Carrier.USPS to it },

carrierPackages
.parseCarrierData(CarrierType.DHL)
.let { Carrier.DHL to it }
)

private fun Map<CarrierType, CarrierDAO>.parseCarrierData(
carrierType: CarrierType
) = get(carrierType)?.let {
it.packageGroup.map { group ->
CarrierPackageGroup(
groupName = group.description,
packages = group.packages.map { packageItem ->
PackageData(
name = packageItem.name,
dimensions = packageItem.dimensions,
isSelected = false,
isLetter = packageItem.isLetter
)
}
)
}
} ?: emptyList()
}

This file was deleted.

Loading

0 comments on commit d44f104

Please sign in to comment.