2026-01-27 22:12:31 +08:00
//
// TimelineCustomizerCollectionViewController.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 27/01/2026.
// Copyright © 2026 Ranchero Software. All rights reserved.
//
import UIKit
import Articles
2026-05-29 10:46:00 -07:00
import Images
2026-01-27 22:12:31 +08:00
class TimelineCustomizerCollectionViewController : UICollectionViewController {
2026-10-26 10:11:55 -07:00
private enum Section : Int , CaseIterable {
case iconSize
case numberOfLines
case options
case noIconPreview
case iconPreview
}
private enum OptionsRow : Int , CaseIterable {
2026-10-26 11:36:44 -07:00
case feedIcons
2026-10-26 10:11:55 -07:00
case thumbnails
}
2026-01-27 22:12:31 +08:00
private var previewArticle : Article {
var components = DateComponents ()
components . year = 1954
components . month = 7
components . day = 29
let calendar = Calendar . current
let date = calendar . date ( from : components ) !
return Article ( accountID : "_testID" ,
articleID : "_testArticleID" ,
feedID : "_testFeedID" ,
uniqueID : UUID (). uuidString ,
title : "At The Sign Of The Prancing Pony" ,
contentHTML : nil ,
2026-03-31 10:59:21 -07:00
contentText : "Bree was the chief village of Bree-land, a small country a few miles broad whose chief claim to fame was its aluminum siding industry. The Men of Bree were cheerful and independent: they belonged to nobody but themselves. In the lands beyond Bree there were mysterious wanderers." ,
2026-01-27 22:12:31 +08:00
markdown : nil ,
url : nil ,
externalURL : nil ,
summary : nil ,
imageURL : nil ,
2026-10-23 12:11:23 -07:00
thumbnailURL : nil ,
2026-01-27 22:12:31 +08:00
datePublished : date ,
dateModified : nil ,
authors : Set ([ Author ( authorID : "_testAuthorID" , name : "J. R. R. Tolkien" , url : nil , avatarURL : nil , emailAddress : nil ) ! ]),
2026-10-23 08:46:30 -07:00
attachments : nil ,
2026-01-27 22:12:31 +08:00
status : ArticleStatus ( articleID : "_testArticleID" , read : false , starred : false , dateArrived : . now ))
}
2026-01-31 21:04:11 -08:00
2026-10-22 13:46:32 -07:00
private var cachedTimelineLines : Int = AppDefaults . timelineNumberOfLines
private var cachedIconSize : IconSize = AppDefaults . timelineIconSize
2026-01-27 22:12:31 +08:00
2026-10-26 10:11:55 -07:00
// Created up front, since UIKit disallows creating a registration in cellForItemAt.
private let optionsCellRegistration = UICollectionView . CellRegistration < UICollectionViewListCell , OptionsRow > { cell , _ , optionsRow in
TimelineCustomizerCollectionViewController . configureOptionsCell ( cell , optionsRow : optionsRow )
}
override func viewDidLoad () {
super . viewDidLoad ()
2026-03-31 12:14:16 -07:00
title = NSLocalizedString ( "Timeline Layout" , comment : "Timeline Layout" )
2026-01-27 22:12:31 +08:00
NotificationCenter . default . addObserver ( forName : UserDefaults . didChangeNotification , object : nil , queue : . main ) { [ weak self ] _ in
guard let self = self else { return }
Task { @ MainActor in
2026-01-31 21:04:11 -08:00
2026-10-22 13:46:32 -07:00
if AppDefaults . timelineNumberOfLines != self . cachedTimelineLines {
self . cachedTimelineLines = AppDefaults . timelineNumberOfLines
2026-01-27 22:12:31 +08:00
self . userDefaultsDidChange ()
}
2026-01-31 21:04:11 -08:00
2026-10-22 13:46:32 -07:00
if AppDefaults . timelineIconSize != self . cachedIconSize {
self . cachedIconSize = AppDefaults . timelineIconSize
2026-01-27 22:12:31 +08:00
self . userDefaultsDidChange ()
}
}
}
2026-10-26 11:36:44 -07:00
NotificationCenter . default . addObserver ( self , selector : #selector ( handleTimelineFeedIconsDisplayDidChange ( _ :)), name : . timelineFeedIconsDisplayDidChange , object : nil )
2026-10-26 10:11:55 -07:00
NotificationCenter . default . addObserver ( self , selector : #selector ( handleTimelineThumbnailsDisplayDidChange ( _ :)), name : . timelineThumbnailsDisplayDidChange , object : nil )
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
configureCollectionView ()
2026-10-26 10:11:55 -07:00
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
private func configureCollectionView () {
collectionView . register (
TimelineHeaderView . self ,
forSupplementaryViewOfKind : UICollectionView . elementKindSectionHeader ,
withReuseIdentifier : TimelineHeaderView . reuseIdentifier
)
2026-01-31 21:04:11 -08:00
2026-06-22 10:55:38 -07:00
collectionView . register ( MainTimelineCell . self , forCellWithReuseIdentifier : MainTimelineCell . reuseIdentifier )
2026-10-26 12:16:44 -07:00
let layout = UICollectionViewCompositionalLayout { sectionIndex , layoutEnvironment in
var config = UICollectionLayoutListConfiguration ( appearance : . insetGrouped )
config . showsSeparators = false
let isHidden = Self . isHidden ( Section ( rawValue : sectionIndex ))
config . headerMode = isHidden ? . none : . supplementary
let section = NSCollectionLayoutSection . list ( using : config , layoutEnvironment : layoutEnvironment )
if isHidden {
// An empty section still has insets, which would leave a gap.
section . contentInsets = . zero
}
return section
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
collectionView . setCollectionViewLayout ( layout , animated : false )
}
2026-01-31 21:04:11 -08:00
2026-10-26 10:11:55 -07:00
// MARK: UICollectionViewDataSource
2026-01-27 22:12:31 +08:00
2026-10-26 10:11:55 -07:00
override func numberOfSections ( in collectionView : UICollectionView ) -> Int {
Section . allCases . count
}
2026-01-27 22:12:31 +08:00
2026-10-26 10:11:55 -07:00
override func collectionView ( _ collectionView : UICollectionView , numberOfItemsInSection section : Int ) -> Int {
2026-10-26 12:16:44 -07:00
let section = Section ( rawValue : section )
if Self . isHidden ( section ) {
return 0
}
if section == . options {
2026-10-26 10:11:55 -07:00
return OptionsRow . allCases . count
}
return 1
}
2026-01-27 22:12:31 +08:00
2026-10-26 10:11:55 -07:00
override func collectionView ( _ collectionView : UICollectionView , cellForItemAt indexPath : IndexPath ) -> UICollectionViewCell {
2026-01-31 21:04:11 -08:00
2026-10-26 10:11:55 -07:00
switch Section ( rawValue : indexPath . section ) {
case . iconSize :
2026-01-27 22:12:31 +08:00
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : "IconSizeSelector" , for : indexPath ) as ! TimelineCustomizerCell
cell . sliderConfiguration = . iconSize
return cell
2026-10-26 10:11:55 -07:00
case . numberOfLines :
2026-01-27 22:12:31 +08:00
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : "NumberOfLinesSelector" , for : indexPath ) as ! TimelineCustomizerCell
cell . sliderConfiguration = . numberOfLines
return cell
2026-10-26 10:11:55 -07:00
case . options :
2026-10-26 11:36:44 -07:00
let optionsRow = OptionsRow ( rawValue : indexPath . item ) ?? . feedIcons
2026-10-26 10:11:55 -07:00
return collectionView . dequeueConfiguredReusableCell ( using : optionsCellRegistration , for : indexPath , item : optionsRow )
case . noIconPreview :
return previewCell ( showIcon : false , indexPath : indexPath )
case . iconPreview :
return previewCell ( showIcon : true , indexPath : indexPath )
case . none :
return collectionView . dequeueReusableCell ( withReuseIdentifier : MainTimelineCell . reuseIdentifier , for : indexPath )
2026-01-27 22:12:31 +08:00
}
2026-10-26 10:11:55 -07:00
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
// MARK: UICollectionViewDelegate
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
override func collectionView ( _ collectionView : UICollectionView , viewForSupplementaryElementOfKind kind : String , at indexPath : IndexPath
) -> UICollectionReusableView {
guard kind == UICollectionView . elementKindSectionHeader else {
return UICollectionReusableView ()
}
let header = collectionView . dequeueReusableSupplementaryView (
ofKind : kind ,
withReuseIdentifier : TimelineHeaderView . reuseIdentifier ,
for : indexPath
) as ! TimelineHeaderView
2026-10-26 10:11:55 -07:00
switch Section ( rawValue : indexPath . section ) {
case . iconSize :
2026-01-27 22:12:31 +08:00
header . label . text = NSLocalizedString ( "Icon Size" , comment : "Icon Size" )
2026-10-26 10:11:55 -07:00
case . numberOfLines :
2026-01-27 22:12:31 +08:00
header . label . text = NSLocalizedString ( "Number of Lines" , comment : "Number of Lines" )
2026-10-26 10:11:55 -07:00
case . noIconPreview :
2026-10-26 12:16:44 -07:00
header . label . text = NSLocalizedString ( "Preview" , comment : "Preview" )
2026-10-26 10:11:55 -07:00
case . iconPreview :
2026-10-26 12:16:44 -07:00
header . label . text = Self . isHidden (. noIconPreview ) ? NSLocalizedString ( "Preview" , comment : "Preview" ) : NSLocalizedString ( "Preview with Feed Icon" , comment : "Timeline Layout: header for the preview that shows a feed icon" )
2026-10-26 10:11:55 -07:00
case . options , . none :
header . label . text = nil
2026-01-27 22:12:31 +08:00
}
return header
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
func collectionView ( _ collectionView : UICollectionView , layout collectionViewLayout : UICollectionViewLayout , referenceSizeForHeaderInSection section : Int
) -> CGSize {
CGSize ( width : collectionView . bounds . width , height : 50 )
}
2026-02-01 20:12:40 +08:00
2026-02-01 19:57:54 +08:00
override func collectionView ( _ collectionView : UICollectionView , shouldSelectItemAt indexPath : IndexPath ) -> Bool {
2026-10-26 10:11:55 -07:00
switch Section ( rawValue : indexPath . section ) {
case . iconSize , . numberOfLines :
return true
default :
2026-02-01 19:57:54 +08:00
return false
}
}
2026-01-27 22:12:31 +08:00
// MARK: Notifications
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
func userDefaultsDidChange () {
2026-10-26 10:11:55 -07:00
collectionView . reloadSections ([ Section . noIconPreview . rawValue , Section . iconPreview . rawValue ])
2026-01-27 22:12:31 +08:00
}
2026-10-26 11:36:44 -07:00
@objc func handleTimelineFeedIconsDisplayDidChange ( _ notification : Notification ) {
2026-10-26 12:16:44 -07:00
// One batch, since reconfiguring alone fails validation when a preview section’ s item count has changed.
collectionView . performBatchUpdates {
reconfigureOptionsRows ()
collectionView . reloadSections ([ Section . noIconPreview . rawValue , Section . iconPreview . rawValue ])
}
2026-10-26 11:36:44 -07:00
}
2026-10-26 10:11:55 -07:00
@objc func handleTimelineThumbnailsDisplayDidChange ( _ notification : Notification ) {
reconfigureOptionsRows ()
2026-10-26 12:57:43 -07:00
collectionView . reloadSections ([ Section . noIconPreview . rawValue , Section . iconPreview . rawValue ])
2026-10-26 10:11:55 -07:00
}
}
// MARK: - Private
private extension TimelineCustomizerCollectionViewController {
2026-10-26 12:16:44 -07:00
/// Hides a preview that the Feed Icons setting means would never be seen.
static private func isHidden ( _ section : Section ?) -> Bool {
switch section {
case . noIconPreview :
AppDefaults . timelineFeedIconsDisplay == . always
case . iconPreview :
AppDefaults . timelineFeedIconsDisplay == . never
default :
false
}
}
2026-10-26 10:11:55 -07:00
func previewCell ( showIcon : Bool , indexPath : IndexPath ) -> MainTimelineCell {
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : MainTimelineCell . reuseIdentifier , for : indexPath ) as ! MainTimelineCell
cell . cellData = MainTimelineCellData ( article : previewArticle ,
showFeedName : . byline ,
feedName : "The Fellowship of the Ring" ,
byline : "J. R. R. Tolkien" ,
iconImage : IconImage ( Assets . Images . nnwFeedIcon ),
showIcon : showIcon ,
numberOfLines : AppDefaults . timelineNumberOfLines ,
2026-10-26 12:57:43 -07:00
iconSize : AppDefaults . timelineIconSize ,
thumbnail : Assets . Images . nnwAppIcon ,
showThumbnail : AppDefaults . timelineThumbnailsDisplay != . none ,
thumbnailsDisplay : AppDefaults . timelineThumbnailsDisplay )
2026-10-26 10:11:55 -07:00
cell . isPreview = true
return cell
}
static private func configureOptionsCell ( _ cell : UICollectionViewListCell , optionsRow : OptionsRow ) {
2026-10-26 11:36:44 -07:00
// The pop-up menu accessory shows only a chevron, so the value cell’ s secondary text shows the current choice.
var content = UIListContentConfiguration . valueCell ()
2026-10-26 10:11:55 -07:00
switch optionsRow {
2026-10-26 11:36:44 -07:00
case . feedIcons :
content . text = NSLocalizedString ( "Feed Icons" , comment : "Feed Icons" )
content . secondaryText = AppDefaults . timelineFeedIconsDisplay . description
cell . accessories = [. popUpMenu ( timelineFeedIconsDisplayMenu ())]
2026-10-26 10:11:55 -07:00
case . thumbnails :
2026-10-26 11:36:44 -07:00
content . text = NSLocalizedString ( "Thumbnail Size" , comment : "Thumbnail Size" )
content . secondaryText = AppDefaults . timelineThumbnailsDisplay . description
2026-10-26 10:11:55 -07:00
cell . accessories = [. popUpMenu ( timelineThumbnailsDisplayMenu ())]
}
cell . contentConfiguration = content
// Matches TimelineCustomizerCell.
cell . configurationUpdateHandler = { cell , state in
var backgroundConfig = UIBackgroundConfiguration . listCell (). updated ( for : state )
backgroundConfig . backgroundColor = cell . traitCollection . userInterfaceStyle == . dark ? . secondarySystemBackground : . white
backgroundConfig . cornerRadius = 20
cell . backgroundConfiguration = backgroundConfig
}
}
2026-10-26 11:36:44 -07:00
static func timelineFeedIconsDisplayMenu () -> UIMenu {
let currentFeedIconsDisplay = AppDefaults . timelineFeedIconsDisplay
let actions = TimelineFeedIconsDisplay . allCases . map { feedIconsDisplay in
UIAction ( title : String ( describing : feedIconsDisplay ), subtitle : feedIconsDisplay . subtitle , state : feedIconsDisplay == currentFeedIconsDisplay ? . on : . off ) { _ in
AppDefaults . timelineFeedIconsDisplay = feedIconsDisplay
}
}
return UIMenu ( options : . singleSelection , children : actions )
}
2026-10-26 10:11:55 -07:00
static func timelineThumbnailsDisplayMenu () -> UIMenu {
let currentThumbnailsDisplay = AppDefaults . timelineThumbnailsDisplay
let actions = TimelineThumbnailsDisplay . allCases . map { thumbnailsDisplay in
UIAction ( title : String ( describing : thumbnailsDisplay ), state : thumbnailsDisplay == currentThumbnailsDisplay ? . on : . off ) { _ in
AppDefaults . timelineThumbnailsDisplay = thumbnailsDisplay
}
}
return UIMenu ( options : . singleSelection , children : actions )
}
func reconfigureOptionsRows () {
let indexPaths = OptionsRow . allCases . map { IndexPath ( item : $0 . rawValue , section : Section . options . rawValue ) }
collectionView . reconfigureItems ( at : indexPaths )
}
2026-01-27 22:12:31 +08:00
}