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

[Woo POS] Extract cart logic to CartViewModel #13191

Merged
merged 17 commits into from
Jul 1, 2024

Conversation

iamgabrielma
Copy link
Contributor

@iamgabrielma iamgabrielma commented Jun 28, 2024

Part of #12998

Description

This is the step 2 of #12998 , here we move Cart-related actions and its handling to a separate CartViewModel.

Its design follows the same pattern as with the product list, we'll observe changes in the model from the dashboard and use these to coordinate and communicate between different subviews. In general:

  • When the cart is submitted to the next stage of the flow via submitCart(_:), emits the cart items to its subscribers.
  • When the cart is modified to the previous stage of the flow via addMoreToCart(), emits a signal to its subscribers.
  • All cart-related logic (add/delete/clear/...) happens internally on the VM.

Changes

  • Created CartViewModel
  • Moved cart-related logic to this new VM
  • Added unit tests

Testing

  • Run the POS, and observe the product selector and cart interactions behave normally:
    • Adding/removing/"clear all" items to the cart works normally
    • Tapping on "checkout" submits the order for next stage (totals + payment screens)
    • Tapping "add more" brings us back to the product list (after re-loading products from the network) and we can continue updating the order as needed.

Simulator Screen Recording - iPad Pro (12 9-inch) (6th generation) - 2024-07-01 at 15 50 33

There's 3 items that I left outside of this PR, since are not totally related to the refactor itself, and can be tackled separately:

@iamgabrielma iamgabrielma added type: task An internally driven task. feature: POS labels Jun 28, 2024
@iamgabrielma iamgabrielma added this to the 19.4 milestone Jun 28, 2024
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jun 28, 2024

WooCommerce iOS📲 You can test the changes from this Pull Request in WooCommerce iOS by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS WooCommerce iOS
Build Numberpr13191-e15efe2
Version19.3
Bundle IDcom.automattic.alpha.woocommerce
Commite15efe2
App Center BuildWooCommerce - Prototype Builds #9827
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

…s-extract-cart-vm

* task/12998-pos-extract-item-list-vm: (46 commits)
  Nit updates.
  Allow setting a different title for the top performers list
  Update localized string keys and comments
  Freeze strings for localization
  Update metadata strings
  Update release_notes for 19.3
  Update CHANGELOG for 19.3
  Ensure Google Campaigns list contains top 5 campaigns by total sales
  Fix linting error
  Remove unused imports
  Add unit tests for Google Campaigns analytics card view model
  Hide campaign image placeholder when card is redacted
  Update total sales properties
  Release Notes: add new section for next version (19.4)
  Update draft release notes for 19.3.
  Bump version number
  Hide image placeholder on Google Campaigns card
  Add view model for Google Campaigns analytics card
  Add Google Ads Programs web report
  Update naming for analytics card to display a stat and list of top performers
  ...
Base automatically changed from task/12998-pos-extract-item-list-vm to trunk June 28, 2024 19:38
jaclync added 4 commits June 28, 2024 16:40
…12998-pos-extract-cart-vm

* task/12998-fix-items-not-set-for-order-sync:
  Fix order sync from passing empty items for the products.

# Conflicts:
#	WooCommerce/Classes/POS/ViewModels/PointOfSaleDashboardViewModel.swift
@jaclync jaclync changed the base branch from trunk to task/12998-fix-items-not-set-for-order-sync June 28, 2024 20:50
Base automatically changed from task/12998-fix-items-not-set-for-order-sync to trunk July 1, 2024 03:52
@iamgabrielma iamgabrielma marked this pull request as ready for review July 1, 2024 08:31
@iamgabrielma
Copy link
Contributor Author

Only one review is needed.

cc: @staskus just looping you in for awareness, but feel free to review/drop any comment as well 🙇

@bozidarsevo
Copy link
Contributor

I see few more // TODO: 12998, will there be more extraction PRs for the PointOfSaleDashboardViewModel?

Copy link
Contributor

@bozidarsevo bozidarsevo left a comment

Choose a reason for hiding this comment

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

Just that question regarding TODOs 👍

@jaclync
Copy link
Contributor

jaclync commented Jul 1, 2024

I see few more // TODO: 12998, will there be more extraction PRs for the PointOfSaleDashboardViewModel?

👋 I added those TODO comments, and yes they will be moved/extracted from PointOfSaleDashboardViewModel to a different view model in #13211.

Copy link
Contributor

@jaclync jaclync left a comment

Choose a reason for hiding this comment

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

Thanks for updating the preview and tests! I added a test case and tested it for 2 transactions again, LGTM :shipit: I'll merge it shortly to unblock #13211.

Comment on lines +114 to +115
let orderStageSubject = PassthroughSubject<PointOfSaleDashboardViewModel.OrderStage, Never>()
let orderStagePublisher = orderStageSubject.eraseToAnyPublisher()
Copy link
Contributor

Choose a reason for hiding this comment

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

In SwiftUI preview, we could also pass a constant value like passing Just< PointOfSaleDashboardViewModel.OrderStage >(.building) to CartViewModel(orderStage:). We can potentially have two previews, one for each order stage case.

Would like to see what you think about this change, I will merge the PR without updating this 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for merging, appreciated! 🙇

In SwiftUI preview, we could also pass a constant value like passing Just< PointOfSaleDashboardViewModel.OrderStage >(.building) to CartViewModel(orderStage:). We can potentially have two previews, one for each order stage case.

That sounds good, I've updated #13207 for reference

Comment on lines +72 to +77
/* TODO:
https://github.com/woocommerce/woocommerce-ios/issues/13209
The unique UUID for CartItem is set on init, but CartItem is only internal to addItemToCart()
We need to extract this to a separate function and assure that ID's are correct,
otherwise the UUID's for testing won't match
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

For this test case, I wonder if we could first add some items by using the cart item from itemsInCart. I added a simple test case in e15efe2, feel free to add other edge cases in the TODO issue.

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, initially I wanted to test only by id to assure that we remove the exact "line item" if a cart has multiple of the same productID, so if we happen to change the implementation, we wouldn't get a false positive from tests if we happen to have just another item with the same name or property in the list.

That said I think testing it like this and adding the followup is good enough for the moment (better than having no test case!). Thanks for tackling it 🙇

@jaclync jaclync enabled auto-merge July 1, 2024 14:46
@jaclync jaclync merged commit 432ea08 into trunk Jul 1, 2024
19 of 22 checks passed
@jaclync jaclync deleted the task/12998-pos-extract-cart-vm branch July 1, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: POS type: task An internally driven task.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants