Skip to content

Commit

Permalink
Merge pull request #13236 from woocommerce/issue/save-package-loading…
Browse files Browse the repository at this point in the history
…-feedback

[Shipping Labels Revamp] Add Package template loading and recovery feedback
  • Loading branch information
atorresveiga authored Jan 4, 2025
2 parents 34be219 + 99625db commit e96624b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ import com.woocommerce.android.extensions.navigateBackWithResult
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.dialog.WooDialog
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.ShowLoadingDialog
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowPackageTypeDialog
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.ShowTemplateCreationErrorDialog
import com.woocommerce.android.widgets.CustomProgressDialog
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WooShippingLabelPackageCreationFragment : BaseFragment() {
val viewModel: WooShippingLabelPackageCreationViewModel by viewModels()

private var progressDialog: CustomProgressDialog? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(DisposeOnViewTreeLifecycleDestroyed)
Expand All @@ -44,6 +50,8 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() {
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
is ShowPackageTypeDialog -> handlePackageTypeSelection(event.currentSelection)
is ShowLoadingDialog -> showLoadingDialog(event.show)
is ShowTemplateCreationErrorDialog -> handleTemplateCreationError()
is PackageSelected -> navigateBackWithResult(PACKAGE_SELECTION_RESULT, event.packageData)
}
}
Expand Down Expand Up @@ -72,6 +80,31 @@ class WooShippingLabelPackageCreationFragment : BaseFragment() {
).let { findNavController().navigateSafely(it) }
}

private fun showLoadingDialog(show: Boolean) {
if (show) {
progressDialog = CustomProgressDialog.show(
title = getString(R.string.loading),
message = getString(R.string.please_wait)
).also { it.show(parentFragmentManager, CustomProgressDialog.TAG) }
} else {
progressDialog?.dismiss()
progressDialog = null
}
}

private fun handleTemplateCreationError() {
WooDialog.showDialog(
activity = requireActivity(),
titleId = R.string.woo_shipping_labels_package_creation_error_title,
messageId = R.string.woo_shipping_labels_package_creation_error_message,
positiveButtonId = R.string.woo_shipping_labels_package_creation_error_proceed,
neutralButtonId = R.string.woo_shipping_labels_package_creation_error_cancel,
posBtnAction = { _, _ ->
viewModel.onAddCustomPackageClick(savePackageAsTemplate = false)
}
)
}

companion object {
const val SELECTOR_REQUEST_KEY = "package_type"
const val PACKAGE_SELECTION_RESULT = "package_selection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(

fun onAddCustomPackageClick(savePackageAsTemplate: Boolean) {
val customPackage = _viewState.value.customPackageCreationData
selectedSite.getOrNull()
?.takeIf { savePackageAsTemplate }
?.let { customPackage.submitToStore(it) }

customPackage.toPackageData(dimensionUnit = storeOptions.dimensionUnit)
.let { triggerEvent(PackageSelected(it)) }
if (savePackageAsTemplate) {
handleCustomSelectionAsTemplate(customPackage)
} else {
triggerEvent(PackageSelected(customPackage.toPackageData(dimensionUnit = storeOptions.dimensionUnit)))
}
}

fun onPackageTypeSpinnerClick() {
Expand Down Expand Up @@ -194,24 +193,55 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(
?.let { set(it, updatedPackage) }
}

private fun CustomPackageCreationData.submitToStore(site: SiteModel) {
private fun handleCustomSelectionAsTemplate(
customPackage: CustomPackageCreationData
) {
triggerEvent(ShowLoadingDialog(true))
launch {
packageRepository.createCustomPackage(
site = site,
requestData = this@submitToStore.let {
CustomPackageCreationRequestData(
name = it.name,
isLetter = it.type == PackageType.ENVELOPE,
innerDimensions = it.dimensions,
boxWeight = it.weight?.toDoubleOrNull() ?: 0.0,
isUserDefined = true,
maxWeight = 0.0
)
}.let { listOf(it) }
selectedSite.getOrNull()
?.let { sendCustomPackageToStore(it, customPackage) }
?.fold(
onSuccess = {
triggerEvent(ShowLoadingDialog(false))
triggerEvent(
PackageSelected(customPackage.toPackageData(dimensionUnit = storeOptions.dimensionUnit))
)
},
onFailure = {
triggerEvent(ShowLoadingDialog(false))
triggerEvent(ShowTemplateCreationErrorDialog)
}
) ?: triggerEvent(
PackageSelected(
customPackage.toPackageData(dimensionUnit = storeOptions.dimensionUnit)
)
)
}
}

private suspend fun sendCustomPackageToStore(
site: SiteModel,
packageData: CustomPackageCreationData
): Result<PackageData> {
val response = packageRepository.createCustomPackage(
site = site,
requestData = CustomPackageCreationRequestData(
name = packageData.name,
isLetter = packageData.type == PackageType.ENVELOPE,
innerDimensions = packageData.dimensions,
boxWeight = packageData.weight?.toDoubleOrNull() ?: 0.0,
isUserDefined = true,
maxWeight = 0.0
).let { listOf(it) }
)

return response.takeIf { it.isError.not() }
?.model?.firstOrNull()
?.let { PackageData.fromPackageDAO(it) }
?.let { Result.success(it) }
?: Result.failure(Throwable("Failed to save package"))
}

@Parcelize
data class ViewState(
val pageTabs: List<PageTab> = emptyList(),
Expand Down Expand Up @@ -260,4 +290,6 @@ class WooShippingLabelPackageCreationViewModel @Inject constructor(

data class PackageSelected(val packageData: PackageData) : MultiLiveEvent.Event()
data class ShowPackageTypeDialog(val currentSelection: PackageType) : MultiLiveEvent.Event()
data class ShowLoadingDialog(val show: Boolean) : MultiLiveEvent.Event()
object ShowTemplateCreationErrorDialog : MultiLiveEvent.Event()
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,12 @@ class FetchPredefinedPackagesFromStore @Inject constructor(

return PredefinedPackagesState.Data(
storeOptions = storePackages.storeOptions.toStoreOptionsForPackages(),
savedPackages = storePackages.filterSavedData(),
savedPackages = storePackages.savedPackages
.map { PackageData.fromPackageDAO(it) },
carrierPackages = storePackages.filterCarrierData()
)
}

private fun StorePackagesDAO.filterSavedData() =
savedPackages.map { packageDAO ->
PackageData(
name = packageDAO.name,
dimensions = packageDAO.dimensions,
weight = packageDAO.weight,
isSelected = false,
isPredefined = true,
isLetter = packageDAO.isLetter,
dimensionUnit = packageDAO.dimensionUnit,
weightUnit = packageDAO.weightUnit,
groupName = packageDAO.groupName,
id = packageDAO.id,
)
}

private fun StorePackagesDAO.filterCarrierData() = mapOf(
carrierPackages
.parseCarrierData(CarrierType.USPS)
Expand All @@ -67,18 +52,7 @@ class FetchPredefinedPackagesFromStore @Inject constructor(
CarrierPackageGroup(
groupName = group.description,
packages = group.packages.map { packageItem ->
PackageData(
name = packageItem.name,
dimensions = packageItem.dimensions,
weight = packageItem.weight,
isSelected = false,
isPredefined = true,
isLetter = packageItem.isLetter,
dimensionUnit = packageItem.dimensionUnit,
weightUnit = packageItem.weightUnit,
groupName = packageItem.groupName,
id = packageItem.id,
)
PackageData.fromPackageDAO(packageItem)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Parcelable
import com.woocommerce.android.R
import com.woocommerce.android.extensions.isNotNullOrEmpty
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationViewModel.PackageType
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.datasource.PackageDAO
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize

Expand Down Expand Up @@ -58,6 +59,23 @@ data class PackageData(
isLetter = false,
groupName = null
)

fun fromPackageDAO(
dao: PackageDAO,
isSelected: Boolean = false,
isPredefined: Boolean = true
): PackageData = PackageData(
id = dao.id,
name = dao.name,
dimensions = dao.dimensions,
weight = dao.weight,
isSelected = isSelected,
isPredefined = isPredefined,
isLetter = dao.isLetter,
dimensionUnit = dao.dimensionUnit,
weightUnit = dao.weightUnit,
groupName = dao.groupName
)
}
}

Expand Down
4 changes: 4 additions & 0 deletions WooCommerce/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4448,5 +4448,9 @@
<string name="woo_shipping_labels_package_creation_error">Failed to load the package data</string>
<string name="woo_shipping_labels_package_creation_box_type">Box</string>
<string name="woo_shipping_labels_package_creation_envelope_type">Envelope</string>
<string name="woo_shipping_labels_package_creation_error_title">We couldn\'t save the package as template</string>
<string name="woo_shipping_labels_package_creation_error_message">Do you want to proceed without saving it?</string>
<string name="woo_shipping_labels_package_creation_error_proceed">Proceed</string>
<string name="woo_shipping_labels_package_creation_error_cancel">Cancel</string>
<string name="email_not_registered_wpcom">Hmm, we can\'t find a WordPress.com account connected to this email address.</string>
</resources>

0 comments on commit e96624b

Please sign in to comment.