Skip to content

Commit

Permalink
Update cover image status when editing order
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeMucientes committed Jan 17, 2025
1 parent 2648b4e commit 2dd833c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ class ProductImagesViewModel @Inject constructor(
}

fun onValidateButtonClicked() {
viewState = viewState.copy(productImagesState = Browsing)
viewState = viewState.copy(
images = images.updateProductCoverImageToFirstItem(),
productImagesState = Browsing
)
}

fun onNavigateBackButtonClicked() {
Expand All @@ -159,6 +162,7 @@ class ProductImagesViewModel @Inject constructor(
images = productImagesState.initialState
)
}

Browsing -> {
val hasChange = !images.areSameImagesAs(originalImages)
analyticsTracker.track(
Expand Down Expand Up @@ -228,8 +232,13 @@ class ProductImagesViewModel @Inject constructor(

fun onGalleryImageDragStarted() {
when (viewState.productImagesState) {
is Dragging -> { /* no-op*/ }
Browsing -> viewState = viewState.copy(productImagesState = Dragging(images))
is Dragging -> { /* no-op*/
}

Browsing -> viewState = viewState.copy(
images = images.uncheckProductCoverImage(),
productImagesState = Dragging(images)
)
}
}

Expand All @@ -249,6 +258,16 @@ class ProductImagesViewModel @Inject constructor(
}
}

private fun List<Product.Image>.updateProductCoverImageToFirstItem() =
this.mapIndexed { index, image ->
when {
index == 0 -> image.copy(isCoverImage = true)
else -> image.copy(isCoverImage = false)
}
}

private fun List<Product.Image>.uncheckProductCoverImage() = this.map { it.copy(isCoverImage = false) }

@Parcelize
data class ViewState(
val showSourceChooser: Boolean? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class WCProductImageGalleryView @JvmOverloads constructor(
imageList.addAll(images)

// restore the "Add image" icon (never shown when list is empty)
if (showAddImageIcon && imageList.size > 0) {
if (showAddImageIcon && imageList.isNotEmpty()) {
imageList.add(
Product.Image(
id = ADD_IMAGE_ITEM_ID,
Expand Down Expand Up @@ -299,7 +299,7 @@ class WCProductImageGalleryView @JvmOverloads constructor(
}

for (index in images.indices) {
if (images[index].id != actualImages[index].id) {
if (images[index] != actualImages[index]) {
return false
}
}
Expand Down Expand Up @@ -451,7 +451,7 @@ class WCProductImageGalleryView @JvmOverloads constructor(
listener.onGalleryImageDeleteIconClicked(image)
}
viewBinding.coverTag.visibility = when {
image.isCoverImage && !viewBinding.deleteImageButton.isVisible -> View.VISIBLE
image.isCoverImage -> View.VISIBLE
else -> View.GONE
}
}
Expand Down

0 comments on commit 2dd833c

Please sign in to comment.