mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 08:17:16 +00:00
180 lines
5.2 KiB
Swift
180 lines
5.2 KiB
Swift
//
|
|
// MainFeedCollectionViewFolderCell.swift
|
|
// NetNewsWire-iOS
|
|
//
|
|
// Created by Stuart Breckenridge on 14/07/2025.
|
|
// Copyright © 2025 Ranchero Software. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Images
|
|
|
|
@MainActor protocol MainFeedCollectionViewFolderCellDelegate: AnyObject {
|
|
func mainFeedCollectionFolderViewCellDisclosureDidToggle(_ sender: MainFeedCollectionViewFolderCell, expanding: Bool)
|
|
}
|
|
|
|
class MainFeedCollectionViewFolderCell: UICollectionViewCell {
|
|
@IBOutlet var folderTitle: UILabel!
|
|
@IBOutlet var faviconView: IconView!
|
|
@IBOutlet var unreadCountLabel: UILabel!
|
|
@IBOutlet var disclosureButton: UIButton!
|
|
|
|
var delegate: MainFeedCollectionViewFolderCellDelegate?
|
|
|
|
private var _unreadCount: Int = 0
|
|
var unreadCount: Int {
|
|
get {
|
|
return _unreadCount
|
|
}
|
|
set {
|
|
_unreadCount = newValue
|
|
if newValue == 0 {
|
|
unreadCountLabel.isHidden = true
|
|
} else {
|
|
unreadCountLabel.isHidden = false
|
|
updateUnreadCountVisibility()
|
|
}
|
|
unreadCountLabel.text = newValue.formatted()
|
|
}
|
|
}
|
|
|
|
var iconImage: IconImage? {
|
|
didSet {
|
|
faviconView.iconImage = iconImage
|
|
if let preferredColor = iconImage?.preferredColor {
|
|
faviconView.tintColor = UIColor(cgColor: preferredColor)
|
|
} else {
|
|
faviconView.tintColor = Assets.Colors.secondaryAccent
|
|
}
|
|
}
|
|
}
|
|
|
|
var disclosureExpanded = true {
|
|
didSet {
|
|
updateExpandedState(animate: true)
|
|
updateUnreadCountVisibility()
|
|
}
|
|
}
|
|
|
|
override func awakeFromNib() {
|
|
MainActor.assumeIsolated {
|
|
super.awakeFromNib()
|
|
isAccessibilityElement = true
|
|
folderTitle.isAccessibilityElement = false
|
|
unreadCountLabel.isAccessibilityElement = false
|
|
faviconView.isAccessibilityElement = false
|
|
disclosureButton.isAccessibilityElement = false
|
|
disclosureButton.addInteraction(UIPointerInteraction())
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
func updateUnreadCountVisibility() {
|
|
if !disclosureExpanded && unreadCount > 0 {
|
|
UIView.animate {
|
|
self.unreadCountLabel.alpha = 1
|
|
}
|
|
} else {
|
|
UIView.animate {
|
|
self.unreadCountLabel.alpha = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction
|
|
func toggleDisclosure() {
|
|
setDisclosure(isExpanded: !disclosureExpanded, animated: true)
|
|
delegate?.mainFeedCollectionFolderViewCellDisclosureDidToggle(self, expanding: disclosureExpanded)
|
|
}
|
|
|
|
func setDisclosure(isExpanded: Bool, animated: Bool) {
|
|
disclosureExpanded = isExpanded
|
|
}
|
|
|
|
override var accessibilityLabel: String? {
|
|
get {
|
|
let name = folderTitle.text ?? ""
|
|
if unreadCount > 0 {
|
|
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
|
|
return "\(name) \(unreadCount) \(unreadLabel) \(expandedStateMessage)"
|
|
} else {
|
|
return "\(name) \(expandedStateMessage)"
|
|
}
|
|
}
|
|
set {}
|
|
}
|
|
|
|
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 {}
|
|
}
|
|
|
|
override func updateConfiguration(using state: UICellConfigurationState) {
|
|
var backgroundConfig: UIBackgroundConfiguration
|
|
if #available(iOS 18, *) {
|
|
backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state)
|
|
} else if traitCollection.userInterfaceIdiom == .pad {
|
|
backgroundConfig = UIBackgroundConfiguration.listSidebarCell().updated(for: state)
|
|
} else {
|
|
backgroundConfig = UIBackgroundConfiguration.listGroupedCell().updated(for: state)
|
|
}
|
|
|
|
switch (state.isHighlighted || state.isSelected || state.isFocused, traitCollection.userInterfaceIdiom) {
|
|
case (true, .pad):
|
|
backgroundConfig.backgroundColor = .tertiarySystemFill
|
|
folderTitle.textColor = Assets.Colors.primaryAccent
|
|
folderTitle.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
|
|
unreadCountLabel.textColor = Assets.Colors.primaryAccent
|
|
unreadCountLabel.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
|
|
case (true, .phone):
|
|
backgroundConfig.backgroundColor = Assets.Colors.primaryAccent
|
|
folderTitle.textColor = .white
|
|
unreadCountLabel.textColor = .white
|
|
faviconView.tintColor = .white
|
|
default:
|
|
folderTitle.textColor = .label
|
|
faviconView.tintColor = Assets.Colors.primaryAccent
|
|
folderTitle.font = UIFont.preferredFont(forTextStyle: .body)
|
|
unreadCountLabel.textColor = .secondaryLabel
|
|
unreadCountLabel.font = UIFont.preferredFont(forTextStyle: .body)
|
|
}
|
|
|
|
if state.cellDropState == .targeted {
|
|
backgroundConfig.backgroundColor = .tertiarySystemFill
|
|
}
|
|
|
|
self.backgroundConfiguration = backgroundConfig
|
|
}
|
|
}
|