Skip to content

Commit fab5daa

Browse files
Merge pull request #13344 from woocommerce/issue/13339-indicate-product-image-cover
Add a product "cover" tag over the cover image
2 parents 4478798 + bd0e0c3 commit fab5daa

File tree

13 files changed

+105
-29
lines changed

13 files changed

+105
-29
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/mediapicker/MediaPickerUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object MediaPickerUtil {
6363

6464
private fun handleMediaLibraryPickerResult(data: Bundle): List<Product.Image> {
6565
return data.parcelableArrayList<MediaItem.Identifier.RemoteMedia>(MediaPickerConstants.EXTRA_REMOTE_MEDIA)
66-
?.map { Product.Image(it.id, it.name, it.url, DateTimeUtils.dateFromIso8601(it.date)) }
66+
?.map { Product.Image(it.id, it.name, it.url, DateTimeUtils.dateFromIso8601(it.date), false) }
6767
?: emptyList()
6868
}
6969
}

WooCommerce/src/main/kotlin/com/woocommerce/android/model/Product.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ data class Product(
104104
val id: Long,
105105
val name: String?,
106106
val source: String,
107-
val dateCreated: Date?
107+
val dateCreated: Date?,
108+
val isCoverImage: Boolean
108109
) : Parcelable
109110

110111
fun isSameProduct(product: Product): Boolean {
@@ -539,10 +540,11 @@ fun WCProductModel.toAppModel(): Product {
539540
numVariations = this.getNumVariations(),
540541
images = this.getImageListOrEmpty().map {
541542
Product.Image(
542-
it.id,
543-
it.name,
544-
it.src,
545-
DateTimeUtils.dateFromIso8601(this.dateCreated) ?: Date()
543+
id = it.id,
544+
name = it.name,
545+
source = it.src,
546+
dateCreated = DateTimeUtils.dateFromIso8601(this.dateCreated) ?: Date(),
547+
isCoverImage = it.src == this.getFirstImageUrl()
546548
)
547549
},
548550
attributes = this.getAttributeList().map { it.toAppModel() },
@@ -597,7 +599,8 @@ fun MediaModel.toAppModel(): Product.Image {
597599
id = this.mediaId,
598600
name = this.fileName.orEmpty(),
599601
source = this.url,
600-
dateCreated = DateTimeUtils.dateFromIso8601(this.uploadDate)
602+
dateCreated = DateTimeUtils.dateFromIso8601(this.uploadDate),
603+
isCoverImage = false
601604
)
602605
}
603606

WooCommerce/src/main/kotlin/com/woocommerce/android/model/ProductVariation.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ fun WCProductVariationModel.toAppModel(): ProductVariation {
289289
globalUniqueId = this.globalUniqueId,
290290
image = this.getImageModel()?.let {
291291
Product.Image(
292-
it.id,
293-
it.name,
294-
it.src,
295-
DateTimeUtils.dateFromIso8601(this.dateCreated) ?: Date()
292+
id = it.id,
293+
name = it.name,
294+
source = it.src,
295+
dateCreated = DateTimeUtils.dateFromIso8601(this.dateCreated) ?: Date(),
296+
isCoverImage = false
296297
)
297298
},
298299
price = this.price.toBigDecimalOrNull(),

WooCommerce/src/main/kotlin/com/woocommerce/android/model/SubscriptionProductVariation.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ class SubscriptionProductVariation(
9494
globalUniqueId = model.globalUniqueId,
9595
image = model.getImageModel()?.let {
9696
Product.Image(
97-
it.id,
98-
it.name,
99-
it.src,
100-
DateTimeUtils.dateFromIso8601(model.dateCreated) ?: Date()
97+
id = it.id,
98+
name = it.name,
99+
source = it.src,
100+
dateCreated = DateTimeUtils.dateFromIso8601(model.dateCreated) ?: Date(),
101+
isCoverImage = false
101102
)
102103
},
103104
price = model.price.toBigDecimalOrNull(),

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/images/ProductImagesViewModel.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ class ProductImagesViewModel @Inject constructor(
148148
}
149149

150150
fun onValidateButtonClicked() {
151-
viewState = viewState.copy(productImagesState = Browsing)
151+
viewState = viewState.copy(
152+
images = images.updateProductCoverImageToFirstItem(),
153+
productImagesState = Browsing
154+
)
152155
}
153156

154157
fun onNavigateBackButtonClicked() {
@@ -159,6 +162,7 @@ class ProductImagesViewModel @Inject constructor(
159162
images = productImagesState.initialState
160163
)
161164
}
165+
162166
Browsing -> {
163167
val hasChange = !images.areSameImagesAs(originalImages)
164168
analyticsTracker.track(
@@ -229,7 +233,11 @@ class ProductImagesViewModel @Inject constructor(
229233
fun onGalleryImageDragStarted() {
230234
when (viewState.productImagesState) {
231235
is Dragging -> { /* no-op*/ }
232-
Browsing -> viewState = viewState.copy(productImagesState = Dragging(images))
236+
237+
Browsing -> viewState = viewState.copy(
238+
images = images.uncheckProductCoverImage(),
239+
productImagesState = Dragging(images)
240+
)
233241
}
234242
}
235243

@@ -249,6 +257,11 @@ class ProductImagesViewModel @Inject constructor(
249257
}
250258
}
251259

260+
private fun List<Product.Image>.updateProductCoverImageToFirstItem() =
261+
this.mapIndexed { index, image -> image.copy(isCoverImage = index == 0) }
262+
263+
private fun List<Product.Image>.uncheckProductCoverImage() = this.map { it.copy(isCoverImage = false) }
264+
252265
@Parcelize
253266
data class ViewState(
254267
val showSourceChooser: Boolean? = null,

WooCommerce/src/main/kotlin/com/woocommerce/android/widgets/WCProductImageGalleryView.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,16 @@ class WCProductImageGalleryView @JvmOverloads constructor(
202202
// use a negative id so we can check it in isPlaceholder() below
203203
val id = (-index - 1).toLong()
204204
// set the image src to this uri so we can preview it while uploading
205-
placeholders.add(0, Product.Image(id, "", imageUriList[index].toString(), Date()))
205+
placeholders.add(
206+
0,
207+
Product.Image(
208+
id = id,
209+
name = "",
210+
source = imageUriList[index].toString(),
211+
dateCreated = Date(),
212+
isCoverImage = false
213+
)
214+
)
206215
}
207216

208217
adapter.setPlaceholderImages(placeholders)
@@ -243,13 +252,14 @@ class WCProductImageGalleryView @JvmOverloads constructor(
243252
imageList.addAll(images)
244253

245254
// restore the "Add image" icon (never shown when list is empty)
246-
if (showAddImageIcon && imageList.size > 0) {
255+
if (showAddImageIcon && imageList.isNotEmpty()) {
247256
imageList.add(
248257
Product.Image(
249258
id = ADD_IMAGE_ITEM_ID,
250259
name = "",
251260
source = "",
252-
dateCreated = Date()
261+
dateCreated = Date(),
262+
isCoverImage = false
253263
)
254264
)
255265
}
@@ -290,7 +300,7 @@ class WCProductImageGalleryView @JvmOverloads constructor(
290300
}
291301

292302
for (index in images.indices) {
293-
if (images[index].id != actualImages[index].id) {
303+
if (images[index] != actualImages[index]) {
294304
return false
295305
}
296306
}
@@ -423,11 +433,13 @@ class WCProductImageGalleryView @JvmOverloads constructor(
423433
viewBinding.uploadProgess.visibility = View.VISIBLE
424434
viewBinding.addImageContainer.visibility = View.GONE
425435
}
436+
426437
VIEW_TYPE_ADD_IMAGE -> {
427438
viewBinding.productImage.visibility = View.GONE
428439
viewBinding.uploadProgess.visibility = View.GONE
429440
viewBinding.addImageContainer.visibility = View.VISIBLE
430441
}
442+
431443
else -> {
432444
viewBinding.productImage.visibility = View.VISIBLE
433445
viewBinding.productImage.alpha = 1.0F
@@ -439,6 +451,10 @@ class WCProductImageGalleryView @JvmOverloads constructor(
439451
viewBinding.deleteImageButton.setOnClickListener {
440452
listener.onGalleryImageDeleteIconClicked(image)
441453
}
454+
viewBinding.coverTag.visibility = when {
455+
image.isCoverImage -> View.VISIBLE
456+
else -> View.GONE
457+
}
442458
}
443459

444460
private fun setMargins() {
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
3-
<stroke android:width="@dimen/minor_10" android:color="@color/divider_color" />
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/listview_background_shape">
4+
<stroke
5+
android:width="@dimen/minor_10"
6+
android:color="@color/divider_color" />
47
<corners android:radius="@dimen/minor_75" />
58
</shape>

WooCommerce/src/main/res/layout/fragment_product_images.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
android:layout_width="match_parent"
6161
android:layout_height="wrap_content"
6262
android:gravity="center"
63-
android:text="@string/product_images_drag_and_drop_description" />
63+
android:text="@string/product_images_drag_and_drop_to_reorder" />
6464

6565
<com.google.android.material.textview.MaterialTextView
6666
android:id="@+id/textWarning"

WooCommerce/src/main/res/layout/image_gallery_item.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,22 @@
5353
android:visibility="gone"
5454
tools:visibility="visible" />
5555

56+
<TextView
57+
android:id="@+id/coverTag"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_gravity="top|start"
61+
android:layout_margin="@dimen/minor_50"
62+
android:background="@drawable/bg_rounded_box"
63+
android:backgroundTint="@color/color_primary"
64+
android:contentDescription="@string/product_cover_photo_tag"
65+
android:paddingHorizontal="@dimen/minor_75"
66+
android:paddingVertical="@dimen/minor_50"
67+
android:text="@string/product_cover_photo_tag"
68+
android:textAppearance="?attr/textAppearanceCaption"
69+
android:textColor="@color/white"
70+
android:textStyle="bold"
71+
android:visibility="gone"
72+
tools:visibility="visible" />
73+
5674
</FrameLayout>

WooCommerce/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,7 @@
24002400
<string name="product_add_photo">Add photo</string>
24012401
<string name="product_replace_photo">Replace photo</string>
24022402
<string name="product_remove_photo">Remove photo</string>
2403+
<string name="product_cover_photo_tag">Cover</string>
24032404
<string name="product_image_add">Add a product image</string>
24042405
<string name="product_image_error_removing">Error removing product image</string>
24052406
<string name="product_image_service_error_uploading">Error uploading product image</string>
@@ -2419,7 +2420,7 @@
24192420
<string name="media_picker_dialog_title">Select Media Source</string>
24202421
<string name="wpmedia_picker_title">WordPress media library</string>
24212422
<string name="product_images_image_limit_warning">Only one photo can be displayed per product variation</string>
2422-
<string name="product_images_drag_and_drop_description">Drag and drop to re-order photos</string>
2423+
<string name="product_images_drag_and_drop_to_reorder">Drag and drop to re-order photos. The first photo will be set as the cover.</string>
24232424
<string name="product_images_validate_drag_and_drop">Validate</string>
24242425
<string name="product_image_service_error_media_null">Media could not be found</string>
24252426
<string name="product_image_service_error_uploading_single">%d file couldn\'t be uploaded</string>

0 commit comments

Comments
 (0)