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

141 lines
3.8 KiB
Swift
Raw Normal View History

2025-07-23 21:58:07 +08:00
//
// MainFeedCollectionHeaderReusableView.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 12/07/2025.
// Copyright © 2025 Ranchero Software. All rights reserved.
//
import UIKit
2026-01-19 20:34:23 -08:00
import Account
2025-07-23 21:58:07 +08:00
2025-11-18 16:29:59 -08:00
@MainActor protocol MainFeedCollectionHeaderReusableViewDelegate: AnyObject {
2025-07-23 21:58:07 +08:00
func mainFeedCollectionHeaderReusableViewDidTapDisclosureIndicator(_ view: MainFeedCollectionHeaderReusableView)
}
2026-01-19 21:54:54 -08:00
enum SectionHeaderType {
case smartFeeds
case account(String) // accountID
}
2025-11-18 16:29:59 -08:00
final class MainFeedCollectionHeaderReusableView: UICollectionReusableView {
2025-07-23 21:58:07 +08:00
var delegate: MainFeedCollectionHeaderReusableViewDelegate?
2026-01-19 21:54:54 -08:00
var sectionHeaderType: SectionHeaderType?
2026-01-20 09:10:06 -08:00
2025-11-19 19:43:09 -08:00
@IBOutlet var headerTitle: UILabel!
@IBOutlet var disclosureIndicator: UIImageView!
@IBOutlet var unreadCountLabel: UILabel!
2025-07-23 21:58:07 +08:00
override var accessibilityLabel: String? {
get {
let name = headerTitle.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
}
private var expandedStateMessage: String {
2026-01-02 17:50:35 -08:00
if disclosureExpanded {
return NSLocalizedString("Expanded", comment: "Expanded")
2025-07-23 21:58:07 +08:00
}
return NSLocalizedString("Collapsed", comment: "Collapsed")
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
updateUnreadCount()
unreadCountLabel.setUnreadCount(newValue)
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)
}
// Mutate via setDisclosure(isExpanded:animated:) — the supplementary provider
// sets this on every dequeue and must not animate.
private(set) var disclosureExpanded = true
func setDisclosure(isExpanded: Bool, animated: Bool) {
disclosureExpanded = isExpanded
updateExpandedState(animate: animated)
updateUnreadCount(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 func awakeFromNib() {
2025-11-18 16:29:59 -08:00
MainActor.assumeIsolated {
super.awakeFromNib()
isAccessibilityElement = true
headerTitle.isAccessibilityElement = false
unreadCountLabel.isAccessibilityElement = false
disclosureIndicator.isAccessibilityElement = false
unreadCountLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 80).isActive = true
2025-11-18 16:29:59 -08:00
configureUI()
addTapGesture()
}
2025-07-23 21:58:07 +08:00
}
2025-11-05 11:22:10 -08:00
override func prepareForReuse() {
super.prepareForReuse()
2026-01-19 21:54:54 -08:00
sectionHeaderType = nil
let contextMenuInteractions = interactions.compactMap { $0 as? UIContextMenuInteraction }
for interaction in contextMenuInteractions {
removeInteraction(interaction)
}
}
2025-07-23 21:58:07 +08:00
func configureUI() {
headerTitle.textColor = traitCollection.userInterfaceIdiom == .pad ? .tertiaryLabel : .label
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
private func addTapGesture() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(containerHeaderTapped))
self.addGestureRecognizer(tapGesture)
self.isUserInteractionEnabled = true
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
@objc private func containerHeaderTapped() {
delegate?.mainFeedCollectionHeaderReusableViewDidTapDisclosureIndicator(self)
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
func configureContainer(withTitle title: String) {
headerTitle.text = title
disclosureIndicator.transform = .identity
}
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.disclosureIndicator.transform = transform
}
if animate {
UIView.animate(withDuration: 0.3, animations: animations)
} else {
animations()
}
}
2025-11-05 11:22:10 -08:00
func updateUnreadCount(animated: Bool = true) {
let alpha: CGFloat = (!disclosureExpanded && unreadCountText != nil) ? 1 : 0
if animated {
2025-07-23 21:58:07 +08:00
UIView.animate(withDuration: 0.3) {
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
}