Files
NetNewsWire/iOS/MainTimeline/MainTimelineNextUnreadFooterView.swift
T

102 lines
3.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// MainTimelineNextUnreadFooterView.swift
// NetNewsWire-iOS
//
// Created by Brent Simmons on 12/5/26.
//
import UIKit
/// After the last row of the timeline: a button that goes to the next feed or folder with unread articles.
/// When marking read on scroll, the footer is also the empty space that lets every row scroll under the navigation bar.
final class MainTimelineNextUnreadFooterView: UICollectionReusableView {
var action: (() -> Void)?
/// A focus ring, for when Down Arrow from the last article moves keyboard focus to the button.
var showsKeyboardFocus = false {
didSet {
button.configuration?.background.strokeColor = showsKeyboardFocus ? .tintColor : nil
button.configuration?.background.strokeWidth = showsKeyboardFocus ? Self.focusRingWidth : 0
}
}
/// The button’s frame in the footer’s coordinates.
var buttonFrame: CGRect {
button.frame
}
/// The footer is always tall enough for the button, and at least this tall.
var minimumHeight: CGFloat {
get {
minimumHeightConstraint.constant
}
set {
minimumHeightConstraint.constant = newValue
}
}
private let button = UIButton()
private lazy var minimumHeightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
private static let placeholderText = " "
private static let verticalPadding: CGFloat = 24.0
private static let horizontalPadding: CGFloat = 20.0
private static let buttonContentInsets = NSDirectionalEdgeInsets(top: 14.0, leading: 20.0, bottom: 14.0, trailing: 20.0)
private static let focusRingWidth: CGFloat = 2.0
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func configure(title: String, subtitle: String?) {
button.configuration?.title = title
button.configuration?.subtitle = subtitle
button.isHidden = false
}
/// The hidden button keeps placeholder text, so the footer is the same height with or without it.
func configureHidden(showSubtitle: Bool) {
button.configuration?.title = Self.placeholderText
button.configuration?.subtitle = showSubtitle ? Self.placeholderText : nil
button.isHidden = true
}
}
private extension MainTimelineNextUnreadFooterView {
func commonInit() {
// Flat, not glass — the button is part of the list, not the toolbar layer above it.
var configuration = UIButton.Configuration.gray()
configuration.baseForegroundColor = .label
configuration.cornerStyle = .large
configuration.contentInsets = Self.buttonContentInsets
configuration.titleAlignment = .center
configuration.titleLineBreakMode = .byTruncatingTail
configuration.subtitleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in
var attributes = attributes
attributes.foregroundColor = .secondaryLabel
return attributes
}
button.configuration = configuration
button.addAction(UIAction { [weak self] _ in
self?.action?()
}, for: .primaryActionTriggered)
button.translatesAutoresizingMaskIntoConstraints = false
addSubview(button)
NSLayoutConstraint.activate([
button.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Self.horizontalPadding),
button.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -Self.horizontalPadding),
button.topAnchor.constraint(equalTo: topAnchor, constant: Self.verticalPadding),
button.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -Self.verticalPadding)
])
minimumHeightConstraint.isActive = true
}
}