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

139 lines
3.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
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
if let preferredColor = iconImage?.preferredColor {
faviconView.tintColor = UIColor(cgColor: preferredColor)
} else {
2025-11-19 10:38:18 -08:00
faviconView.tintColor = 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()
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 {
if unreadCount > 0 {
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
return "\(String(describing: folderTitle.text)) \(unreadCount) \(unreadLabel)"
} else {
return (String(describing: folderTitle.text))
}
}
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
2025-07-23 21:58:07 +08:00
override func updateConfiguration(using state: UICellConfigurationState) {
var backgroundConfig = UIBackgroundConfiguration.listCell().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)
unreadCountLabel.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .semibold)
2025-07-23 21:58:07 +08:00
case (true, .phone):
2025-11-19 10:38:18 -08:00
backgroundConfig.backgroundColor = Assets.Colors.primaryAccent
2025-07-23 21:58:07 +08:00
folderTitle.textColor = .white
2025-09-10 11:42:59 +08:00
unreadCountLabel.textColor = .secondaryLabel
2025-07-23 21:58:07 +08:00
faviconView.tintColor = .white
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)
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
}
}