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

125 lines
3.4 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
2025-11-18 16:29:59 -08:00
@MainActor protocol MainFeedCollectionHeaderReusableViewDelegate: AnyObject {
2025-07-23 21:58:07 +08:00
func mainFeedCollectionHeaderReusableViewDidTapDisclosureIndicator(_ view: MainFeedCollectionHeaderReusableView)
}
2025-11-18 16:29:59 -08:00
final class MainFeedCollectionHeaderReusableView: UICollectionReusableView {
2025-07-23 21:58:07 +08:00
var delegate: MainFeedCollectionHeaderReusableViewDelegate?
2025-11-05 11:22:10 -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
private var unreadLabelWidthConstraint: NSLayoutConstraint?
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 "\(headerTitle.text ?? "") \(unreadCount) \(unreadLabel) \(expandedStateMessage) "
} else {
return "\(headerTitle.text ?? "") \(expandedStateMessage) "
}
}
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: "Disclosure button expanded state for accessibility")
2025-07-23 21:58:07 +08:00
}
2026-01-02 17:50:35 -08:00
return NSLocalizedString("Collapsed", comment: "Disclosure button collapsed state for accessibility")
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.text = newValue.formatted()
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
var disclosureExpanded = true {
didSet {
updateExpandedState(animate: true)
updateUnreadCount()
}
}
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()
unreadLabelWidthConstraint = unreadCountLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 80)
unreadLabelWidthConstraint?.isActive = true
configureUI()
addTapGesture()
}
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 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) {
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
if disclosureExpanded == false {
unreadLabelWidthConstraint = unreadCountLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 80)
} else {
unreadLabelWidthConstraint = unreadCountLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 0)
unreadLabelWidthConstraint?.isActive = false
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
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
2025-07-23 21:58:07 +08:00
func updateUnreadCount() {
if !disclosureExpanded && unreadCount > 0 {
UIView.animate(withDuration: 0.3) {
self.unreadCountLabel.alpha = 1
}
} else {
UIView.animate(withDuration: 0.3) {
self.unreadCountLabel.alpha = 0
}
}
}
2025-11-05 11:22:10 -08:00
2025-07-23 21:58:07 +08:00
}