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

170 lines
4.9 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
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
if newValue == 0 {
unreadCountLabel.isHidden = true
} else {
unreadCountLabel.isHidden = false
2026-01-06 15:16:00 +08:00
updateUnreadCountVisibility()
2025-07-23 21:58:07 +08:00
}
unreadCountLabel.text = newValue.formatted()
}
}
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
var disclosureExpanded = true {
didSet {
updateExpandedState(animate: true)
2026-01-06 15:16:00 +08:00
updateUnreadCountVisibility()
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
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
2026-01-06 15:16:00 +08:00
func updateUnreadCountVisibility() {
if !disclosureExpanded && unreadCount > 0 {
UIView.animate {
2025-07-23 21:58:07 +08:00
self.unreadCountLabel.alpha = 1
}
} else {
2026-01-06 15:16:00 +08:00
UIView.animate {
2025-07-23 21:58:07 +08:00
self.unreadCountLabel.alpha = 0
}
}
}
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
}
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 ?? ""
2025-07-23 21:58:07 +08:00
if unreadCount > 0 {
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
return "\(name) \(unreadCount) \(unreadLabel) \(expandedStateMessage)"
2025-07-23 21:58:07 +08:00
} else {
return "\(name) \(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-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
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)
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)
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
}
}