From fa39785e5ec5ef1ff84fa047833f66c11122c093 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Fri, 23 Jan 2026 08:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Fix=20wrapping=20and=20delete=20?= =?UTF-8?q?old=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iOS/MainTimeline/Cells/MainTimelineCell.swift | 3 +- .../Cells/MainTimelineFeedCell.swift | 266 ------------------ .../MainTimelineViewController.swift | 39 ++- 3 files changed, 28 insertions(+), 280 deletions(-) delete mode 100644 iOS/MainTimeline/Cells/MainTimelineFeedCell.swift diff --git a/iOS/MainTimeline/Cells/MainTimelineCell.swift b/iOS/MainTimeline/Cells/MainTimelineCell.swift index 4737bee12..114e7e2e6 100644 --- a/iOS/MainTimeline/Cells/MainTimelineCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineCell.swift @@ -130,7 +130,7 @@ class MainTimelineCell: UITableViewCell { ] let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes) - let linesUsed = countLines(of: titleAttributed, width: 343) + let linesUsed = countLines(of: titleAttributed, width: articleContent.bounds.width) // 2. Measure Title Height if linesUsed >= cellData.numberOfLines { // The title already fills 3 lines, set it and exit @@ -138,7 +138,6 @@ class MainTimelineCell: UITableViewCell { articleContent.lineBreakMode = .byTruncatingTail return } - print(linesUsed, articleContent.bounds.width) attributedCellText.append(titleAttributed) } diff --git a/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift b/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift deleted file mode 100644 index cae3f44a7..000000000 --- a/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift +++ /dev/null @@ -1,266 +0,0 @@ -// -// MainTimelineFeedCell.swift -// NetNewsWire-iOS -// -// Created by Stuart Breckenridge on 20/07/2025. -// Copyright © 2025 Ranchero Software. All rights reserved. -// - -import UIKit - -class MainTimelineFeedCell: UITableViewCell { - @IBOutlet var articleTitle: UILabel! - @IBOutlet var authorByLine: UILabel! - @IBOutlet var indicatorView: IconView! - @IBOutlet var articleDate: UILabel! - @IBOutlet var metaDataStackView: UIStackView! - @IBOutlet var separatorView: UIView! - - private var separatorLeadingConstraint: NSLayoutConstraint? - private var separatorTrailingConstraint: NSLayoutConstraint? - private var separatorTopConstraint: NSLayoutConstraint? - private var separatorHeightConstraint: NSLayoutConstraint? - - - private(set) var usedTitleLineCount: Int = 0 - - var cellData: MainTimelineCellData! { - didSet { - configure(cellData) - } - } - - var indexPathRow: Int = 0 { - didSet { - configureSeparator(indexRow: indexPathRow) - } - } - - var isPreview: Bool = false - - override func awakeFromNib() { - MainActor.assumeIsolated { - super.awakeFromNib() - indicatorView.alpha = 0.0 - configureStackView() - separatorView.translatesAutoresizingMaskIntoConstraints = false - separatorTopConstraint = separatorView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 0) - separatorHeightConstraint = separatorView.heightAnchor.constraint(equalToConstant: 1) - separatorTrailingConstraint = separatorView.trailingAnchor.constraint(equalTo: articleTitle.trailingAnchor) - // Leading constraint will be configured in configureSeparator(indexRow:) - NSLayoutConstraint.activate([ - separatorTopConstraint!, - separatorHeightConstraint!, - separatorTrailingConstraint! - ]) - } - } - - override func layoutSubviews() { - super.layoutSubviews() - applyTitleTextWithAttributes(configurationState) - articleTitle.setNeedsLayout() - articleTitle.layoutIfNeeded() - } - - private func configureStackView() { - switch traitCollection.preferredContentSizeCategory { - case .accessibilityMedium, .accessibilityLarge, .accessibilityExtraLarge, .accessibilityExtraExtraLarge, .accessibilityExtraExtraExtraLarge: - metaDataStackView.axis = .vertical - metaDataStackView.alignment = .leading - metaDataStackView.distribution = .fill - default: - metaDataStackView.axis = .horizontal - metaDataStackView.alignment = .center - metaDataStackView.distribution = .fill - } - } - - private func configure(_ cellData: MainTimelineCellData) { - updateIndicatorView(cellData) - articleTitle.numberOfLines = cellData.numberOfLines - - if cellData.showFeedName == .feed { - authorByLine.text = cellData.feedName - } else if cellData.showFeedName == .byline { - authorByLine.text = cellData.byline - } else if cellData.showFeedName == .none { - authorByLine.text = "" - } - - articleDate.text = cellData.dateString - } - - private func configureSeparator(indexRow: Int) { - // Set base constraints if they don't exist. - if separatorTopConstraint == nil || separatorHeightConstraint == nil || separatorTrailingConstraint == nil { - separatorView.translatesAutoresizingMaskIntoConstraints = false - separatorTopConstraint = separatorView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 0) - separatorHeightConstraint = separatorView.heightAnchor.constraint(equalToConstant: 1) - separatorTrailingConstraint = separatorView.trailingAnchor.constraint(equalTo: articleTitle.trailingAnchor) - NSLayoutConstraint.activate([ - separatorTopConstraint!, - separatorHeightConstraint!, - separatorTrailingConstraint! - ]) - print(#function, "Base constraints activated") - } - - // disable leading constraints - if let existingLeading = separatorLeadingConstraint { - existingLeading.isActive = false - separatorLeadingConstraint = nil - print(#function, "Leading deactivated") - } - - // Give initial row special treatment - if indexRow == 0 { - separatorLeadingConstraint = separatorView.leadingAnchor.constraint(equalTo: indicatorView.leadingAnchor) - print(#function, "Index 0: Leading set") - } else { - separatorLeadingConstraint = separatorView.leadingAnchor.constraint(equalTo: articleTitle.leadingAnchor, constant: 0) - print(#function, "Index \(indexPathRow): Leading set") - } - - separatorLeadingConstraint?.isActive = true - print(#function, "Leading activated") - - setNeedsLayout() - layoutIfNeeded() - print(#function, "Layout triggered") - } - - private func updateIndicatorView(_ cellData: MainTimelineCellData) { - if cellData.read == false { - if indicatorView.alpha == 0.0 { - indicatorView.alpha = 1.0 - } - UIView.animate(withDuration: 0.25) { - self.indicatorView.iconImage = Assets.Images.unreadCellIndicator - self.indicatorView.tintColor = Assets.Colors.secondaryAccent - } - return - } else if cellData.starred { - if indicatorView.alpha == 0.0 { - indicatorView.alpha = 1.0 - } - UIView.animate(withDuration: 0.25) { - self.indicatorView.iconImage = Assets.Images.starredFeed - self.indicatorView.tintColor = Assets.Colors.star - } - return - } else if indicatorView.alpha == 1.0 { - UIView.animate(withDuration: 0.25) { - self.indicatorView.alpha = 0.0 - self.indicatorView.iconImage = nil - } - } - } - - private func applyTitleTextWithAttributes(_ state: UICellConfigurationState) { - let attributedCellText = NSMutableAttributedString() - if cellData.title != "" { - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.minimumLineHeight = UIFont.preferredFont(forTextStyle: .headline).lineHeight - paragraphStyle.maximumLineHeight = UIFont.preferredFont(forTextStyle: .headline).lineHeight - let titleAttributes: [NSAttributedString.Key: Any] = [ - .font: UIFont.preferredFont(forTextStyle: .headline), - .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) - ] - let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes) - attributedCellText.append(titleAttributed) - } - - articleTitle.attributedText = attributedCellText - - if linesUsedForTitleGreaterThanOrEqualToPreference() { - articleTitle.lineBreakMode = .byWordWrapping - return - } - - if cellData.summary != "" { - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.minimumLineHeight = UIFont.preferredFont(forTextStyle: .body).lineHeight - paragraphStyle.maximumLineHeight = UIFont.preferredFont(forTextStyle: .body).lineHeight - let summaryAttributes: [NSAttributedString.Key: Any] = [ - .font: UIFont.preferredFont(forTextStyle: .body), - .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) - ] - var summaryAttributed: NSAttributedString - if cellData.title != "" { - summaryAttributed = NSAttributedString(string: "\n" + cellData.summary, attributes: summaryAttributes) - } else { - summaryAttributed = NSAttributedString(string: cellData.summary, attributes: summaryAttributes) - } - attributedCellText.append(summaryAttributed) - } - - articleTitle.attributedText = attributedCellText - articleTitle.lineBreakMode = .byTruncatingTail - } - - func titleTextColor(for state: UICellConfigurationState) -> UIColor { - let isSelected = state.isSelected || state.isHighlighted || state.isEditing || state.isSwiped - if isSelected { - return .white - } else { - return .label - } - } - - private func linesUsedForTitleGreaterThanOrEqualToPreference() -> Bool { - let attributed = articleTitle.attributedText ?? NSAttributedString() - let textStorage = NSTextStorage(attributedString: attributed) - let containerSize = CGSize(width: articleTitle.bounds.width, height: .greatestFiniteMagnitude) - let textContainer = NSTextContainer(size: containerSize) - textContainer.lineFragmentPadding = 0 - textContainer.maximumNumberOfLines = articleTitle.numberOfLines // 0 means unlimited - textContainer.lineBreakMode = articleTitle.lineBreakMode - - let layoutManager = NSLayoutManager() - layoutManager.addTextContainer(textContainer) - textStorage.addLayoutManager(layoutManager) - - _ = layoutManager.glyphRange(for: textContainer) - - var lineCount = 0 - var glyphIndex = 0 - let glyphs = layoutManager.numberOfGlyphs - while glyphIndex < glyphs { - var lineRange = NSRange() - _ = layoutManager.lineFragmentRect(forGlyphAt: glyphIndex, effectiveRange: &lineRange) - glyphIndex = NSMaxRange(lineRange) - lineCount += 1 - } - usedTitleLineCount = lineCount - return usedTitleLineCount == AppDefaults.shared.timelineNumberOfLines - } - - override func updateConfiguration(using state: UICellConfigurationState) { - super.updateConfiguration(using: state) - - var backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state) - backgroundConfig.cornerRadius = 20 - if traitCollection.userInterfaceIdiom == .pad { - backgroundConfig.edgesAddingLayoutMarginsToBackgroundInsets = [.leading, .trailing] - backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(top: 0, leading: !isPreview ? -4 : -12, bottom: 0, trailing: !isPreview ? -4 : -12) - } - - let isActive = state.isSelected || state.isHighlighted || state.isEditing || state.isSwiped - - if isActive { - backgroundConfig.backgroundColor = Assets.Colors.primaryAccent - articleTitle.textColor = titleTextColor(for: state) - articleDate.textColor = .lightText - authorByLine.textColor = .lightText - } else { - articleTitle.textColor = titleTextColor(for: state) - articleDate.textColor = .secondaryLabel - authorByLine.textColor = .secondaryLabel - } - - self.backgroundConfiguration = backgroundConfig - } -} diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index f56174eb6..6224036ee 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -14,6 +14,11 @@ import RSWeb import Account import Articles +let standardCellIdentifier = "MainTimelineCellStandard" +let standardCellIdentifierIndex0 = "MainTimelineCellIndexZero" +let standardCellIdentifierIcon = "MainTimelineCellIcon" +let standardCellIdentifierIconIndex0 = "MainTimelineCellIconIndexZero" + final class MainTimelineViewController: UITableViewController, UndoableCommandRunner { private var numberOfTextLines = 0 @@ -154,7 +159,7 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu // TODO: fix this temporary hack, which will probably require refactoring image handling. // We want to know when to possibly reconfigure our cells with a new image, and we don’t - // always know when an image is available — but watching the .htmlMetadataAvailable Notification + // always know when an image is available — but watching the .htmlMetadataAvailable Notification // lets us know that it’s time to request an image. NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .htmlMetadataAvailable, object: nil) @@ -681,7 +686,7 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu updateIconForVisibleArticles() } - /// Update icon for all visible articles — or, if feed is non-nil, update articles only from that feed. + /// Update icon for all visible articles — or, if feed is non-nil, update articles only from that feed. private func updateIconForVisibleArticles(_ feed: Feed? = nil) { guard isViewLoaded, view.window != nil else { return @@ -867,7 +872,7 @@ private extension MainTimelineViewController { tableView.rowHeight = UITableView.automaticDimension } - var snapshot = NSDiffableDataSourceSnapshot() + var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([0]) snapshot.appendItems(articles ?? ArticleArray(), toSection: 0) @@ -882,21 +887,31 @@ private extension MainTimelineViewController { MainTimelineDataSource(tableView: tableView, cellProvider: { [weak self] tableView, indexPath, article in let cellData = self!.configure(article: article) if self!.showIcons { - let cell = tableView.dequeueReusableCell(withIdentifier: "MainTimelineIconFeedCell", for: indexPath) as! MainTimelineIconFeedCell - cell.cellData = cellData - cell.indexPathRow = indexPath.row - return cell + if indexPath.row == 0 { + let cell = tableView.dequeueReusableCell(withIdentifier: standardCellIdentifierIconIndex0, for: indexPath) as! MainTimelineCell + cell.cellData = cellData + return cell + } else { + let cell = tableView.dequeueReusableCell(withIdentifier: standardCellIdentifierIcon, for: indexPath) as! MainTimelineCell + cell.cellData = cellData + return cell + } } else { - let cell = tableView.dequeueReusableCell(withIdentifier: "MainTimelineFeedCell", for: indexPath) as! MainTimelineFeedCell - cell.cellData = cellData - cell.indexPathRow = indexPath.row - return cell + if indexPath.row == 0 { + let cell = tableView.dequeueReusableCell(withIdentifier: standardCellIdentifierIndex0, for: indexPath) as! MainTimelineCell + cell.cellData = cellData + return cell + } else { + let cell = tableView.dequeueReusableCell(withIdentifier: standardCellIdentifier, for: indexPath) as! MainTimelineCell + cell.cellData = cellData + return cell + } } }) dataSource.defaultRowAnimation = .middle return dataSource - } + } @discardableResult func configure(article: Article) -> MainTimelineCellData {