Skip to content

Commit 03f2ff1

Browse files
authored
[WCiOS17] Use registerForTraitChanges in refund views (#16076)
2 parents 31699ac + 822f931 commit 03f2ff1

File tree

8 files changed

+87
-44
lines changed

8 files changed

+87
-44
lines changed

Modules/Sources/Networking/Model/POSProductVariation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public struct POSProductVariation: Codable, Equatable, GeneratedCopiable, Genera
9494
guard value.lowercased() == Values.manageStockParent else {
9595
let message = "Unexpected manage stock value: \(value)"
9696
assertionFailure(message)
97-
DDLogError(message)
97+
let formattedMessage = DDLogMessageFormat(stringLiteral: message)
98+
DDLogError(formattedMessage)
9899
return false
99100
}
100101
return false

Modules/Sources/Networking/Model/Product/ProductVariation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
273273
guard value.lowercased() == Values.manageStockParent else {
274274
let message = "Unexpected manage stock value: \(value)"
275275
assertionFailure(message)
276-
DDLogError(message)
276+
let formattedMessage = DDLogMessageFormat(stringLiteral: message)
277+
DDLogError(formattedMessage)
277278
return false
278279
}
279280
return false

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct POSFullScreenCoverModifier<CoverContent: View>: ViewModifier {
7777
.environmentObject(sheetManager)
7878
.environmentObject(coverManager)
7979
})
80-
.onChange(of: isPresented) { newValue in
80+
.onChange(of: isPresented) { _, newValue in
8181
parentCoverManager.isPresented = newValue
8282
}
8383
}
@@ -104,7 +104,7 @@ struct POSFullScreenCoverModifierForItem<Item: Identifiable & Equatable, CoverCo
104104
.environmentObject(sheetManager)
105105
.environmentObject(coverManager)
106106
})
107-
.onChange(of: item) { newValue in
107+
.onChange(of: item) { _, newValue in
108108
parentCoverManager.isPresented = newValue != nil
109109
}
110110
}

WooCommerce/Classes/ViewRelated/Orders/Cells/OrderTableViewCell.swift

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ final class OrderTableViewCell: UITableViewCell & SearchResultCell {
3131
///
3232
@IBOutlet weak var contentStackView: UIStackView!
3333

34+
/// Retains the content trait registration token, so it's not deallocated immediately
35+
///
36+
// periphery: ignore - False negative. Perhaps does not catch non-direct reads?
37+
private var contentSizeTraitRegistration: UITraitChangeRegistration?
38+
3439
static func register(for tableView: UITableView) {
3540
tableView.registerNib(for: self)
3641
}
@@ -75,17 +80,7 @@ final class OrderTableViewCell: UITableViewCell & SearchResultCell {
7580

7681
}
7782

78-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
79-
super.traitCollectionDidChange(previousTraitCollection)
80-
if traitCollection.preferredContentSizeCategory > .extraExtraLarge {
81-
contentStackView.axis = .vertical
82-
} else {
83-
contentStackView.axis = .horizontal
84-
}
85-
}
86-
8783
// MARK: - Overridden Methods
88-
8984
override func awakeFromNib() {
9085
super.awakeFromNib()
9186
configureBackground()
@@ -107,18 +102,40 @@ final class OrderTableViewCell: UITableViewCell & SearchResultCell {
107102
override func prepareForReuse() {
108103
super.prepareForReuse()
109104
paymentStatusLabel.layer.borderColor = UIColor.clear.cgColor
105+
contentSizeTraitRegistration = nil
110106
}
111107

112108
override func updateConfiguration(using state: UICellConfigurationState) {
113109
super.updateConfiguration(using: state)
114110
updateDefaultBackgroundConfiguration(using: state)
115111
}
116-
}
117112

113+
override func willMove(toSuperview newSuperview: UIView?) {
114+
super.willMove(toSuperview: newSuperview)
115+
116+
// Registers for trait changes when the cell is about to be added to view hierarchy,
117+
// applies initial layout, and cleans up when removed from hierarchy
118+
if newSuperview != nil {
119+
contentSizeTraitRegistration = registerForTraitChanges([
120+
UITraitPreferredContentSizeCategory.self
121+
]) { [weak self] (_: OrderTableViewCell, _: UITraitCollection) in
122+
self?.applyContentSizeCategoryLayout()
123+
}
124+
applyContentSizeCategoryLayout()
125+
} else {
126+
contentSizeTraitRegistration = nil
127+
}
128+
}
129+
}
118130

