Skip to content

Commit 31baa7e

Browse files
ivanvorobeiclaude
andcommitted
Extract shared cell dimming logic into DiffableTableViewCell base class
DiffableSubtitleTableViewCell and DiffableButtonTableViewCell now inherit from DiffableTableViewCell instead of UITableViewCell directly, removing duplicated image dimming, prepareForReuse and tintColorDidChange code. Changed reuseIdentifier from static to class var to support overrides. Extracted magic numbers into named constants. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e83ff95 commit 31baa7e

File tree

4 files changed

+19
-70
lines changed

4 files changed

+19
-70
lines changed
Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,41 @@
11
import UIKit
22

3-
open class DiffableButtonTableViewCell: UITableViewCell {
3+
open class DiffableButtonTableViewCell: DiffableTableViewCell {
44

5-
public static var reuseIdentifier: String { "DiffableButtonTableViewCell" }
5+
open override class var reuseIdentifier: String { "DiffableButtonTableViewCell" }
66

7-
private var originalImage: UIImage?
7+
private static let highlightAlpha: CGFloat = 0.6
8+
private static let highlightAnimationDuration: TimeInterval = 0.15
89

910
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
10-
super.init(style: style, reuseIdentifier: reuseIdentifier)
11+
super.init(cellStyle: style, reuseIdentifier: reuseIdentifier)
1112
selectionStyle = .none
1213
}
1314

1415
public required init?(coder: NSCoder) {
1516
super.init(coder: coder)
1617
}
1718

18-
open override func prepareForReuse() {
19-
super.prepareForReuse()
20-
originalImage = nil
21-
contentConfiguration = nil
22-
accessoryView = nil
23-
}
24-
25-
open override func tintColorDidChange() {
26-
super.tintColorDidChange()
27-
updateDimming()
28-
}
29-
30-
func updateDimming() {
19+
override func updateImageDimming() {
3120
guard var content = contentConfiguration as? UIListContentConfiguration else { return }
3221
let dimmed = tintAdjustmentMode == .dimmed
33-
3422
let color: UIColor = dimmed ? .secondaryLabel : tintColor
3523
if content.textProperties.color != color {
3624
content.textProperties.color = color
25+
contentConfiguration = content
3726
}
38-
39-
if let image = content.image {
40-
if dimmed {
41-
if image !== originalImage { originalImage = image }
42-
if let desaturated = originalImage?.desaturated() {
43-
content.image = desaturated
44-
}
45-
} else if let original = originalImage {
46-
content.image = original
47-
originalImage = nil
48-
}
49-
}
50-
51-
contentConfiguration = content
27+
super.updateImageDimming()
5228
}
5329

5430
open override func setHighlighted(_ highlighted: Bool, animated: Bool) {
5531
super.setHighlighted(highlighted, animated: animated)
56-
let alpha: CGFloat = highlighted ? 0.6 : 1
32+
let alpha: CGFloat = highlighted ? Self.highlightAlpha : 1
5733
if animated {
58-
UIView.animate(withDuration: 0.15) {
34+
UIView.animate(withDuration: Self.highlightAnimationDuration) {
5935
self.contentView.subviews.forEach { $0.alpha = alpha }
6036
}
6137
} else {
6238
contentView.subviews.forEach { $0.alpha = alpha }
6339
}
6440
}
65-
6641
}
Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
11
import UIKit
22

3-
open class DiffableSubtitleTableViewCell: UITableViewCell {
3+
open class DiffableSubtitleTableViewCell: DiffableTableViewCell {
44

5-
public static var reuseIdentifier: String { "DiffableSubtitleTableViewCell" }
6-
7-
private var originalImage: UIImage?
5+
open override class var reuseIdentifier: String { "DiffableSubtitleTableViewCell" }
86

97
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
10-
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
8+
super.init(cellStyle: .subtitle, reuseIdentifier: reuseIdentifier)
119
}
1210

1311
public required init?(coder: NSCoder) {
1412
super.init(coder: coder)
1513
}
16-
17-
open override func prepareForReuse() {
18-
super.prepareForReuse()
19-
originalImage = nil
20-
contentConfiguration = nil
21-
accessoryView = nil
22-
}
23-
24-
open override func tintColorDidChange() {
25-
super.tintColorDidChange()
26-
updateImageDimming()
27-
}
28-
29-
func updateImageDimming() {
30-
guard var content = contentConfiguration as? UIListContentConfiguration else { return }
31-
let dimmed = tintAdjustmentMode == .dimmed
32-
if dimmed {
33-
let currentImage = content.image
34-
if currentImage !== originalImage { originalImage = currentImage }
35-
guard let desaturated = originalImage?.desaturated() else { return }
36-
content.image = desaturated
37-
} else {
38-
guard let original = originalImage else { return }
39-
content.image = original
40-
originalImage = nil
41-
}
42-
contentConfiguration = content
43-
}
4414
}

Sources/DiffableKit/Table/Cells/DiffableTableViewCell.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import UIKit
22

33
open class DiffableTableViewCell: UITableViewCell {
44

5-
public static var reuseIdentifier: String { "DiffableTableViewCell" }
5+
open class var reuseIdentifier: String { "DiffableTableViewCell" }
66

77
private var originalImage: UIImage?
88

99
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
1010
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
1111
}
1212

13+
internal init(cellStyle: UITableViewCell.CellStyle, reuseIdentifier: String?) {
14+
super.init(style: cellStyle, reuseIdentifier: reuseIdentifier)
15+
}
16+
1317
public required init?(coder: NSCoder) {
1418
super.init(coder: coder)
1519
}

Sources/DiffableKit/Table/DataSource/DiffableTableDataSource+CellProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extension DiffableTableDataSource {
8787
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
8888
content.textProperties.font = UIFont.systemFont(ofSize: descriptor.pointSize, weight: .medium)
8989
cell.contentConfiguration = content
90-
cell.updateDimming()
90+
cell.updateImageDimming()
9191
cell.accessoryType = item.accessoryType
9292
return cell
9393
}

0 commit comments

Comments
 (0)