Skip to content

Commit 43675f8

Browse files
[WCiOS17] Update trait update API for product flows (#16140)
2 parents e46322c + 358e207 commit 43675f8

File tree

6 files changed

+73
-37
lines changed

6 files changed

+73
-37
lines changed

WooCommerce/Classes/Extensions/UICollectionViewCell+Helpers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ extension UICollectionViewCell {
2222
func applyGrayBackgroundStyle() {
2323
backgroundColor = .systemColor(.secondarySystemGroupedBackground)
2424
}
25+
26+
func applyContentBorderColorOnInterfaceStyleChange(borderColor: @escaping () -> UIColor) {
27+
let traits: [UITrait] = [
28+
UITraitUserInterfaceStyle.self,
29+
UITraitAccessibilityContrast.self
30+
]
31+
registerForTraitChanges(traits) { (self: Self, _: UITraitCollection) in
32+
self.contentView.layer.borderColor = borderColor().cgColor
33+
}
34+
}
2535
}

WooCommerce/Classes/ViewRelated/Products/Cells/Product Images/Collection View Cells/AddProductImageCollectionViewCell.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ final class AddProductImageCollectionViewCell: UICollectionViewCell {
1010
configureBackground()
1111
configureImageView()
1212
configureCellAppearance()
13-
}
14-
15-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
16-
super.traitCollectionDidChange(previousTraitCollection)
17-
// Border color is not automatically updated on trait collection changes and thus manually updated here.
18-
contentView.layer.borderColor = Colors.borderColor.cgColor
13+
observeInterfaceStyleChange()
1914
}
2015
}
2116

@@ -38,6 +33,13 @@ private extension AddProductImageCollectionViewCell {
3833
contentView.layer.borderColor = Colors.borderColor.cgColor
3934
contentView.layer.masksToBounds = Settings.maskToBounds
4035
}
36+
37+
func observeInterfaceStyleChange() {
38+
/// The border color is not automatically updated on trait collection changes so we observe for changes and update it manually.
39+
applyContentBorderColorOnInterfaceStyleChange {
40+
return Colors.borderColor
41+
}
42+
}
4143
}
4244

4345
/// Constants

WooCommerce/Classes/ViewRelated/Products/Cells/Product Images/Collection View Cells/ProductImageCollectionViewCell.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ final class ProductImageCollectionViewCell: UICollectionViewCell {
3535
configureImageView()
3636
configureCellAppearance()
3737
configureCoverTagView()
38-
}
39-
40-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
41-
super.traitCollectionDidChange(previousTraitCollection)
42-
// Border color is not automatically updated on trait collection changes and thus manually updated here.
43-
contentView.layer.borderColor = Colors.borderColor.cgColor
38+
observeInterfaceStyleChange()
4439
}
4540

4641
override func prepareForReuse() {
@@ -86,6 +81,13 @@ private extension ProductImageCollectionViewCell {
8681
contentView.topAnchor.constraint(equalTo: coverTagView.topAnchor, constant: -Constants.tagPadding),
8782
])
8883
}
84+
85+
func observeInterfaceStyleChange() {
86+
/// Border color is not automatically updated on trait collection changes and thus manually updated here.
87+
applyContentBorderColorOnInterfaceStyleChange {
88+
return Colors.borderColor
89+
}
90+
}
8991
}
9092

9193
/// Constants

WooCommerce/Classes/ViewRelated/Products/Cells/Product Images/ProductImagesHeaderTableViewCell.swift

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class ProductImagesHeaderTableViewCell: UITableViewCell {
3636
configureBackground()
3737
configureSeparator()
3838
updateCollectionViewHeight()
39+
observeInterfaceTraitChanges()
3940
}
4041

4142
/// Configure cell
@@ -56,19 +57,6 @@ final class ProductImagesHeaderTableViewCell: UITableViewCell {
5657
}
5758
}
5859

