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

184 lines
6.1 KiB
Swift
Raw Normal View History

2025-07-23 21:58:07 +08:00
//
// MainFeedCollectionViewFolderCell.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 14/07/2025.
// Copyright © 2025 Ranchero Software. All rights reserved.
//
import UIKit
import RSCore
2026-05-29 10:46:00 -07:00
import Images
2025-07-23 21:58:07 +08:00
2025-11-18 16:39:51 -08:00
@MainActor protocol MainFeedCollectionViewFolderCellDelegate: AnyObject {
2025-07-23 21:58:07 +08:00
func mainFeedCollectionFolderViewCellDisclosureDidToggle(_ sender: MainFeedCollectionViewFolderCell, expanding: Bool)
}
class MainFeedCollectionViewFolderCell: UICollectionViewCell {
2025-11-19 19:43:09 -08:00
@IBOutlet var folderTitle: UILabel!
@IBOutlet var faviconView: IconView!
@IBOutlet var unreadCountLabel: UILabel!
@IBOutlet var disclosureButton: UIButton!
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
var delegate: MainFeedCollectionViewFolderCellDelegate?
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
private var _unreadCount: Int = 0
var unreadCount: Int {
get {
return _unreadCount
}
set {
_unreadCount = newValue
let unreadCountText = unreadCountText
unreadCountLabel.isHidden = unreadCountText == nil
if unreadCountText != nil {
2026-01-06 15:16:00 +08:00
updateUnreadCountVisibility()
2025-07-23 21:58:07 +08:00
}
unreadCountLabel.setUnreadCount(newValue)
setNeedsUpdateConfiguration()
2025-07-23 21:58:07 +08:00
}
}
2025-11-05 11:22:10 -08:00
private var unreadCountText: String? {
AppDefaults.unreadCountDisplay.text(for: unreadCount)
}
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
// Mutate via setDisclosure(isExpanded:animated:) so configure-time calls
// can skip animation — a 0.3s chevron spin during a diffable apply is wrong.
private(set) var disclosureExpanded = true
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
folderTitle.isAccessibilityElement = false
unreadCountLabel.isAccessibilityElement = false
faviconView.isAccessibilityElement = false
disclosureButton.isAccessibilityElement = false
2025-11-18 16:29:59 -08:00
disclosureButton.addInteraction(UIPointerInteraction())
}
2025-07-23 21:58:07 +08:00
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
func updateExpandedState(animate: Bool) {
let angle: CGFloat = disclosureExpanded ? 0 : -.pi / 2
let transform = CGAffineTransform(rotationAngle: angle)
let animations = {
self.disclosureButton.transform = transform
}
if animate {
UIView.animate(withDuration: 0.3, animations: animations)
} else {
animations()
}
}
2025-11-05 11:22:10 -08:00
func updateUnreadCountVisibility(animated: Bool = true) {
let alpha: CGFloat = (!disclosureExpanded && unreadCountText != nil) ? 1 : 0
if animated {
2026-01-06 15:16:00 +08:00
UIView.animate {
self.unreadCountLabel.alpha = alpha
2025-07-23 21:58:07 +08:00
}
} else {
unreadCountLabel.alpha = alpha
2025-07-23 21:58:07 +08:00
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
@IBAction
func toggleDisclosure() {
setDisclosure(isExpanded: !disclosureExpanded, animated: true)
delegate?.mainFeedCollectionFolderViewCellDisclosureDidToggle(self, expanding: disclosureExpanded)
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
func setDisclosure(isExpanded: Bool, animated: Bool) {
disclosureExpanded = isExpanded
updateExpandedState(animate: animated)
updateUnreadCountVisibility(animated: animated)
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 var accessibilityLabel: String? {
get {
let name = folderTitle.text ?? ""
guard let unreadText = AppDefaults.unreadCountDisplay.accessibilityText(for: unreadCount) else {
return "\(name) \(expandedStateMessage)"
2025-07-23 21:58:07 +08:00
}
return "\(name) \(unreadText) \(expandedStateMessage)"
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
private var expandedStateMessage: String {
if disclosureExpanded {
return NSLocalizedString("Expanded", comment: "Expanded")
}
return NSLocalizedString("Collapsed", comment: "Collapsed")
}
override var accessibilityCustomActions: [UIAccessibilityCustomAction]? {
get {
let name: String
if disclosureExpanded {
name = NSLocalizedString("Collapse", comment: "Collapse")
} else {
name = NSLocalizedString("Expand", comment: "Expand")
}
let toggleAction = UIAccessibilityCustomAction(name: name) { [weak self] _ in
self?.toggleDisclosure()
return true
}
return [toggleAction]
}
set {}
}
2025-07-23 21:58:07 +08:00
override func updateConfiguration(using state: UICellConfigurationState) {
2026-10-21 19:05:11 -07:00
var backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state)
2025-11-05 11:22:10 -08:00
// Matches the timeline: accent background and white text when the feeds list is first responder,
// and no highlight while a row is pressed, so the row goes straight to the selected style.
let isExpanded = isInExpandedSplitView
let isActiveSelection = state.isSelected && isExpanded && enclosingViewController?.isFirstResponder == true
let isHighlighted = state.isHighlighted && !isExpanded
if state.isHighlighted && !state.isSelected && isExpanded {
backgroundConfig.backgroundColor = .clear
}
switch (isHighlighted || state.isSelected || state.isFocused, traitCollection.userInterfaceIdiom) {
case _ where isActiveSelection:
backgroundConfig.backgroundColor = Assets.Colors.primaryAccent
folderTitle.textColor = .white
folderTitle.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
unreadCountLabel.textColor = .white
unreadCountLabel.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
faviconView.tintColor = .white
disclosureButton.configuration?.baseForegroundColor = .white
2025-07-23 21:58:07 +08:00
case (true, .pad):
backgroundConfig.backgroundColor = .tertiarySystemFill
2025-11-19 10:38:18 -08:00
folderTitle.textColor = Assets.Colors.primaryAccent
folderTitle.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)
faviconView.tintColor = Assets.Colors.primaryAccent
disclosureButton.configuration?.baseForegroundColor = .label
2025-07-23 21:58:07 +08:00
default:
folderTitle.textColor = .label
2025-11-19 10:38:18 -08:00
faviconView.tintColor = Assets.Colors.primaryAccent
2025-07-30 21:53:42 +08:00
folderTitle.font = UIFont.preferredFont(forTextStyle: .body)
2026-01-30 13:43:40 -08:00
unreadCountLabel.textColor = .secondaryLabel
unreadCountLabel.font = UIFont.preferredFont(forTextStyle: .body)
disclosureButton.configuration?.baseForegroundColor = .label
2025-07-23 21:58:07 +08:00
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
if state.cellDropState == .targeted {
backgroundConfig.backgroundColor = .tertiarySystemFill
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
self.backgroundConfiguration = backgroundConfig
}
}