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

116 lines
3.6 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-05-29 10:46:00 -07:00
import Images
2025-07-23 21:58:07 +08:00
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
faviconView.tintColor = iconImage?.preferredColor ?? 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 {
let name = feedTitle.text ?? ""
2025-07-23 21:58:07 +08:00
if unreadCount > 0 {
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
return "\(name) \(unreadCount) \(unreadLabel)"
2025-07-23 21:58:07 +08:00
} else {
return name
2025-07-23 21:58:07 +08:00
}
}
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()
isAccessibilityElement = true
feedTitle.isAccessibilityElement = false
unreadCountLabel.isAccessibilityElement = false
faviconView.isAccessibilityElement = false
2025-11-18 16:29:59 -08:00
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) {
2026-03-29 21:31:02 -07:00
var backgroundConfig: UIBackgroundConfiguration
if #available(iOS 18, *) {
backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state)
2026-03-31 10:43:52 -07:00
} else if traitCollection.userInterfaceIdiom == .pad {
backgroundConfig = UIBackgroundConfiguration.listSidebarCell().updated(for: state)
2026-03-29 21:31:02 -07:00
} else {
backgroundConfig = UIBackgroundConfiguration.listGroupedCell().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)
2026-01-30 13:43:40 -08:00
unreadCountLabel.textColor = Assets.Colors.primaryAccent
unreadCountLabel.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
2025-07-23 21:58:07 +08:00
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" {
faviconView.tintColor = iconImage?.preferredColor ?? Assets.Colors.secondaryAccent
2025-07-23 21:58:07 +08:00
}
}
}
self.backgroundConfiguration = backgroundConfig
}
}