59-
/// Rotation management and accessibility changes
60-
///
61-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
62-
super.traitCollectionDidChange(previousTraitCollection)
63-
if traitCollection != previousTraitCollection {
64-
// Update collection view height for accessibility changes
65-
updateCollectionViewHeight()
66-
// Invalidate layout when trait collection changes (including accessibility changes)
67-
collectionView.collectionViewLayout.invalidateLayout()
68-
collectionView.reloadData()
69-
}
70-
}
71-
7260
/// Updates the collection view height based on current accessibility settings
7361
private func updateCollectionViewHeight() {
7462
let cellSize = ProductImagesHeaderViewModel.cellSize(for: traitCollection.preferredContentSizeCategory)
@@ -166,6 +154,29 @@ private extension ProductImagesHeaderTableViewCell {
166154
collectionView.collectionViewLayout = ProductImagesFlowLayout(itemSize: dynamicSize, config: config)
167155
}
168156
}
157+
158+
/// Rotation management and accessibility changes
159+
///
160+
func observeInterfaceTraitChanges() {
161+
let traits: [UITrait] = [
162+
UITraitPreferredContentSizeCategory.self,
163+
UITraitHorizontalSizeClass.self,
164+
UITraitVerticalSizeClass.self,
165+
UITraitUserInterfaceStyle.self,
166+
UITraitAccessibilityContrast.self
167+
]
168+
registerForTraitChanges(traits) { (self: Self, previousTraitCollection: UITraitCollection) in
169+
guard self.traitCollection != previousTraitCollection else {
170+
return
171+
}
172+
173+
/// Update collection view height for accessibility changes
174+
self.updateCollectionViewHeight()
175+
/// Invalidate layout when trait collection changes (including accessibility changes)
176+
self.collectionView.collectionViewLayout.invalidateLayout()
177+
self.collectionView.reloadData()
178+
}
179+
}
169180
}
170181

171182
private extension ProductImagesHeaderTableViewCell {

WooCommerce/Classes/ViewRelated/Products/Cells/ProductsTabProductTableViewCell.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class ProductsTabProductTableViewCell: UITableViewCell {
3939
configureDetailsLabel()
4040
configureProductImageView()
4141
configureBottomBorderView()
42+
observeInterfaceTraitChanges()
4243
// From iOS 15.0, a focus effect will be applied automatically to a selected cell
4344
// modifying its style (e.g: by adding a border)
4445
focusEffect = nil
@@ -48,12 +49,6 @@ final class ProductsTabProductTableViewCell: UITableViewCell {
4849
fatalError("init(coder:) has not been implemented")
4950
}
5051

51-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
52-
super.traitCollectionDidChange(previousTraitCollection)
53-
// Border color is not automatically updated on trait collection changes and thus manually updated here.
54-
productImageView.layer.borderColor = Colors.imageBorderColor.cgColor
55-
}
56-
5752
override func updateConfiguration(using state: UICellConfigurationState) {
5853
super.updateConfiguration(using: state)
5954
updateDefaultBackgroundConfiguration(using: state)
@@ -266,6 +261,17 @@ private extension ProductsTabProductTableViewCell {
266261
productImageView.addSubview(view)
267262
productImageView.pinSubviewToAllEdges(view)
268263
}
264+
265+
func observeInterfaceTraitChanges() {
266+
/// Border color is not automatically updated on trait collection changes and thus manually updated here.
267+
let traits: [UITrait] = [
268+
UITraitUserInterfaceStyle.self,
269+
UITraitAccessibilityContrast.self
270+
]
271+
registerForTraitChanges(traits) { (self: Self, _: UITraitCollection) in
272+
self.productImageView.layer.borderColor = Colors.imageBorderColor.cgColor
273+
}
274+
}
269275
}
270276

271277
/// Constants

WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewController.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ final class ProductFormViewController<ViewModel: ProductFormViewModelProtocol>:
168168
observeUpdateCTAVisibility()
169169
observeVariationsPriceChanges()
170170
observeUpdateBlazeEligibility()
171+
observeTraitChanges()
171172

172173
productImageStatusesSubscription = productImageActionHandler.addUpdateObserver(self) { [weak self] productImageStatuses in
173174
guard let self = self else {
@@ -203,12 +204,6 @@ final class ProductFormViewController<ViewModel: ProductFormViewModelProtocol>:
203204
return true
204205
}
205206

206-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
207-
super.traitCollectionDidChange(previousTraitCollection)
208-
209-
updateNavigationBarTitle()
210-
}
211-
212207
// MARK: - Navigation actions handling
213208

214209
override func shouldPopOnBackButton() -> Bool {
@@ -949,6 +944,16 @@ private extension ProductFormViewController {
949944
self?.displayImageUploadErrorAlert(error: error, for: asset)
950945
})
951946
}
947+
948+
func observeTraitChanges() {
949+
let traits: [UITrait] = [
950+
UITraitHorizontalSizeClass.self,
951+
UITraitVerticalSizeClass.self
952+
]
953+
registerForTraitChanges(traits) { (self: Self, _) in
954+
self.updateNavigationBarTitle()
955+
}
956+
}
952957
}
953958

954959
// MARK: More details actions

0 commit comments

Comments
 (0)