Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions WooCommerce/Classes/Extensions/UICollectionViewCell+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ extension UICollectionViewCell {
func applyGrayBackgroundStyle() {
backgroundColor = .systemColor(.secondarySystemGroupedBackground)
}

func applyContentBorderColorOnInterfaceStyleChange(borderColor: @escaping () -> UIColor) {
let traits: [UITrait] = [
UITraitUserInterfaceStyle.self,
UITraitAccessibilityContrast.self
]
registerForTraitChanges(traits) { (self: Self, _: UITraitCollection) in
self.contentView.layer.borderColor = borderColor().cgColor
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ final class AddProductImageCollectionViewCell: UICollectionViewCell {
configureBackground()
configureImageView()
configureCellAppearance()
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// Border color is not automatically updated on trait collection changes and thus manually updated here.
contentView.layer.borderColor = Colors.borderColor.cgColor
observeInterfaceStyleChange()
}
}

Expand All @@ -38,6 +33,13 @@ private extension AddProductImageCollectionViewCell {
contentView.layer.borderColor = Colors.borderColor.cgColor
contentView.layer.masksToBounds = Settings.maskToBounds
}

func observeInterfaceStyleChange() {
/// The border color is not automatically updated on trait collection changes so we observe for changes and update it manually.
applyContentBorderColorOnInterfaceStyleChange {
return Colors.borderColor
}
}
}

/// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ final class ProductImageCollectionViewCell: UICollectionViewCell {
configureImageView()
configureCellAppearance()
configureCoverTagView()
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// Border color is not automatically updated on trait collection changes and thus manually updated here.
contentView.layer.borderColor = Colors.borderColor.cgColor
observeInterfaceStyleChange()
}

override func prepareForReuse() {
Expand Down Expand Up @@ -86,6 +81,13 @@ private extension ProductImageCollectionViewCell {
contentView.topAnchor.constraint(equalTo: coverTagView.topAnchor, constant: -Constants.tagPadding),
])
}

func observeInterfaceStyleChange() {
/// Border color is not automatically updated on trait collection changes and thus manually updated here.
applyContentBorderColorOnInterfaceStyleChange {
return Colors.borderColor
}
}
}

/// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class ProductImagesHeaderTableViewCell: UITableViewCell {
configureBackground()
configureSeparator()
updateCollectionViewHeight()
observeInterfaceTraitChanges()
}

/// Configure cell
Expand All @@ -56,19 +57,6 @@ final class ProductImagesHeaderTableViewCell: UITableViewCell {
}
}

/// Rotation management and accessibility changes
///
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection != previousTraitCollection {
// Update collection view height for accessibility changes
updateCollectionViewHeight()
// Invalidate layout when trait collection changes (including accessibility changes)
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
}
}

/// Updates the collection view height based on current accessibility settings
private func updateCollectionViewHeight() {
let cellSize = ProductImagesHeaderViewModel.cellSize(for: traitCollection.preferredContentSizeCategory)
Expand Down Expand Up @@ -166,6 +154,29 @@ private extension ProductImagesHeaderTableViewCell {
collectionView.collectionViewLayout = ProductImagesFlowLayout(itemSize: dynamicSize, config: config)
}
}

/// Rotation management and accessibility changes
///
func observeInterfaceTraitChanges() {
let traits: [UITrait] = [
UITraitPreferredContentSizeCategory.self,
UITraitHorizontalSizeClass.self,
UITraitVerticalSizeClass.self,
UITraitUserInterfaceStyle.self,
UITraitAccessibilityContrast.self
]
registerForTraitChanges(traits) { (self: Self, previousTraitCollection: UITraitCollection) in
guard self.traitCollection != previousTraitCollection else {
return
}

/// Update collection view height for accessibility changes
self.updateCollectionViewHeight()
/// Invalidate layout when trait collection changes (including accessibility changes)
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()
}
}
}

private extension ProductImagesHeaderTableViewCell {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class ProductsTabProductTableViewCell: UITableViewCell {
configureDetailsLabel()
configureProductImageView()
configureBottomBorderView()
observeInterfaceTraitChanges()
// From iOS 15.0, a focus effect will be applied automatically to a selected cell
// modifying its style (e.g: by adding a border)
focusEffect = nil
Expand All @@ -48,12 +49,6 @@ final class ProductsTabProductTableViewCell: UITableViewCell {
fatalError("init(coder:) has not been implemented")
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// Border color is not automatically updated on trait collection changes and thus manually updated here.
productImageView.layer.borderColor = Colors.imageBorderColor.cgColor
}

override func updateConfiguration(using state: UICellConfigurationState) {
super.updateConfiguration(using: state)
updateDefaultBackgroundConfiguration(using: state)
Expand Down Expand Up @@ -266,6 +261,17 @@ private extension ProductsTabProductTableViewCell {
productImageView.addSubview(view)
productImageView.pinSubviewToAllEdges(view)
}

func observeInterfaceTraitChanges() {
/// Border color is not automatically updated on trait collection changes and thus manually updated here.
let traits: [UITrait] = [
UITraitUserInterfaceStyle.self,
UITraitAccessibilityContrast.self
]
registerForTraitChanges(traits) { (self: Self, _: UITraitCollection) in
self.productImageView.layer.borderColor = Colors.imageBorderColor.cgColor
}
}
}

/// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ final class ProductFormViewController<ViewModel: ProductFormViewModelProtocol>:
observeUpdateCTAVisibility()
observeVariationsPriceChanges()
observeUpdateBlazeEligibility()
observeTraitChanges()

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

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

updateNavigationBarTitle()
}

// MARK: - Navigation actions handling

override func shouldPopOnBackButton() -> Bool {
Expand Down Expand Up @@ -949,6 +944,16 @@ private extension ProductFormViewController {
self?.displayImageUploadErrorAlert(error: error, for: asset)
})
}

func observeTraitChanges() {
let traits: [UITrait] = [
UITraitHorizontalSizeClass.self,
UITraitVerticalSizeClass.self
]
registerForTraitChanges(traits) { (self: Self, _) in
self.updateNavigationBarTitle()
}
}
}

// MARK: More details actions
Expand Down