From ff92ce46dbe03a899599c03198f4a4d4d78f6c55 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 7 Nov 2026 21:12:10 -0800 Subject: [PATCH] Add setting for feed name and title at top of timeline cell. --- Shared/Localizable.xcstrings | 3 + .../Cell/MainTimelineCellData.swift | 5 +- .../Cell/MainTimelineCellLayout.swift | 91 ++++++++++++++----- .../MainTimelineViewController.swift | 13 ++- ...neCustomizerCollectionViewController.swift | 54 +++++++++-- 5 files changed, 130 insertions(+), 36 deletions(-) diff --git a/Shared/Localizable.xcstrings b/Shared/Localizable.xcstrings index 9049f1d32..ccc4a8c9d 100644 --- a/Shared/Localizable.xcstrings +++ b/Shared/Localizable.xcstrings @@ -627,6 +627,9 @@ "Feed Inspector" : { "comment" : "Feed Inspector window title" }, + "Feed Name and Date Above Title" : { + "comment" : "Timeline Layout: switch that shows the feed name and date above the title in the timeline" + }, "Feeds" : { "comment" : "Feeds" }, diff --git a/iOS/MainTimeline/Cell/MainTimelineCellData.swift b/iOS/MainTimeline/Cell/MainTimelineCellData.swift index 0c336186c..9887f84a5 100644 --- a/iOS/MainTimeline/Cell/MainTimelineCellData.swift +++ b/iOS/MainTimeline/Cell/MainTimelineCellData.swift @@ -33,8 +33,9 @@ import Images let showThumbnail: Bool // Make space even when thumbnail is nil let thumbnailsDisplay: TimelineThumbnailsDisplay let fadesReadArticles: Bool + let feedNameAndDateAboveTitle: Bool - init(article: Article, showFeedName: ShowFeedName, feedName: String?, byline: String?, iconImage: IconImage?, showIcon: Bool, numberOfLines: Int, iconSize: IconSize, thumbnail: UIImage?, showThumbnail: Bool, thumbnailsDisplay: TimelineThumbnailsDisplay, fadesReadArticles: Bool) { + init(article: Article, showFeedName: ShowFeedName, feedName: String?, byline: String?, iconImage: IconImage?, showIcon: Bool, numberOfLines: Int, iconSize: IconSize, thumbnail: UIImage?, showThumbnail: Bool, thumbnailsDisplay: TimelineThumbnailsDisplay, fadesReadArticles: Bool, feedNameAndDateAboveTitle: Bool) { self.accountID = article.accountID self.articleID = article.articleID @@ -75,6 +76,7 @@ import Images self.showThumbnail = showThumbnail self.thumbnailsDisplay = thumbnailsDisplay self.fadesReadArticles = fadesReadArticles + self.feedNameAndDateAboveTitle = feedNameAndDateAboveTitle } init() { // Empty @@ -97,6 +99,7 @@ import Images self.showThumbnail = false self.thumbnailsDisplay = .none self.fadesReadArticles = false + self.feedNameAndDateAboveTitle = false } } diff --git a/iOS/MainTimeline/Cell/MainTimelineCellLayout.swift b/iOS/MainTimeline/Cell/MainTimelineCellLayout.swift index 73279b670..18ba71e1d 100644 --- a/iOS/MainTimeline/Cell/MainTimelineCellLayout.swift +++ b/iOS/MainTimeline/Cell/MainTimelineCellLayout.swift @@ -118,6 +118,8 @@ struct MainTimelineDefaultCellLayout: MainTimelineCellLayout { static var dateFont: UIFont { UIFont.preferredFont(forTextStyle: .footnote) } static let dateMarginBottom = CGFloat(1) + static let feedNameAndDateBottomMargin = CGFloat(2) // Above the title, when feedNameAndDateAboveTitle + static var summaryFont: UIFont { UIFont.preferredFont(forTextStyle: .body) } static let thumbnailDimension = CGFloat(76) @@ -143,8 +145,8 @@ struct MainTimelineDefaultCellLayout: MainTimelineCellLayout { currentPoint.x = Self.cellPadding.left + insets.left + Self.unreadCircleMarginLeft currentPoint.y = Self.cellPadding.top - self.unreadIndicatorRect = Self.rectForUnreadIndicator(currentPoint) - self.starRect = Self.rectForStar(currentPoint) + var unreadIndicatorRect = Self.rectForUnreadIndicator(currentPoint) + var starRect = Self.rectForStar(currentPoint) currentPoint.x += Self.unreadCircleDimension + Self.unreadCircleMarginRight @@ -161,6 +163,16 @@ struct MainTimelineDefaultCellLayout: MainTimelineCellLayout { let textAreaWidth = rightEdge - currentPoint.x - smallThumbnailSpace self.separatorRect = CGRect(x: currentPoint.x, y: 0, width: rightEdge - currentPoint.x, height: 0) + var feedNameRect = CGRect.zero + var dateRect = CGRect.zero + if cellData.feedNameAndDateAboveTitle { + (feedNameRect, dateRect) = Self.rectsForFeedNameAndDate(cellData, currentPoint, textAreaWidth) + currentPoint.y = [feedNameRect, dateRect].maxY() + Self.feedNameAndDateBottomMargin + // Centered on the feed name and date line, which is the first line. + unreadIndicatorRect.origin.y = floor(dateRect.midY - (unreadIndicatorRect.height / 2.0)) + starRect.origin.y = floor(dateRect.midY - (starRect.height / 2.0)) + } + let (titleRect, numberOfLinesForTitle) = Self.rectForTitle(cellData, currentPoint, textAreaWidth) self.titleRect = titleRect @@ -169,18 +181,23 @@ struct MainTimelineDefaultCellLayout: MainTimelineCellLayout { } self.summaryRect = Self.rectForSummary(cellData, currentPoint, textAreaWidth, numberOfLinesForTitle) - var y = [self.titleRect, self.summaryRect].maxY() - if y == 0 { - y = iconImageRect.origin.y + iconImageRect.height - let tmp = Self.rectForDate(cellData, currentPoint, textAreaWidth) - y -= tmp.height + if !cellData.feedNameAndDateAboveTitle { + var y = [self.titleRect, self.summaryRect].maxY() + if y == 0 { + y = iconImageRect.origin.y + iconImageRect.height + let tmp = Self.rectForDate(cellData, currentPoint, textAreaWidth) + y -= tmp.height + } + currentPoint.y = y + (feedNameRect, dateRect) = Self.rectsForFeedNameAndDate(cellData, currentPoint, textAreaWidth) } - currentPoint.y = y - self.dateRect = Self.rectForDate(cellData, currentPoint, textAreaWidth) + self.feedNameRect = feedNameRect + self.dateRect = dateRect + self.unreadIndicatorRect = unreadIndicatorRect + self.starRect = starRect - let feedNameWidth = textAreaWidth - (Self.feedRightMargin + self.dateRect.size.width) - self.feedNameRect = Self.rectForFeedName(cellData, currentPoint, feedNameWidth) + let textMaxY = [self.titleRect, self.summaryRect, self.feedNameRect, self.dateRect].maxY() if showsSmallThumbnail { // Top right. The cell grows if needed to fit it. @@ -188,13 +205,21 @@ struct MainTimelineDefaultCellLayout: MainTimelineCellLayout { } else if cellData.showThumbnail && cellData.thumbnailsDisplay == .large { // Below the text and icon, from the icon’s left edge (or the text’s) to the right padding. let originX = cellData.showIcon ? self.iconImageRect.minX : currentPoint.x - let originY = ceil(max([self.feedNameRect, self.dateRect].maxY(), self.iconImageRect.maxY) + Self.largeThumbnailMarginTop) + let originY = ceil(max(textMaxY, self.iconImageRect.maxY) + Self.largeThumbnailMarginTop) self.thumbnailRect = CGRect(x: originX, y: originY, width: rightEdge - originX, height: Self.largeThumbnailHeight) } else { self.thumbnailRect = CGRect.zero } - self.height = [self.iconImageRect, self.feedNameRect, self.dateRect, self.thumbnailRect].maxY() + Self.cellPadding.bottom + self.height = max(textMaxY, [self.iconImageRect, self.thumbnailRect].maxY()) + Self.cellPadding.bottom + } + + /// One line: the feed name at the left and the date at the right. + static func rectsForFeedNameAndDate(_ cellData: MainTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> (feedNameRect: CGRect, dateRect: CGRect) { + let dateRect = Self.rectForDate(cellData, point, textAreaWidth) + let feedNameWidth = textAreaWidth - (Self.feedRightMargin + dateRect.size.width) + let feedNameRect = Self.rectForFeedName(cellData, point, feedNameWidth) + return (feedNameRect, dateRect) } static func rectForDate(_ cellData: MainTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect { @@ -243,6 +268,13 @@ struct MainTimelineAccessibilityCellLayout: MainTimelineCellLayout { let textAreaWidth = width - (currentPoint.x + MainTimelineDefaultCellLayout.cellPadding.right + insets.right) self.separatorRect = CGRect(x: currentPoint.x, y: 0, width: textAreaWidth, height: 0) + var feedNameRect = CGRect.zero + var dateRect = CGRect.zero + if cellData.feedNameAndDateAboveTitle { + (feedNameRect, dateRect) = Self.rectsForFeedNameAndDate(cellData, currentPoint, textAreaWidth) + currentPoint.y = dateRect.maxY + MainTimelineDefaultCellLayout.feedNameAndDateBottomMargin + } + let (titleRect, numberOfLinesForTitle) = Self.rectForTitle(cellData, currentPoint, textAreaWidth) self.titleRect = titleRect @@ -251,20 +283,19 @@ struct MainTimelineAccessibilityCellLayout: MainTimelineCellLayout { } self.summaryRect = Self.rectForSummary(cellData, currentPoint, textAreaWidth, numberOfLinesForTitle) - currentPoint.y = [self.titleRect, self.summaryRect].maxY() - - if cellData.showFeedName != .none { - self.feedNameRect = Self.rectForFeedName(cellData, currentPoint, textAreaWidth) - currentPoint.y = self.feedNameRect.maxY - } else { - self.feedNameRect = CGRect.zero + if !cellData.feedNameAndDateAboveTitle { + currentPoint.y = [self.titleRect, self.summaryRect].maxY() + (feedNameRect, dateRect) = Self.rectsForFeedNameAndDate(cellData, currentPoint, textAreaWidth) } - self.dateRect = Self.rectForDate(cellData, currentPoint, textAreaWidth) + self.feedNameRect = feedNameRect + self.dateRect = dateRect - // Below the date, at the text’s left edge. Large spans the text area. + let textMaxY = [self.titleRect, self.summaryRect, self.feedNameRect, self.dateRect].maxY() + + // Below the text, at the text’s left edge. Large spans the text area. if cellData.showThumbnail && cellData.thumbnailsDisplay != .none { - let originY = ceil(self.dateRect.maxY + MainTimelineDefaultCellLayout.largeThumbnailMarginTop) + let originY = ceil(textMaxY + MainTimelineDefaultCellLayout.largeThumbnailMarginTop) if cellData.thumbnailsDisplay == .large { self.thumbnailRect = CGRect(x: currentPoint.x, y: originY, width: textAreaWidth, height: MainTimelineDefaultCellLayout.largeThumbnailHeight) } else { @@ -274,7 +305,19 @@ struct MainTimelineAccessibilityCellLayout: MainTimelineCellLayout { self.thumbnailRect = CGRect.zero } - self.height = [self.dateRect, self.thumbnailRect].maxY() + MainTimelineDefaultCellLayout.cellPadding.bottom + self.height = max(textMaxY, self.thumbnailRect.maxY) + MainTimelineDefaultCellLayout.cellPadding.bottom + } + + /// Two lines: the feed name, when shown, and then the date. + static func rectsForFeedNameAndDate(_ cellData: MainTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> (feedNameRect: CGRect, dateRect: CGRect) { + var datePoint = point + var feedNameRect = CGRect.zero + if cellData.showFeedName != .none { + feedNameRect = Self.rectForFeedName(cellData, point, textAreaWidth) + datePoint.y = feedNameRect.maxY + } + let dateRect = Self.rectForDate(cellData, datePoint, textAreaWidth) + return (feedNameRect, dateRect) } static func rectForDate(_ cellData: MainTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect { diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index 40ef146c9..f9e699b6a 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -23,6 +23,7 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner private var iconSize = IconSize.medium private var thumbnailsDisplay = TimelineThumbnailsDisplay.none private var fadesReadArticles = false + private var feedNameAndDateAboveTitle = false private var articleIDsToReload = Set() private lazy var feedTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showFeedInspector(_:))) private lazy var filterButton = UIBarButtonItem(image: Assets.Images.filter, style: .plain, target: self, action: #selector(toggleFilter(_:))) @@ -191,6 +192,7 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner iconSize = AppDefaults.timelineIconSize thumbnailsDisplay = AppDefaults.timelineThumbnailsDisplay fadesReadArticles = AppDefaults.timelineFadeReadArticles + feedNameAndDateAboveTitle = AppDefaults.timelineFeedNameAndDateAboveTitle assert(collectionView?.refreshControl != nil) collectionView?.refreshControl = UIRefreshControl() @@ -760,6 +762,7 @@ private extension MainTimelineViewController { NotificationCenter.default.addObserver(self, selector: #selector(timelineNumberOfLinesDidChange(_:)), name: .timelineNumberOfLinesDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineThumbnailsDisplayDidChange(_:)), name: .timelineThumbnailsDisplayDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFadeReadArticlesDidChange(_:)), name: .timelineFadeReadArticlesDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFeedNameAndDateAboveTitleDidChange(_:)), name: .timelineFeedNameAndDateAboveTitleDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleThumbnailDidBecomeAvailable(_:)), name: .thumbnailDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleArticleThumbnailDownloadDidFail(_:)), name: .articleThumbnailDownloadDidFail, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleNetworkStatusDidChange(_:)), name: .networkStatusDidChange, object: nil) @@ -994,7 +997,7 @@ private extension MainTimelineViewController { showThumbnail = true thumbnail = ThumbnailDownloader.shared.thumbnail(url: thumbnailURL, mode: thumbnailMode, articleTitle: article.title) } - let cellData = MainTimelineCellData(article: article, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, byline: article.byline(), iconImage: iconImage, showIcon: showIcon, numberOfLines: numberOfTextLines, iconSize: iconSize, thumbnail: thumbnail, showThumbnail: showThumbnail, thumbnailsDisplay: thumbnailsDisplay, fadesReadArticles: fadesReadArticles) + let cellData = MainTimelineCellData(article: article, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, byline: article.byline(), iconImage: iconImage, showIcon: showIcon, numberOfLines: numberOfTextLines, iconSize: iconSize, thumbnail: thumbnail, showThumbnail: showThumbnail, thumbnailsDisplay: thumbnailsDisplay, fadesReadArticles: fadesReadArticles, feedNameAndDateAboveTitle: feedNameAndDateAboveTitle) return cellData } @@ -1195,6 +1198,14 @@ private extension MainTimelineViewController { reloadVisibleCells() } + @objc func handleTimelineFeedNameAndDateAboveTitleDidChange(_ notification: Notification) { + guard feedNameAndDateAboveTitle != AppDefaults.timelineFeedNameAndDateAboveTitle else { + return + } + feedNameAndDateAboveTitle = AppDefaults.timelineFeedNameAndDateAboveTitle + reloadAllCells() + } + @objc func handleThumbnailDidBecomeAvailable(_ notification: Notification) { guard let thumbnailMode, let collectionView, let dataSource, let url = notification.userInfo?[ThumbnailDownloader.urlKey] as? String else { return diff --git a/iOS/Settings/TimelineCustomizerCollectionViewController.swift b/iOS/Settings/TimelineCustomizerCollectionViewController.swift index 1ff1d9dba..0ed5c9475 100644 --- a/iOS/Settings/TimelineCustomizerCollectionViewController.swift +++ b/iOS/Settings/TimelineCustomizerCollectionViewController.swift @@ -36,6 +36,7 @@ class TimelineCustomizerCollectionViewController: UICollectionViewController { case feedIcons case thumbnails case fadeReadArticles + case feedNameAndDateAboveTitle } private var previewArticle: Article { @@ -71,7 +72,7 @@ class TimelineCustomizerCollectionViewController: UICollectionViewController { private var cachedIconSize: IconSize = AppDefaults.timelineIconSize // Created up front, since UIKit disallows creating a registration in cellForItemAt. - private let optionsCellRegistration = UICollectionView.CellRegistration { cell, _, optionsRow in + private let optionsCellRegistration = UICollectionView.CellRegistration { cell, _, optionsRow in TimelineCustomizerCollectionViewController.configureOptionsCell(cell, optionsRow: optionsRow) } @@ -97,6 +98,7 @@ class TimelineCustomizerCollectionViewController: UICollectionViewController { NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFeedIconsDisplayDidChange(_:)), name: .timelineFeedIconsDisplayDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineThumbnailsDisplayDidChange(_:)), name: .timelineThumbnailsDisplayDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFadeReadArticlesDidChange(_:)), name: .timelineFadeReadArticlesDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleTimelineFeedNameAndDateAboveTitleDidChange(_:)), name: .timelineFeedNameAndDateAboveTitleDidChange, object: nil) configureCollectionView() } @@ -223,6 +225,10 @@ class TimelineCustomizerCollectionViewController: UICollectionViewController { @objc func handleTimelineFadeReadArticlesDidChange(_ notification: Notification) { reloadOptionsRowsAndPreviews() } + + @objc func handleTimelineFeedNameAndDateAboveTitleDidChange(_ notification: Notification) { + reloadOptionsRowsAndPreviews() + } } // MARK: - Private @@ -279,12 +285,13 @@ private extension TimelineCustomizerCollectionViewController { thumbnail: Assets.Images.timelinePreviewThumbnail, showThumbnail: section.showsThumbnail, thumbnailsDisplay: AppDefaults.timelineThumbnailsDisplay, - fadesReadArticles: AppDefaults.timelineFadeReadArticles) + fadesReadArticles: AppDefaults.timelineFadeReadArticles, + feedNameAndDateAboveTitle: AppDefaults.timelineFeedNameAndDateAboveTitle) cell.isPreview = true return cell } - static private func configureOptionsCell(_ cell: UICollectionViewListCell, optionsRow: OptionsRow) { + static private func configureOptionsCell(_ cell: TimelineCustomizerOptionsCell, optionsRow: OptionsRow) { // The pop-up menu accessory shows only a chevron, so the value cell’s secondary text shows the current choice. var content = UIListContentConfiguration.valueCell() switch optionsRow { @@ -299,13 +306,15 @@ private extension TimelineCustomizerCollectionViewController { case .fadeReadArticles: content = UIListContentConfiguration.cell() content.text = NSLocalizedString("Fade Read Articles", comment: "Timeline Layout: switch that fades read articles in the timeline") - let fadeReadArticlesSwitch = UISwitch(frame: .zero, primaryAction: UIAction { action in - if let fadeReadArticlesSwitch = action.sender as? UISwitch { - AppDefaults.timelineFadeReadArticles = fadeReadArticlesSwitch.isOn - } - }) - fadeReadArticlesSwitch.isOn = AppDefaults.timelineFadeReadArticles - cell.accessories = [.customView(configuration: .init(customView: fadeReadArticlesSwitch, placement: .trailing()))] + configureSwitch(cell, isOn: AppDefaults.timelineFadeReadArticles) { isOn in + AppDefaults.timelineFadeReadArticles = isOn + } + case .feedNameAndDateAboveTitle: + content = UIListContentConfiguration.cell() + content.text = NSLocalizedString("Feed Name and Date Above Title", comment: "Timeline Layout: switch that shows the feed name and date above the title in the timeline") + configureSwitch(cell, isOn: AppDefaults.timelineFeedNameAndDateAboveTitle) { isOn in + AppDefaults.timelineFeedNameAndDateAboveTitle = isOn + } } cell.contentConfiguration = content @@ -318,6 +327,12 @@ private extension TimelineCustomizerCollectionViewController { } } + static func configureSwitch(_ cell: TimelineCustomizerOptionsCell, isOn: Bool, switchDidChange: @escaping (Bool) -> Void) { + cell.switchControl.isOn = isOn + cell.switchDidChange = switchDidChange + cell.accessories = [.customView(configuration: .init(customView: cell.switchControl, placement: .trailing()))] + } + static func timelineFeedIconsDisplayMenu() -> UIMenu { let currentFeedIconsDisplay = AppDefaults.timelineFeedIconsDisplay let actions = TimelineFeedIconsDisplay.allCases.map { feedIconsDisplay in @@ -351,3 +366,22 @@ private extension TimelineCustomizerCollectionViewController { } } } + +// MARK: - TimelineCustomizerOptionsCell + +/// Keeps one switch for the cell’s lifetime, since a new switch on every reconfigure animates into place. +final class TimelineCustomizerOptionsCell: UICollectionViewListCell { + + var switchDidChange: ((Bool) -> Void)? + + lazy var switchControl: UISwitch = { + let switchControl = UISwitch() + switchControl.addAction(UIAction { [weak self] _ in + guard let self else { + return + } + self.switchDidChange?(self.switchControl.isOn) + }, for: .valueChanged) + return switchControl + }() +}