Skip to content

Commit

Permalink
[Woo POS] Architecture improvements: minor updates (#14582)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshheald authored Dec 3, 2024
2 parents 7ced0fc + dd7f273 commit 0b7e751
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Codegen

/// Represents an item in a Product Bundle
///
public struct ProductBundleItem: Codable, Equatable, GeneratedCopiable, GeneratedFakeable {
public struct ProductBundleItem: Codable, Equatable, Hashable, GeneratedCopiable, GeneratedFakeable {
/// Bundled item ID
public let bundledItemID: Int64

Expand Down
8 changes: 7 additions & 1 deletion WooCommerce/Classes/POS/Presentation/CartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct CartView: View {
)
}
.padding(.leading, Constants.itemHorizontalPadding)
.renderedIf(posModel.cart.isNotEmpty && posModel.orderStage == .building)
.renderedIf(shouldShowClearCartButton)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down Expand Up @@ -147,6 +147,12 @@ private extension CartView {
orderState: posModel.orderState,
paymentState: posModel.paymentState)
}

var shouldShowClearCartButton: Bool {
viewHelper.shouldShowClearCartButton(
cart: posModel.cart,
orderStage: posModel.orderStage)
}
}

private extension CartView {
Expand Down
4 changes: 4 additions & 0 deletions WooCommerce/Classes/POS/ViewHelpers/CartViewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ final class CartViewHelper {
}
return orderState.isSyncing
}

func shouldShowClearCartButton(cart: [CartItem], orderStage: PointOfSaleOrderStage) -> Bool {
cart.isNotEmpty && orderStage == .building
}
}

private extension PointOfSalePaymentState {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import Testing
@testable import WooCommerce

Expand Down Expand Up @@ -68,4 +69,20 @@ struct CartViewHelperTests {
#expect(sut.itemsInCartLabel(for: count) == expected)
}

@Test func shouldShowClearCartButton_empty_cart_false() async throws {
#expect(sut.shouldShowClearCartButton(cart: [], orderStage: .building) == false)
#expect(sut.shouldShowClearCartButton(cart: [], orderStage: .finalizing) == false)
}

@Test func shouldShowClearCartButton_items_in_cart_and_building_true() async throws {
#expect(sut.shouldShowClearCartButton(cart: [makeItem()], orderStage: .building) == true)
}

@Test func shouldShowClearCartButton_items_in_cart_and_finalizing_false() async throws {
#expect(sut.shouldShowClearCartButton(cart: [makeItem()], orderStage: .finalizing) == false)
}
}

private func makeItem() -> CartItem {
CartItem(id: UUID(), item: MockPOSItem(name: "Item", formattedPrice: "$1.00"), quantity: 1)
}
2 changes: 1 addition & 1 deletion Yosemite/Yosemite/PointOfSale/POSProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct POSProduct: POSOrderableItem, OrderSyncProductTypeProtocol, Equatable {
let bundledItems: [ProductBundleItem] = []
}

extension POSProduct {
extension POSProduct: Hashable {
func toOrderSyncProductInput(quantity: Decimal) -> OrderSyncProductInput {
OrderSyncProductInput(product: .product(self), quantity: quantity)
}
Expand Down
29 changes: 21 additions & 8 deletions Yosemite/Yosemite/Stores/Order/OrderSyncProductInput.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
public protocol OrderSyncProductTypeProtocol {
import Networking

public protocol OrderSyncProductTypeProtocol: Hashable {
var price: String { get }
var productID: Int64 { get }
var productType: ProductType { get }
var bundledItems: [ProductBundleItem] { get }

func isEqual(to: any OrderSyncProductTypeProtocol) -> Bool
}

extension Product: OrderSyncProductTypeProtocol {}

public extension OrderSyncProductTypeProtocol where Self: Equatable {
func isEqual(to other: any OrderSyncProductTypeProtocol) -> Bool {
guard let other = other as? Self else { return false }
return self == other
}
}

/// Product input for an `OrderSynchronizer` type.
///
public struct OrderSyncProductInput {
Expand All @@ -27,24 +38,26 @@ public struct OrderSyncProductInput {
/// Types of products the synchronizer supports
///
public enum ProductType: Hashable {
case product(OrderSyncProductTypeProtocol)
case product(any OrderSyncProductTypeProtocol)
case variation(ProductVariation)

public func hash(into hasher: inout Hasher) {
switch self {
case .product(let product):
hasher.combine("product-\(product.productID)")
hasher.combine("productType-product")
hasher.combine(product)
case .variation(let variation):
hasher.combine("variation-\(variation.productVariationID)")
hasher.combine("productType-variation")
hasher.combine(variation)
}
}

public static func == (lhs: OrderSyncProductInput.ProductType, rhs: OrderSyncProductInput.ProductType) -> Bool {
switch (lhs, rhs) {
case (.product(let l), .product(let r)):
return l.productID == r.productID
case (.variation(let l), .variation(let r)):
return l.productVariationID == r.productVariationID
case (.product(let lhsProduct), .product(let rhsProduct)):
return lhsProduct.isEqual(to: rhsProduct)
case (.variation(let lhsVariation), .variation(let rhsVariation)):
return lhsVariation == rhsVariation
default:
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public struct ProductInputTransformer {
quantity: Decimal,
discount: Decimal? = nil,
bundleConfiguration: [BundledProductConfiguration] = [],
allProducts: [OrderSyncProductTypeProtocol],
allProducts: [any OrderSyncProductTypeProtocol],
allProductVariations: Set<ProductVariation>,
defaultDiscount: Decimal) -> OrderSyncProductInput? {
// Finds the product or productVariation associated with the order item.
Expand Down
2 changes: 1 addition & 1 deletion Yosemite/Yosemite/Tools/POS/POSOrderService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public final class POSOrderService: POSOrderServiceProtocol {
}
}

private struct POSOrderSyncProductType: OrderSyncProductTypeProtocol {
private struct POSOrderSyncProductType: OrderSyncProductTypeProtocol, Hashable {
let productID: Int64
let price: String
// Not used in POS but have to be included for the app usage.
Expand Down

0 comments on commit 0b7e751

Please sign in to comment.