-
Notifications
You must be signed in to change notification settings - Fork 117
[Woo POS] Initial mechanism to handle quantities and out of stock #12767
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
59c38d8
Add SearchView and FilterView to dashboard
iamgabrielma 8282277
Add stockQuantity property to POSProduct model
iamgabrielma 859239c
Make grid product cards of fixed size
iamgabrielma 2f822dc
Extract search and filter views to own files
iamgabrielma 5a2bfc4
linting
iamgabrielma 3c7d085
Add preview macros and debug preprocessor
iamgabrielma 004e59b
Add different style to “Out of stock” products
iamgabrielma 7e38b42
Handle reduce inventory
iamgabrielma 64ad688
Handle restore inventory
iamgabrielma 4939afd
Merge branch 'trunk' into task/12761-pos-quantity-mechanism
iamgabrielma 09c8216
Wrap cartProduct rows in scrollview
iamgabrielma 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
import SwiftUI | ||
|
||
struct FilterView: View { | ||
@ObservedObject private var viewModel: PointOfSaleDashboardViewModel | ||
|
||
init(viewModel: PointOfSaleDashboardViewModel) { | ||
self.viewModel = viewModel | ||
} | ||
|
||
var body: some View { | ||
Button("Filter") { | ||
// TODO: https://github.com/woocommerce/woocommerce-ios/issues/12761 | ||
} | ||
.frame(maxWidth: .infinity, idealHeight: 120) | ||
.font(.title2) | ||
.foregroundColor(Color.white) | ||
.background(Color.secondaryBackground) | ||
.cornerRadius(10) | ||
.border(Color.white, width: 2) | ||
} | ||
} | ||
|
||
#if DEBUG | ||
#Preview { | ||
FilterView(viewModel: PointOfSaleDashboardViewModel(products: [], | ||
cardReaderConnectionViewModel: .init(state: .connectingToReader))) | ||
} | ||
#endif |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
12 changes: 10 additions & 2 deletions
12
WooCommerce/Classes/POS/Presentation/QuantityBadgeView.swift
This file contains hidden or 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 |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import SwiftUI | ||
|
||
struct QuantityBadgeView: View { | ||
private let productQuantity: Int | ||
|
||
init(_ productQuantity: Int) { | ||
self.productQuantity = productQuantity | ||
} | ||
|
||
var body: some View { | ||
Text("n") | ||
Text("\(productQuantity)") | ||
.foregroundColor(Color.white) | ||
} | ||
} | ||
|
||
#if DEBUG | ||
#Preview { | ||
QuantityBadgeView() | ||
QuantityBadgeView(3) | ||
} | ||
#endif |
This file contains hidden or 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,20 @@ | ||
import SwiftUI | ||
|
||
struct SearchView: View { | ||
// TODO: https://github.com/woocommerce/woocommerce-ios/issues/12762 | ||
var body: some View { | ||
TextField("Search", text: .constant("Search")) | ||
.frame(maxWidth: .infinity, idealHeight: 120) | ||
.font(.title2) | ||
.foregroundColor(Color.white) | ||
.background(Color.secondaryBackground) | ||
.cornerRadius(10) | ||
.border(Color.white, width: 2) | ||
} | ||
} | ||
|
||
#if DEBUG | ||
#Preview { | ||
SearchView() | ||
} | ||
#endif |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -13,15 +13,54 @@ final class PointOfSaleDashboardViewModel: ObservableObject { | |
self.cardReaderConnectionViewModel = cardReaderConnectionViewModel | ||
} | ||
|
||
// Creates a unique `CartProduct` from a `Product`, and adds it to the Cart | ||
func addProductToCart(_ product: POSProduct) { | ||
let cartProduct = CartProduct(id: UUID(), product: product, quantity: 1) | ||
productsInCart.append(cartProduct) | ||
if product.stockQuantity > 0 { | ||
reduceInventory(product) | ||
|
||
let cartProduct = CartProduct(id: UUID(), product: product, quantity: 1) | ||
productsInCart.append(cartProduct) | ||
} else { | ||
// TODO: Handle out of stock | ||
// wp.me/p91TBi-bcW#comment-12123 | ||
return | ||
} | ||
} | ||
|
||
func reduceInventory(_ product: POSProduct) { | ||
guard let index = products.firstIndex(where: { $0.itemID == product.itemID }) else { | ||
return | ||
} | ||
let updatedQuantity = product.stockQuantity - 1 | ||
let updatedProduct = POSProduct(itemID: product.itemID, | ||
productID: product.productID, | ||
name: product.name, | ||
price: product.price, | ||
stockQuantity: updatedQuantity) | ||
products[index] = updatedProduct | ||
} | ||
|
||
func restoreInventory(_ product: POSProduct) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could simplified into an unique method for reducing/increasing, no strong reason to keep them separate at this point, just preference. |
||
guard let index = products.firstIndex(where: { $0.itemID == product.itemID }) else { | ||
return | ||
} | ||
let updatedQuantity = product.stockQuantity + 1 | ||
let updatedProduct = POSProduct(itemID: product.itemID, | ||
productID: product.productID, | ||
name: product.name, | ||
price: product.price, | ||
stockQuantity: updatedQuantity) | ||
products[index] = updatedProduct | ||
} | ||
|
||
// Removes a `CartProduct` from the Cart | ||
func removeProductFromCart(_ cartProduct: CartProduct) { | ||
productsInCart.removeAll(where: { $0.id == cartProduct.id }) | ||
|
||
// When removing an item from the cart, restore previous inventory | ||
guard let match = products.first(where: { $0.productID == cartProduct.product.productID }) else { | ||
return | ||
} | ||
restoreInventory(match) | ||
} | ||
|
||
func submitCart() { | ||
|
This file contains hidden or 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
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.
Maybe make the whole view a button so that tapping the entire view content adds the product to cart?
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.
I'll leave this change out for the moment, since we have a specific button for the
+
and we do not know if tapping in the card itself should have a different behaviour (if any)