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 {
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 ,
datePublished : date ,
dateModified : nil ,
authors : Set ([ Author ( authorID : "_testAuthorID" , name : "J. R. R. Tolkien" , url : nil , avatarURL : nil , emailAddress : nil ) ! ]),
status : ArticleStatus ( articleID : "_testArticleID" , read : false , starred : false , dateArrived : . now ))
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
private var cachedTimelineLines : Int = AppDefaults . shared . timelineNumberOfLines
private var cachedIconSize : IconSize = AppDefaults . shared . timelineIconSize
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-01-27 22:12:31 +08:00
if AppDefaults . shared . timelineNumberOfLines != self . cachedTimelineLines {
self . cachedTimelineLines = AppDefaults . shared . timelineNumberOfLines
self . userDefaultsDidChange ()
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
if AppDefaults . shared . timelineIconSize != self . cachedIconSize {
self . cachedIconSize = AppDefaults . shared . timelineIconSize
self . userDefaultsDidChange ()
}
}
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
configureCollectionView ()
}
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-01-27 22:12:31 +08:00
var config = UICollectionLayoutListConfiguration ( appearance : . insetGrouped )
config . showsSeparators = false
config . headerMode = . supplementary
let layout = UICollectionViewCompositionalLayout . list ( using : config )
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-01-27 22:12:31 +08:00
// MARK: UICollectionViewDataSource
override func numberOfSections ( in collectionView : UICollectionView ) -> Int {
return 4
}
override func collectionView ( _ collectionView : UICollectionView , numberOfItemsInSection section : Int ) -> Int {
// #warning Incomplete implementation, return the number of items
return 1
}
override func collectionView ( _ collectionView : UICollectionView , cellForItemAt indexPath : IndexPath ) -> UICollectionViewCell {
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
if indexPath . section == 0 {
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : "IconSizeSelector" , for : indexPath ) as ! TimelineCustomizerCell
cell . sliderConfiguration = . iconSize
return cell
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
if indexPath . section == 1 {
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : "NumberOfLinesSelector" , for : indexPath ) as ! TimelineCustomizerCell
cell . sliderConfiguration = . numberOfLines
return cell
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
if indexPath . section == 2 {
2026-06-22 10:55:38 -07:00
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : MainTimelineCell . reuseIdentifier , for : indexPath ) as ! MainTimelineCell
2026-01-27 22:12:31 +08:00
cell . cellData = MainTimelineCellData ( article : previewArticle ,
showFeedName : . byline ,
feedName : "The Fellowship of the Ring" ,
byline : "J. R. R. Tolkien" ,
iconImage : IconImage ( Assets . Images . nnwFeedIcon ),
showIcon : false ,
numberOfLines : AppDefaults . shared . timelineNumberOfLines ,
iconSize : AppDefaults . shared . timelineIconSize )
cell . isPreview = true
return cell
}
2026-01-31 21:04:11 -08:00
2026-01-27 22:12:31 +08:00
if indexPath . section == 3 {
2026-06-22 10:55:38 -07:00
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : MainTimelineCell . reuseIdentifier , for : indexPath ) as ! MainTimelineCell
2026-01-27 22:12:31 +08:00
cell . cellData = MainTimelineCellData ( article : previewArticle ,
showFeedName : . byline ,
feedName : "The Fellowship of the Ring" ,
byline : "J. R. R. Tolkien" ,
iconImage : IconImage ( Assets . Images . nnwFeedIcon ),
showIcon : true ,
numberOfLines : AppDefaults . shared . timelineNumberOfLines ,
iconSize : AppDefaults . shared . timelineIconSize )
cell . isPreview = true
return cell
}
2026-01-31 21:04:11 -08:00
2026-06-22 10:55:38 -07:00
let cell = collectionView . dequeueReusableCell ( withReuseIdentifier : MainTimelineCell . reuseIdentifier , for : indexPath )
2026-01-27 22:12:31 +08:00
return cell
}
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
switch indexPath . section {
case 0 :
header . label . text = NSLocalizedString ( "Icon Size" , comment : "Icon Size" )
case 1 :
header . label . text = NSLocalizedString ( "Number of Lines" , comment : "Number of Lines" )
case 2 :
header . label . text = NSLocalizedString ( "No Icon Preview" , comment : "No Icon Preview" )
case 3 :
header . label . text = NSLocalizedString ( "Icon Preview" , comment : "Icon Preview" )
default :
header . label . text = NSLocalizedString ( "" , comment : "" )
}
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 {
if indexPath . section > 1 {
return false
}
return true
}
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-01-31 21:04:11 -08:00
collectionView . reloadSections ([ 2 , 3 ])
2026-01-27 22:12:31 +08:00
}
}