119-
// MARK: - Private
120-
//
121131
private extension OrderTableViewCell {
132+
func applyContentSizeCategoryLayout() {
133+
if traitCollection.preferredContentSizeCategory > .extraExtraLarge {
134+
contentStackView.axis = .vertical
135+
} else {
136+
contentStackView.axis = .horizontal
137+
}
138+
}
122139

123140
/// Reset the UI to a "no data" state.
124141
///

WooCommerce/Classes/ViewRelated/Orders/Order Details/Issue Refunds/Cells/RefundItemTableViewCell.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ final class RefundItemTableViewCell: UITableViewCell {
4343
applyAccessibilityChanges()
4444
}
4545

46-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
47-
super.traitCollectionDidChange(previousTraitCollection)
48-
applyAccessibilityChanges()
46+
override func willMove(toSuperview newSuperview: UIView?) {
47+
super.willMove(toSuperview: newSuperview)
48+
let sizeTraits: [UITrait] = [
49+
UITraitPreferredContentSizeCategory.self,
50+
UITraitUserInterfaceIdiom.self,
51+
UITraitVerticalSizeClass.self
52+
]
53+
54+
registerForTraitChanges(sizeTraits) { (self: Self, _: UITraitCollection) in
55+
self.applyAccessibilityChanges()
56+
}
4957
}
5058

5159
override func updateConfiguration(using state: UICellConfigurationState) {

WooCommerce/Classes/ViewRelated/Orders/Order Details/Issue Refunds/Cells/RefundShippingDetailsTableViewCell.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ final class RefundShippingDetailsTableViewCell: UITableViewCell {
5252
super.awakeFromNib()
5353
applyCellStyles()
5454
applyAccessibilityChanges()
55-
}
5655

57-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
58-
super.traitCollectionDidChange(previousTraitCollection)
59-
applyAccessibilityChanges()
56+
let traits: [UITrait] = [
57+
UITraitPreferredContentSizeCategory.self,
58+
UITraitUserInterfaceIdiom.self,
59+
UITraitVerticalSizeClass.self
60+
]
61+
62+
registerForTraitChanges(traits) { (self: Self, _: UITraitCollection) in
63+
self.applyAccessibilityChanges()
64+
}
6065
}
6166

6267
override func updateConfiguration(using state: UICellConfigurationState) {

WooCommerce/Classes/ViewRelated/Orders/Order Details/Issue Refunds/IssueRefundViewController.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ final class IssueRefundViewController: UIViewController {
4747
observeViewModel()
4848
viewModel.fetch()
4949
updateWithViewModelContent()
50+
51+
let traits: [UITrait] = [
52+
UITraitPreferredContentSizeCategory.self,
53+
UITraitUserInterfaceIdiom.self,
54+
UITraitVerticalSizeClass.self
55+
]
56+
57+
registerForTraitChanges(traits) {(self: Self, _: UITraitCollection) in
58+
self.configureHeaderStackView()
59+
self.tableView.updateHeaderHeight()
60+
}
5061
}
5162

5263
override func viewWillLayoutSubviews() {
@@ -194,16 +205,6 @@ private extension IssueRefundViewController {
194205
}
195206
}
196207

197-
// MARK: Accessibility handling
198-
//
199-
extension IssueRefundViewController {
200-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
201-
super.traitCollectionDidChange(previousTraitCollection)
202-
configureHeaderStackView()
203-
tableView.updateHeaderHeight()
204-
}
205-
}
206-
207208
// MARK: TableView Delegate & DataSource
208209
extension IssueRefundViewController: UITableViewDelegate, UITableViewDataSource {
209210

WooCommerce/Classes/ViewRelated/Orders/Order Filters/FilteredOrdersHeaderBar.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ final class FilteredOrdersHeaderBar: UIView {
1212

1313
private let bottomBorder = CALayer()
1414

15+
/// Retains the content trait registration token, so it's not deallocated immediately
16+
///
17+
// periphery: ignore - False negative. Perhaps does not catch non-direct reads?
18+
private var contentSizeTraitRegistration: UITraitChangeRegistration?
19+
1520
/// The number of filters applied
1621
///
1722
private var numberOfFilters = 0
@@ -30,6 +35,21 @@ final class FilteredOrdersHeaderBar: UIView {
3035
updateStackViewAxis(for: traitCollection)
3136
}
3237

38+
override func willMove(toSuperview newSuperview: UIView?) {
39+
super.willMove(toSuperview: newSuperview)
40+
41+
if newSuperview != nil {
42+
contentSizeTraitRegistration = registerForTraitChanges([
43+
UITraitPreferredContentSizeCategory.self
44+
]) { [weak self] (_: FilteredOrdersHeaderBar, _: UITraitCollection) in
45+
guard let self = self else { return }
46+
self.updateStackViewAxis(for: self.traitCollection)
47+
}
48+
} else {
49+
contentSizeTraitRegistration = nil
50+
}
51+
}
52+
3353
override func layoutSubviews() {
3454
super.layoutSubviews()
3555
adjustBorderOnFrameUpdate()
@@ -55,16 +75,6 @@ final class FilteredOrdersHeaderBar: UIView {
5575
/// The `Last updated: time` tends to get truncated at larger text sizes by the filter button.
5676
/// Laying out the overall stack view vertically avoids this with accessibility sizes.
5777
extension FilteredOrdersHeaderBar {
58-
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
59-
super.traitCollectionDidChange(previousTraitCollection)
60-
61-
guard let previousTraitCollection,
62-
traitCollection.preferredContentSizeCategory != previousTraitCollection.preferredContentSizeCategory else {
63-
return
64-
}
65-
updateStackViewAxis(for: traitCollection)
66-
}
67-
6878
private func updateStackViewAxis(for traitCollection: UITraitCollection) {
6979
if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
7080
headerBarLayoutStackView.axis = .vertical

0 commit comments

Comments
 (0)