Files
NetNewsWire/iOS/MainFeed/Collection View Cells/MainFeedCollectionViewCell.swift
T

117 lines
3.5 KiB
Swift
Raw Normal View History

2025-07-23 21:58:07 +08:00
//
// MainFeedCollectionViewCell.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 23/06/2025.
// Copyright © 2025 Ranchero Software. All rights reserved.
//
import UIKit
import RSCore
import Account
import RSTree
2026-01-01 17:55:12 -08:00
final class MainFeedCollectionViewCell: UICollectionViewCell {
2025-11-19 19:43:09 -08:00
@IBOutlet var feedTitle: UILabel!
@IBOutlet var faviconView: IconView!
@IBOutlet var unreadCountLabel: UILabel!
2025-07-23 21:58:07 +08:00
private var faviconLeadingConstraint: NSLayoutConstraint?
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
var iconImage: IconImage? {
didSet {
faviconView.iconImage = iconImage
if let preferredColor = iconImage?.preferredColor {
faviconView.tintColor = UIColor(cgColor: preferredColor)
} else {
2025-11-19 10:38:18 -08:00
faviconView.tintColor = Assets.Colors.secondaryAccent
2025-07-23 21:58:07 +08:00
}
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
private var _unreadCount: Int = 0
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
var unreadCount: Int {
get {
return _unreadCount
}
set {
_unreadCount = newValue
if newValue == 0 {
unreadCountLabel.isHidden = true
} else {
unreadCountLabel.isHidden = false
}
unreadCountLabel.text = newValue.formatted()
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
/// If the feed is contained in a folder, the indentation level is 1
/// and the cell's favicon leading constrain is increased. Otherwise,
/// it has the standard leading constraint.
///
/// On the storyboard, no leading constraint is set.
var indentationLevel: Int = 0 {
didSet {
if indentationLevel == 1 {
faviconLeadingConstraint?.constant = 32
} else {
faviconLeadingConstraint?.constant = 16
}
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
override var accessibilityLabel: String? {
get {
if unreadCount > 0 {
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
return "\(String(describing: feedTitle.text)) \(unreadCount) \(unreadLabel)"
} else {
return (String(describing: feedTitle.text))
}
}
2026-01-02 17:50:35 -08:00
set {}
2025-07-23 21:58:07 +08:00
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
override func awakeFromNib() {
2025-11-18 16:29:59 -08:00
MainActor.assumeIsolated {
super.awakeFromNib()
faviconLeadingConstraint = faviconView.leadingAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.leadingAnchor)
faviconLeadingConstraint?.isActive = true
}
2025-07-23 21:58:07 +08:00
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
override func updateConfiguration(using state: UICellConfigurationState) {
var backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state)
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
switch (state.isHighlighted || state.isSelected || state.isFocused, traitCollection.userInterfaceIdiom) {
case (true, .pad):
backgroundConfig.backgroundColor = .tertiarySystemFill
2025-11-19 10:38:18 -08:00
feedTitle.textColor = Assets.Colors.primaryAccent
feedTitle.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize,
weight: .semibold)
unreadCountLabel.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
2025-07-23 21:58:07 +08:00
case (true, .phone):
2025-11-19 10:38:18 -08:00
backgroundConfig.backgroundColor = Assets.Colors.primaryAccent
2025-07-23 21:58:07 +08:00
feedTitle.textColor = .white
2025-09-10 11:42:59 +08:00
unreadCountLabel.textColor = .secondaryLabel
2025-07-23 21:58:07 +08:00
if feedTitle.text == "All Unread" {
faviconView.tintColor = .white
}
default:
feedTitle.textColor = .label
2025-07-30 21:53:42 +08:00
feedTitle.font = UIFont.preferredFont(forTextStyle: .body)
unreadCountLabel.font = UIFont.preferredFont(forTextStyle: .body)
2025-07-23 21:58:07 +08:00
unreadCountLabel.textColor = .secondaryLabel
if traitCollection.userInterfaceIdiom == .phone {
if feedTitle.text == "All Unread" {
if let preferredColor = iconImage?.preferredColor {
faviconView.tintColor = UIColor(cgColor: preferredColor)
} else {
2025-11-19 10:38:18 -08:00
faviconView.tintColor = Assets.Colors.secondaryAccent
2025-07-23 21:58:07 +08:00
}
}
}
}
self.backgroundConfiguration = backgroundConfig
}
}