Skip to content

Conversation

@AnirudhBhat
Copy link
Contributor

@AnirudhBhat AnirudhBhat commented May 22, 2023

Closes: #9081

Description

Before this PR
When you scan the same product more than once, the same product was being added to the list in a separate row instead of just incrementing the already existing product count.

After this PR
When you scan the same product more than once, the product quantity gets incremented as expected.

Testing instructions

  1. Navigate to the Order details screen (orders -> "+")
  2. Click on the barcode icon present under the products section
  3. Scan the barcode (Make sure to generate the barcode with an valid SKU present in your store - example: https://barcode.tec-it.com/en/?data=AC-1)
  4. Ensure the product gets added
  5. Scan the same barcode again
  6. Ensure the already added product's quantity gets incremented and no new product gets added.

Images/gif

Please ignore the scanner not displaying the camera session. It's something to do with Google play services. It's messed up on my device, again.

screen-20230522-174950.2.mp4
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

…ue/9081-increment-product-quantity

# Conflicts:
#	WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/creation/CreationFocusedOrderCreateEditViewModelTest.kt
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented May 22, 2023

You can test the changes on this Pull Request by downloading an installable build, or scanning this QR code:

@codecov-commenter
Copy link

codecov-commenter commented May 22, 2023

Codecov Report

Patch coverage: 90.47% and project coverage change: -0.06 ⚠️

Comparison is base (b6dc535) 43.59% compared to head (42c121f) 43.54%.

❗ Current head 42c121f differs from pull request most recent head ff6c60d. Consider uploading reports for the commit ff6c60d to get more accurate results

Additional details and impacted files
@@                                 Coverage Diff                                 @@
##             issue/9073-handle-error-case-barcode-scanning    #9083      +/-   ##
===================================================================================
- Coverage                                            43.59%   43.54%   -0.06%     
+ Complexity                                            4094     4041      -53     
===================================================================================
  Files                                                  834      833       -1     
  Lines                                                43985    43941      -44     
  Branches                                              5763     5741      -22     
===================================================================================
- Hits                                                 19176    19133      -43     
+ Misses                                               23137    23134       -3     
- Partials                                              1672     1674       +2     
Impacted Files Coverage Δ
...oid/ui/orders/creation/OrderCreateEditViewModel.kt 88.02% <90.00%> (+1.02%) ⬆️
...com/woocommerce/android/ui/products/ProductType.kt 76.19% <100.00%> (ø)

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

…sponse from the product search using SKU returns type as "variation" for variable product.
@AnirudhBhat AnirudhBhat requested a review from kidinov May 22, 2023 12:42
@AnirudhBhat AnirudhBhat added the feature: mobile payments Related to mobile payments / card present payments / Woo Payments. label May 22, 2023
@AnirudhBhat AnirudhBhat added this to the 13.8 milestone May 22, 2023
@AnirudhBhat AnirudhBhat marked this pull request as ready for review May 22, 2023 12:42
@kidinov kidinov self-assigned this May 23, 2023
}
}

private fun getItemIdIfVariableProductIsAlreadySelected(product: com.woocommerce.android.model.Product): Long? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np: you may consider using

import com.woocommerce.android.model.Product as ModelProduct

To make it a bit more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here ff6c60d

