Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Image Uploads Improvement] Add entry points to the error screen #13389

Merged
merged 18 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d4b5280
Add button to view recent upload failure on product images screen
hafizrahman Jan 24, 2025
4a23dea
Observe current upload errors and hide/show button based on it.
hafizrahman Jan 24, 2025
3883253
Simplify observe logic
hafizrahman Jan 24, 2025
36f54f9
Make button navigate to upload screen
hafizrahman Jan 24, 2025
bbd18da
Add button to go to upload error screen when there's error on product…
hafizrahman Jan 24, 2025
1690c8b
Do not clear all image errors when entering error upload image screen
JorgeMucientes Jan 30, 2025
8d687a1
Merge branch 'trunk' into issue/13370-pt-ii-entry-points-to-error-screen
JorgeMucientes Jan 30, 2025
383ded3
Add function to clear upload error from list, upon retrying
JorgeMucientes Jan 30, 2025
1d3acea
Remove unnecessary comment
JorgeMucientes Jan 30, 2025
5beda83
Replace CTA from upload error snackbar
JorgeMucientes Jan 30, 2025
ca15009
Set red text to highlight is error screen
JorgeMucientes Jan 30, 2025
b246e4a
Update upload error snackbar from product detail screen as well
JorgeMucientes Jan 30, 2025
12dc0e8
Fix detekt identation issues
JorgeMucientes Jan 30, 2025
f617f90
Fix and add unit tests
JorgeMucientes Jan 30, 2025
1557aa5
Clear image upload errors when exiting product details discarding cha…
JorgeMucientes Jan 30, 2025
d7c0eef
Update button placement on product details.
hafizrahman Jan 31, 2025
7776bc0
Extract clearing upload error to view model
JorgeMucientes Jan 31, 2025
49a1560
Merge branch 'issue/13370-pt-ii-entry-points-to-error-screen' of http…
JorgeMucientes Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ interface UIMessageResolver {
anchorViewId?.let { setAnchorView(it) }
}

/**
* Create and return a snackbar with the provided [UiString].
*
* @param [stringResId] The string resource id of the base message
* @param [stringArgs] Optional. One or more format argument stringArgs
*/
fun getUiStringSnack(message: UiString) = when (message) {
is UiString.UiStringRes ->
Snackbar.make(snackbarRoot, message.stringRes, BaseTransientBottomBar.LENGTH_LONG)

is UiString.UiStringText -> Snackbar.make(snackbarRoot, message.text, BaseTransientBottomBar.LENGTH_LONG)
}.apply {
anchorViewId?.let { setAnchorView(it) }
}

