-
Notifications
You must be signed in to change notification settings - Fork 114
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3a18a4d
Create CartViewModel
iamgabrielma 36b6f5c
Extract addItemToCart
iamgabrielma 96d2150
Extract removal methods
iamgabrielma 5c2ab4e
Restore isCartCollapsed
iamgabrielma 6f76016
Observe cart submissions
iamgabrielma bfe2381
Extract CartViewModel to own file
iamgabrielma 5282326
Merge branch 'task/12998-pos-extract-item-list-vm' into task/12998-po…
jaclync d0f890c
Implement other cart observations to maintain the existing behavior.
jaclync c4a2bb1
Merge branch 'task/12998-fix-items-not-set-for-order-sync' into task/…
jaclync ed2237b
Fix cart items still deletable in order finalizing stage.
jaclync e00e222
Hide clear cart CTA in order finalizing stage.
jaclync 8c8d6f7
Mark `CartViewModel.orderStage` as private.
jaclync b9649d9
restore `CartView` preview
iamgabrielma f5b2bfd
Unit tests for CartViewModel
iamgabrielma a623662
Delete cart-related tests from dashboard test suite
iamgabrielma 000442f
lint
iamgabrielma e15efe2
Add a test case on `CartViewModel.removeItemFromCart`.
jaclync File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import SwiftUI | ||
import Combine | ||
import protocol Yosemite.POSItem | ||
|
||
final class CartViewModel: ObservableObject { | ||
/// Emits cart items when the CTA is tapped to submit the cart. | ||
let cartSubmissionPublisher: AnyPublisher<[CartItem], Never> | ||
private let cartSubmissionSubject: PassthroughSubject<[CartItem], Never> = .init() | ||
|
||
/// Emits a signal when the CTA is tapped to update the cart. | ||
let addMoreToCartActionPublisher: AnyPublisher<Void, Never> | ||
private let addMoreToCartActionSubject: PassthroughSubject<Void, Never> = .init() | ||
|
||
@Published private(set) var itemsInCart: [CartItem] = [] | ||
|
||
// It should be synced with the source of truth in `PointOfSaleDashboardViewModel`. | ||
@Published private var orderStage: PointOfSaleDashboardViewModel.OrderStage = .building | ||
|
||
var canDeleteItemsFromCart: Bool { | ||
orderStage != .finalizing | ||
} | ||
|
||
init(orderStage: AnyPublisher<PointOfSaleDashboardViewModel.OrderStage, Never>) { | ||
cartSubmissionPublisher = cartSubmissionSubject.eraseToAnyPublisher() | ||
addMoreToCartActionPublisher = addMoreToCartActionSubject.eraseToAnyPublisher() | ||
orderStage.assign(to: &$orderStage) | ||
} | ||
|
||
func addItemToCart(_ item: POSItem) { | ||
let cartItem = CartItem(id: UUID(), item: item, quantity: 1) | ||
itemsInCart.append(cartItem) | ||
} | ||
|
||
func removeItemFromCart(_ cartItem: CartItem) { | ||
itemsInCart.removeAll(where: { $0.id == cartItem.id }) | ||
} | ||
|
||
func removeAllItemsFromCart() { | ||
itemsInCart.removeAll() | ||
} | ||
|
||
var itemToScrollToWhenCartUpdated: CartItem? { | ||
return itemsInCart.last | ||
} | ||
|
||
var itemsInCartLabel: String? { | ||
switch itemsInCart.count { | ||
case 0: | ||
return nil | ||
default: | ||
return String.pluralize(itemsInCart.count, | ||
singular: "%1$d item", | ||
plural: "%1$d items") | ||
} | ||
} | ||
|
||
func submitCart() { | ||
cartSubmissionSubject.send(itemsInCart) | ||
} | ||
|
||
func addMoreToCart() { | ||
addMoreToCartActionSubject.send(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
toCartViewModel(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 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for merging, appreciated! 🙇
That sounds good, I've updated #13207 for reference