Skip to content

Commit 1efcfa4

Browse files
committed
Make ProductImageThumbnail's placeholder view injectable so that POS image view can pass a different placeholder image.
1 parent 8729326 commit 1efcfa4

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

WooCommerce/Classes/POS/Presentation/Reusable Views/POSItemImageView.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ struct POSItemImageView: View {
66
let imageSize: CGFloat
77
let scale: CGFloat
88

9+
private var placeholderView: some View {
10+
Rectangle()
11+
.foregroundColor(Constants.placeholderBackgroundColor)
12+
.overlay {
13+
Text(Image(systemName: "archivebox"))
14+
.font(.posButtonSymbol)
15+
.foregroundColor(Constants.placeholderIconColor)
16+
}
17+
}
18+
919
var body: some View {
1020
if let imageSource {
1121
ProductImageThumbnail(productImageURL: URL(string: imageSource),
1222
productImageSize: imageSize,
1323
scale: scale,
1424
foregroundColor: Constants.placeholderIconColor,
15-
cachesOriginalImage: true)
25+
cachesOriginalImage: true) {
26+
placeholderView
27+
}
1628
} else {
17-
Rectangle()
18-
.foregroundColor(Constants.placeholderBackgroundColor)
19-
.overlay {
20-
Text(Image(systemName: "archivebox"))
21-
.font(.posButtonSymbol)
22-
.foregroundColor(Constants.placeholderIconColor)
23-
}
29+
placeholderView
2430
}
2531
}
2632
}

WooCommerce/Classes/ViewRelated/Products/ProductSelector/ProductImageThumbnail.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import SwiftUI
22
import Kingfisher
33

4-
struct ProductImageThumbnail: View {
4+
struct ProductImageThumbnail<Placeholder: View>: View {
55
private let productImageURL: URL?
66
private let productImageSize: CGFloat
77
private let scale: CGFloat
88
private let productImageCornerRadius: CGFloat
99
private let foregroundColor: Color
1010
private let cachesOriginalImage: Bool
11+
private let placeholder: Placeholder
1112

1213
/// Image processor to resize images in a background thread to avoid blocking the UI
1314
///
@@ -23,21 +24,23 @@ struct ProductImageThumbnail: View {
2324
scale: CGFloat,
2425
productImageCornerRadius: CGFloat = 0,
2526
foregroundColor: Color,
26-
cachesOriginalImage: Bool = false) {
27+
cachesOriginalImage: Bool = false,
28+
@ViewBuilder placeholder: () -> Placeholder) {
2729
self.productImageURL = productImageURL
2830
self.productImageSize = productImageSize
2931
self.scale = scale
3032
self.productImageCornerRadius = productImageCornerRadius
3133
self.foregroundColor = foregroundColor
3234
self.cachesOriginalImage = cachesOriginalImage
35+
self.placeholder = placeholder()
3336
}
3437

3538
var body: some View {
3639
KFImage
3740
.url(productImageURL)
3841
.cacheOriginalImage(cachesOriginalImage)
3942
.placeholder {
40-
Image(uiImage: .productPlaceholderImage)
43+
placeholder
4144
}
4245
.setProcessor(imageProcessor)
4346
.resizable()
@@ -48,3 +51,22 @@ struct ProductImageThumbnail: View {
4851
.accessibilityHidden(true)
4952
}
5053
}
54+
55+
// Convenience initializer that maintains the default behavior.
56+
extension ProductImageThumbnail where Placeholder == Image {
57+
init(productImageURL: URL?,
58+
productImageSize: CGFloat,
59+
scale: CGFloat,
60+
productImageCornerRadius: CGFloat = 0,
61+
foregroundColor: Color,
62+
cachesOriginalImage: Bool = false) {
63+
self.init(productImageURL: productImageURL,
64+
productImageSize: productImageSize,
65+
scale: scale,
66+
productImageCornerRadius: productImageCornerRadius,
67+
foregroundColor: foregroundColor,
68+
cachesOriginalImage: cachesOriginalImage) {
69+
Image(uiImage: .productPlaceholderImage)
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)