/**
* Display a snackbar with the provided message.
*
Expand Down Expand Up @@ -265,6 +280,7 @@ interface UIMessageResolver {
val snackbar = when (message) {
is UiString.UiStringRes ->
Snackbar.make(snackbarRoot, message.stringRes, BaseTransientBottomBar.LENGTH_LONG)

is UiString.UiStringText -> Snackbar.make(snackbarRoot, message.text, BaseTransientBottomBar.LENGTH_LONG)
}.apply {
anchorViewId?.let { setAnchorView(it) }
Expand Down Expand Up @@ -296,6 +312,7 @@ interface UIMessageResolver {
snackbar.show()
}
}

private fun getIndefiniteSnackbarWithAction(
view: View,
msg: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ class MediaFileUploadHandler @Inject constructor(
is Event.MediaUploadEvent.FetchSucceeded -> {
enqueueMediaUpload(event)
}

is Event.MediaUploadEvent.FetchFailed -> {
statusList[index] = newStatus
showUploadFailureNotifIfNoObserver(event.productId, statusList)
}

is Event.MediaUploadEvent.UploadSucceeded -> {
if (externalObservers.contains(event.productId)) {
WooLog.d(WooLog.T.MEDIA, "MediaFileUploadHandler -> Upload successful, while handler is observed")
Expand All @@ -94,6 +96,7 @@ class MediaFileUploadHandler @Inject constructor(
statusList[index] = newStatus
}
}

is Event.MediaUploadEvent.UploadFailed -> {
WooLog.e(WooLog.T.MEDIA, "MediaFileUploadHandler -> Upload failed", event.error)
statusList[index] = newStatus
Expand Down Expand Up @@ -125,6 +128,7 @@ class MediaFileUploadHandler @Inject constructor(
when (event) {
is Event.ProductUpdateEvent.ProductUpdateFailed ->
notificationHandler.postUpdateFailureNotification(event.productId, event.product)

is Event.ProductUpdateEvent.ProductUpdateSucceeded ->
notificationHandler.postUpdateSuccessNotification(event.productId, event.product, event.imagesCount)
}
Expand Down Expand Up @@ -196,6 +200,16 @@ class MediaFileUploadHandler @Inject constructor(
notificationHandler.removeUploadFailureNotification(remoteProductId)
}

fun clearImageErrors(remoteProductId: Long, uris: List<String>) {
uploadsStatus.update { list ->
list.filterNot {
it.remoteProductId == remoteProductId &&
it.uploadStatus is UploadStatus.Failed &&
uris.contains(it.localUri)
}
}
}

fun observeCurrentUploadErrors(remoteProductId: Long): Flow<List<ProductImageUploadData>> =
uploadsStatus.map { list ->
list.filter { it.remoteProductId == remoteProductId && it.uploadStatus is UploadStatus.Failed }
Expand Down Expand Up @@ -267,12 +281,14 @@ class MediaFileUploadHandler @Inject constructor(
mediaErrorMessage = resourceProvider.getString(R.string.product_image_service_error_media_null),
mediaErrorType = MediaStore.MediaErrorType.NULL_MEDIA_ARG
)

is Event.MediaUploadEvent.FetchSucceeded -> UploadStatus.InProgress
is Event.MediaUploadEvent.UploadFailed -> UploadStatus.Failed(
media = error.media,
mediaErrorMessage = error.errorMessage,
mediaErrorType = error.errorType
)

is Event.MediaUploadEvent.UploadSucceeded -> UploadStatus.UploadSuccess(media = media)
}
return ProductImageUploadData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ class MediaUploadErrorListViewModel @Inject constructor(
}
mediaFileUploadHandler.observeCurrentUploadErrors(navArgs.remoteProductId)
.filter { it.isNotEmpty() }
.onEach { errors ->
.onEach { newErrors ->
val currentErrors = _viewState.value.uploadErrorList +
errors.map { ErrorUiModel(it.uploadStatus as UploadStatus.Failed, it.localUri) }
newErrors
.map { ErrorUiModel(it.uploadStatus as UploadStatus.Failed, it.localUri) }
.filter { _viewState.value.uploadErrorList.contains(it).not() } // Filter duplicates
_viewState.update {
_viewState.value.copy(
uploadErrorList = currentErrors,
Expand All @@ -58,8 +60,6 @@ class MediaUploadErrorListViewModel @Inject constructor(
)
)
}
// Remove errors from mediaFileUploadHandler to avoid duplicate notifications
mediaFileUploadHandler.clearImageErrors(navArgs.remoteProductId)
}
.launchIn(this)
}
Expand All @@ -70,6 +70,7 @@ class MediaUploadErrorListViewModel @Inject constructor(

fun onRetryUploadClicked(error: ErrorUiModel) {
mediaFileUploadHandler.enqueueUpload(navArgs.remoteProductId, listOf(error.localUri))
mediaFileUploadHandler.clearImageErrors(navArgs.remoteProductId, listOf(error.localUri))
_viewState.update {
_viewState.value.copy(
uploadErrorList = _viewState.value.uploadErrorList - error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.woocommerce.android.extensions.takeIfNotEqualTo
import com.woocommerce.android.extensions.windowSizeClass
import com.woocommerce.android.model.Product
import com.woocommerce.android.model.Product.Image
import com.woocommerce.android.model.UiString
import com.woocommerce.android.ui.aztec.AztecEditorFragment
import com.woocommerce.android.ui.aztec.AztecEditorFragment.Companion.ARG_AZTEC_EDITOR_TEXT
import com.woocommerce.android.ui.aztec.AztecEditorFragment.Companion.ARG_AZTEC_TITLE_FROM_AI_DESCRIPTION
Expand Down Expand Up @@ -83,9 +84,10 @@ import com.woocommerce.android.ui.products.variations.VariationListViewModel.Var
import com.woocommerce.android.ui.promobanner.PromoBanner
import com.woocommerce.android.ui.promobanner.PromoBannerType
import com.woocommerce.android.util.ChromeCustomTabUtils
import com.woocommerce.android.util.UiHelpers.getTextOfUiString
import com.woocommerce.android.util.WooAnimUtils
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.LaunchUrlInChromeTab
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowActionSnackbar
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowUiStringSnackbar
import com.woocommerce.android.widgets.CustomProgressDialog
import com.woocommerce.android.widgets.SkeletonView
import com.woocommerce.android.widgets.WCProductImageGalleryView.OnGalleryImageInteractionListener
Expand Down Expand Up @@ -183,9 +185,11 @@ class ProductDetailFragment :
ProductsCommunicationViewModel.CommunicationEvent.ProductSelected(mode.remoteProductId)
)
}

is Mode.Loading, is Mode.Empty -> {
findNavController().popBackStack()
}

is Mode.AddNewProduct -> {
}
}
Expand Down Expand Up @@ -223,6 +227,10 @@ class ProductDetailFragment :
}
binding.cardsRecyclerView.layoutManager = layoutManager
binding.cardsRecyclerView.itemAnimator = null

binding.openUploadScreenButton.setOnClickListener {
viewModel.openUploadScreen()
}
}

private fun initializeViewModel() {
Expand Down Expand Up @@ -361,6 +369,9 @@ class ProductDetailFragment :
hideProgressDialog()
}
}
new.hasUploadErrors?.takeIfNotEqualTo(old?.hasUploadErrors) { hasErrors ->
binding.openUploadScreenButton.visibility = if (hasErrors) View.VISIBLE else View.GONE
}
}

viewModel.productDetailCards.observe(viewLifecycleOwner) {
Expand Down Expand Up @@ -392,11 +403,7 @@ class ProductDetailFragment :
)
}

is ShowActionSnackbar -> displayProductImageUploadErrorSnackBar(
event.message,
event.actionText,
event.action
)
is ShowUiStringSnackbar -> displayProductImageUploadErrorSnackBar(event.message)

is HideImageUploadErrorSnackbar -> imageUploadErrorsSnackbar?.dismiss()
is ShowLinkedProductPromoBanner -> showLinkedProductPromoBanner()
Expand All @@ -417,6 +424,7 @@ class ProductDetailFragment :
is ProductUpdated -> productsCommunicationViewModel.pushEvent(
ProductsCommunicationViewModel.CommunicationEvent.ProductUpdated
)

is ProductDetailViewModel.ShowUpdateProductError -> showUpdateProductError(event.message)
else -> event.isHandled = false
}
Expand Down Expand Up @@ -521,19 +529,11 @@ class ProductDetailFragment :
}
}

private fun displayProductImageUploadErrorSnackBar(
message: String,
actionText: String,
actionListener: View.OnClickListener
) {
private fun displayProductImageUploadErrorSnackBar(uiString: UiString) {
if (imageUploadErrorsSnackbar == null) {
imageUploadErrorsSnackbar = uiMessageResolver.getIndefiniteActionSnack(
message = message,
actionText = actionText,
actionListener = actionListener
)
imageUploadErrorsSnackbar = uiMessageResolver.getUiStringSnack(message = uiString)
} else {
imageUploadErrorsSnackbar?.setText(message)
imageUploadErrorsSnackbar?.setText(getTextOfUiString(requireContext(), uiString))
}
imageUploadErrorsSnackbar?.show()
}
Expand All @@ -555,6 +555,7 @@ class ProductDetailFragment :
Loading, None -> {
binding.productErrorStateContainer.isVisible = false
}

is Error -> {
binding.productErrorStateContainer.isVisible = true
binding.productDetailRoot.isVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.woocommerce.android.model.ProductTag
import com.woocommerce.android.model.RequestResult
import com.woocommerce.android.model.SubscriptionDetails
import com.woocommerce.android.model.SubscriptionPeriod
import com.woocommerce.android.model.UiString.UiStringText
import com.woocommerce.android.model.addTags
import com.woocommerce.android.model.sortCategories
import com.woocommerce.android.model.toAppModel
Expand Down Expand Up @@ -93,9 +94,9 @@ import com.woocommerce.android.util.WooLog
import com.woocommerce.android.viewmodel.LiveDataDelegate
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.LaunchUrlInChromeTab
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowActionSnackbar
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowDialog
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowUiStringSnackbar
import com.woocommerce.android.viewmodel.ResourceProvider
import com.woocommerce.android.viewmodel.ScopedViewModel
import com.woocommerce.android.viewmodel.getNullableStateFlow
Expand Down Expand Up @@ -930,6 +931,7 @@ class ProductDetailViewModel @Inject constructor(
val positiveAction = DialogInterface.OnClickListener { _, _ ->
// Make sure to cancel any remaining image uploads
mediaFileUploadHandler.cancelUpload(getRemoteProductId())
mediaFileUploadHandler.clearImageErrors(getRemoteProductId())
triggerEvent(ProductNavigationTarget.ExitProduct)
}

Expand Down Expand Up @@ -2134,15 +2136,16 @@ class ProductDetailViewModel @Inject constructor(
mediaFileUploadHandler.observeCurrentUploadErrors(productId)
.onEach { errorList ->
if (errorList.isEmpty()) {
viewState = viewState.copy(hasUploadErrors = false)
triggerEvent(HideImageUploadErrorSnackbar)
} else {
viewState = viewState.copy(hasUploadErrors = true)
triggerEvent(
ShowActionSnackbar(
message = resources.getMediaUploadErrorMessage(errorList.size),
actionText = resources.getString(R.string.details)
) {
triggerEvent(ProductNavigationTarget.ViewMediaUploadErrors(productId))
}
ShowUiStringSnackbar(
message = UiStringText(
resources.getMediaUploadErrorMessage(errorList.size)
),
)
)
}
}
Expand Down Expand Up @@ -2651,6 +2654,10 @@ class ProductDetailViewModel @Inject constructor(
}
}

fun openUploadScreen() {
triggerEvent(ProductNavigationTarget.ViewMediaUploadErrors(getRemoteProductId()))
}

/**
* Sealed class that handles the back navigation for the product detail screens while providing a common
* interface for managing them as a single type. Currently used in all the product sub detail screens when
Expand Down Expand Up @@ -2720,6 +2727,7 @@ class ProductDetailViewModel @Inject constructor(
val productAggregateDraft: ProductAggregate? = null,
val auxiliaryState: AuxiliaryState = AuxiliaryState.None,
val uploadingImageUris: List<Uri>? = null,
val hasUploadErrors: Boolean? = null,
val isProgressDialogShown: Boolean? = null,
val showBottomSheetButton: Boolean? = null,
val isConfirmingTrash: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.woocommerce.android.extensions.navigateSafely
import com.woocommerce.android.extensions.takeIfNotEqualTo
import com.woocommerce.android.mediapicker.MediaPickerHelper
import com.woocommerce.android.model.Product
import com.woocommerce.android.model.UiString
import com.woocommerce.android.ui.products.BaseProductEditorFragment
import com.woocommerce.android.ui.products.ConfirmRemoveProductImageDialog
import com.woocommerce.android.ui.products.ProductNavigationTarget
Expand All @@ -36,14 +37,15 @@ import com.woocommerce.android.ui.products.images.ProductImagesViewModel.ShowIma
import com.woocommerce.android.ui.products.images.ProductImagesViewModel.ShowStorageChooser
import com.woocommerce.android.ui.products.images.ProductImagesViewModel.ShowWPMediaPicker
import com.woocommerce.android.util.ChromeCustomTabUtils
import com.woocommerce.android.util.UiHelpers.getTextOfUiString
import com.woocommerce.android.util.setHomeIcon
import com.woocommerce.android.util.setupTabletSecondPaneToolbar
import com.woocommerce.android.viewmodel.MultiLiveEvent
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ExitWithResult
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowActionSnackbar
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowDialog
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowUiStringSnackbar
import com.woocommerce.android.viewmodel.fixedHiltNavGraphViewModels
import com.woocommerce.android.widgets.WCProductImageGalleryView
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -154,6 +156,10 @@ class ProductImagesFragment :
}
}

binding.openUploadScreenButton.setOnClickListener {
viewModel.openUploadScreen()
}

setupTabletSecondPaneToolbar(
title = getString(R.string.product_images_title),
onMenuItemSelected = ::onMenuItemSelected,
Expand Down Expand Up @@ -194,6 +200,9 @@ class ProductImagesFragment :
}
}
}
new.hasUploadErrors?.takeIfNotEqualTo(old?.hasUploadErrors) { hasErrors ->
binding.openUploadScreenButton.visibility = if (hasErrors) View.VISIBLE else View.GONE
}
new.isDragDropDescriptionVisible?.takeIfNotEqualTo(old?.isDragDropDescriptionVisible) { isVisible ->
binding.dragAndDropDescription.isVisible = isVisible
}
Expand All @@ -203,11 +212,7 @@ class ProductImagesFragment :
when (event) {
is Exit -> findNavController().navigateUp()
is ShowSnackbar -> uiMessageResolver.showSnack(event.message)
is ShowActionSnackbar -> displayProductImageUploadErrorSnackBar(
event.message,
event.actionText,
event.action
)
is ShowUiStringSnackbar -> displayProductImageUploadErrorSnackBar(event.message)
is ProductNavigationTarget -> navigator.navigate(this, event)
is ExitWithResult<*> -> navigateBackWithResult(KEY_IMAGES_DIALOG_RESULT, event.data)
is ShowDialog -> event.showDialog()
Expand All @@ -230,19 +235,11 @@ class ProductImagesFragment :
).show()
}

private fun displayProductImageUploadErrorSnackBar(
message: String,
actionText: String,
actionListener: View.OnClickListener
) {
private fun displayProductImageUploadErrorSnackBar(uiString: UiString) {
if (imageUploadErrorsSnackbar == null) {
imageUploadErrorsSnackbar = uiMessageResolver.getIndefiniteActionSnack(
message = message,
actionText = actionText,
actionListener = actionListener
)
imageUploadErrorsSnackbar = uiMessageResolver.getUiStringSnack(message = uiString)
} else {
imageUploadErrorsSnackbar?.setText(message)
imageUploadErrorsSnackbar?.setText(getTextOfUiString(requireContext(), uiString))
}
imageUploadErrorsSnackbar?.show()
}
Expand Down
Loading
Loading