-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Woo POS] Extract cart logic to
CartViewModel
(#13191)
- Loading branch information
Showing
7 changed files
with
315 additions
and
131 deletions.
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.