Skip to content

Commit ff996a8

Browse files
authored
[Woo POS] Hide cart images when the view is too narrow (#15028)
2 parents f134b12 + 4dca723 commit ff996a8

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

WooCommerce/Classes/POS/Presentation/CartView.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct CartView: View {
1515
posModel.cart.isNotEmpty && offSetPosition < 0
1616
}
1717

18+
@State private var shouldShowItemImages: Bool = false
19+
1820
var body: some View {
1921
VStack {
2022
DynamicHStack(spacing: Constants.cartHeaderSpacing) {
@@ -71,6 +73,7 @@ struct CartView: View {
7173
VStack(spacing: Constants.cartItemSpacing) {
7274
ForEach(posModel.cart, id: \.id) { cartItem in
7375
ItemRowView(cartItem: cartItem,
76+
showImage: $shouldShowItemImages,
7477
onItemRemoveTapped: posModel.orderStage == .building ? {
7578
posModel.remove(cartItem: cartItem)
7679
} : nil)
@@ -82,6 +85,15 @@ struct CartView: View {
8285
.background(GeometryReader { geometry in
8386
Color.clear.preference(key: ScrollOffSetPreferenceKey.self,
8487
value: geometry.frame(in: coordinateSpace).origin.y)
88+
.onAppear {
89+
updateItemImageVisibility(cartListWidth: geometry.size.width)
90+
}
91+
.onChange(of: geometry.size.width) {
92+
updateItemImageVisibility(cartListWidth: $0)
93+
}
94+
.onChange(of: dynamicTypeSize) {
95+
updateItemImageVisibility(dynamicTypeSize: $0, cartListWidth: geometry.size.width)
96+
}
8597
})
8698
.onPreferenceChange(ScrollOffSetPreferenceKey.self) { position in
8799
self.offSetPosition = position
@@ -158,6 +170,39 @@ private extension CartView {
158170
cart: posModel.cart,
159171
orderStage: posModel.orderStage)
160172
}
173+
174+
func updateItemImageVisibility(dynamicTypeSize: DynamicTypeSize? = nil, cartListWidth: CGFloat) {
175+
let newVisibility = cartListWidth >= minimumWidthToShowItemImages(with: dynamicTypeSize ?? self.dynamicTypeSize)
176+
guard newVisibility != shouldShowItemImages else {
177+
return
178+
}
179+
shouldShowItemImages = newVisibility
180+
}
181+
182+
func minimumWidthToShowItemImages(with dynamicTypeSize: DynamicTypeSize) -> CGFloat {
183+
switch dynamicTypeSize {
184+
case .xSmall, .small:
185+
240
186+
case .medium:
187+
260
188+
case .large:
189+
270
190+
case .xLarge:
191+
280
192+
case .xxLarge, .xxxLarge:
193+
300
194+
case .accessibility1:
195+
320
196+
case .accessibility2:
197+
400
198+
case .accessibility3, .accessibility4:
199+
420
200+
case .accessibility5:
201+
450
202+
@unknown default:
203+
450
204+
}
205+
}
161206
}
162207

163208
@available(iOS 17.0, *)

WooCommerce/Classes/POS/Presentation/ItemRowView.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ struct ItemRowView: View {
55
private let onItemRemoveTapped: (() -> Void)?
66

77
@ScaledMetric private var scale: CGFloat = 1.0
8-
@Environment(\.dynamicTypeSize) var dynamicTypeSize
98
@Environment(\.colorScheme) var colorScheme
9+
@Binding private var showProductImage: Bool
1010

11-
init(cartItem: CartItem, onItemRemoveTapped: (() -> Void)? = nil) {
11+
init(cartItem: CartItem, showImage: Binding<Bool> = .constant(true), onItemRemoveTapped: (() -> Void)? = nil) {
1212
self.cartItem = cartItem
13+
self._showProductImage = showImage
1314
self.onItemRemoveTapped = onItemRemoveTapped
1415
}
1516

@@ -31,6 +32,7 @@ struct ItemRowView: View {
3132
.font(Constants.itemPriceFont)
3233
}
3334
.frame(maxWidth: .infinity, alignment: .leading)
35+
.padding(.leading, showProductImage ? 0 : Constants.cardContentHorizontalPadding)
3436
.accessibilityElement(children: .combine)
3537

3638
if let onItemRemoveTapped {
@@ -41,7 +43,7 @@ struct ItemRowView: View {
4143
.font(.posBodyRegular)
4244
})
4345
.accessibilityLabel(Localization.removeFromCartAccessibilityLabel)
44-
.padding()
46+
.padding(.trailing, Constants.cardContentHorizontalPadding)
4547
.foregroundColor(Color.posTertiaryText)
4648
}
4749
}
@@ -57,7 +59,7 @@ struct ItemRowView: View {
5759

5860
@ViewBuilder
5961
private var productImage: some View {
60-
if dynamicTypeSize >= .accessibility3 {
62+
if !showProductImage {
6163
EmptyView()
6264
} else if let imageSource = cartItem.item.productImageSource {
6365
ProductImageThumbnail(productImageURL: URL(string: imageSource),
@@ -105,6 +107,7 @@ private extension ItemRowView {
105107
static let cardOutlineWidth: CGFloat = 1
106108
static let horizontalPadding: CGFloat = 16
107109
static let horizontalElementSpacing: CGFloat = 16
110+
static let cardContentHorizontalPadding: CGFloat = 16
108111
static let itemTitleAndPriceSpacing: CGFloat = 4
109112
static let itemTitleFont: POSFontStyle = .posDetailEmphasized
110113
static let itemSubtitleFont: POSFontStyle = .posDetailLight

WooCommerce/Classes/POS/Utils/Color+WooCommercePOS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension Color {
3232
Color(
3333
UIColor(
3434
light: .white,
35-
dark: .tertiarySystemBackground
35+
dark: UIColor(red: 44.0 / 255.0, green: 44.0 / 255.0, blue: 46.0 / 255.0, alpha: 1.0)
3636
)
3737
)
3838
}

0 commit comments

Comments
 (0)