selectedItems + SelectedItem.ProductVariation(
productId = product.parentId,
variationId = product.remoteId
getItemIdIfVariableProductIsAlreadySelected(product)?.let { itemId ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np: It took me some time to figure out this code. Maybe to put these a bit more streightforward:

                        when (val alreadySelectedItemId = getItemIdIfVariableProductIsAlreadySelected(product)) {
                            null -> onProductsSelected(selectedItems +
                                SelectedItem.ProductVariation(
                                    productId = product.parentId,
                                    variationId = product.remoteId
                                )
                            )
                            else -> onIncreaseProductsQuantity(alreadySelectedItemId)
                        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here ff6c60d

@kidinov
Copy link
Contributor

kidinov commented May 23, 2023

I notice that I can not add variable products via scanning. No error is shown. The issue comes from this code from my quick debugging

                val itemsToAdd = selectedItems.filter { selectedItem ->
                    if (selectedItem is SelectedItem.ProductVariation) {
                        none { it.variationId == selectedItem.variationId }
                    } else {
                        none { it.productId == selectedItem.id }
                    }

(check SKU: AC)

05-23--12-39.mp4

@AnirudhBhat
Copy link
Contributor Author

Thanks for the review @kidinov

I notice that I can not add variable products via scanning

Hmm. I believe we cannot add the parent variable product as is right? We should select the specific variant of the product. Example: AC-1, AC-2.

Even with adding the products manually, we should select the specific variant and cannot add the parent variable product itself. Right?

…ue/9081-increment-product-quantity

# Conflicts:
#	WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/creation/OrderCreateEditViewModel.kt
@kidinov
Copy link
Contributor

kidinov commented May 24, 2023

@AnirudhBhat

Hmm. I believe we cannot add the parent variable product as is right? We should select the specific variant of the product. Example: AC-1, AC-2.

Even with adding the products manually, we should select the specific variant and cannot add the parent variable product itself. Right?

It sounds about right. But still - this case is swollen, while we have all the info to handle that. We may want to show a message/snack bar that explains that the parent product can not be added to the order

@peril-woocommerce
Copy link

peril-woocommerce bot commented May 24, 2023

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 2 days Please, make sure to get it merged by then or assign it to a later expiring milestone

Generated by 🚫 dangerJS

@AnirudhBhat
Copy link
Contributor Author

Thanks for the review @kidinov

It sounds about right. But still - this case is swollen, while we have all the info to handle that. We may want to show a message/snack bar that explains that the parent product can not be added to the order

I agree. Implemented here: ff6c60d

}

private fun sendAddingProductsViaScanningFailedEvent(
@StringRes message: Int = string.order_creation_barcode_scanning_unable_to_add_product
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably in the parent PR, you will use different strings for 2 different error handling, so I guess having a default value here doesn't make any good?

Also, np: we usually use R.string import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Done here ed8b7e8

?.takeIf { it.isNotEmpty() }
?.find { it.productId == 10L }
?.let { assertThat(it.quantity).isEqualTo(3f) }
?: Assertions.fail("Expected an item with productId 10L with quantity as 3")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np: usually in test we use static imports for assertions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here ed8b7e8

@kidinov
Copy link
Contributor

kidinov commented May 24, 2023

Not related to the PR, but we make the same architecture mistake here as with IPP/TTP when we tight application-scoped things to VM scope. Try to enable don't keep activity and see that it all falls apart. We are not subscribed to the emitter, and instead of using StateFlow, we use stateless flow so the result is lost

Maybe create a ticket and think if and how it should be addressed

05-24--11-52.mp4

@kidinov kidinov self-requested a review May 24, 2023 07:54
@kidinov
Copy link
Contributor

kidinov commented May 24, 2023

Is this expected that the snackbar stays forever?

05-24--11-57.mp4

Copy link
Contributor

@kidinov kidinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good to me. I left a couple of non blocking comments, and a couple of not related to the PR but related to the feature itself

…ue/9081-increment-product-quantity

# Conflicts:
#	WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/creation/OrderCreateEditViewModel.kt
#	WooCommerce/src/main/res/values/strings.xml
@AnirudhBhat
Copy link
Contributor Author

Not related to the PR, but we make the same architecture mistake here as with IPP/TTP when we tight application-scoped things to VM scope. Try to enable don't keep activity and see that it all falls apart. We are not subscribed to the emitter, and instead of using StateFlow, we use stateless flow so the result is lost

Yup. This is a known issue and I'll take care of this as soon as the first release is out.

These are the things that I'll be taking care of after the first release

  1. Add information in the user-facing errors
  2. Handle process death while scanning

Let me know if I've missed anything here.

@AnirudhBhat
Copy link
Contributor Author

Is this expected that the snackbar stays forever?

Yes. In order to dismiss it. You need to swipe right.

Base automatically changed from issue/9073-handle-error-case-barcode-scanning to trunk May 24, 2023 08:32
@kidinov
Copy link
Contributor

kidinov commented May 24, 2023

@AnirudhBhat

Yes. In order to dismiss it. You need to swipe right.

Hm, feels not really Android way of showing errors then. I'd vote for using dismissive snackbars for this purpose then

@AnirudhBhat AnirudhBhat enabled auto-merge May 24, 2023 08:33
@AnirudhBhat AnirudhBhat merged commit 4b62b61 into trunk May 24, 2023
@AnirudhBhat AnirudhBhat deleted the issue/9081-increment-product-quantity branch May 24, 2023 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: mobile payments Related to mobile payments / card present payments / Woo Payments.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Increment the product quantity when the same product is scanned more than one time

5 participants