diff --git a/iOS/MainTimeline/Cells/MainTimelineCell.swift b/iOS/MainTimeline/Cells/MainTimelineCell.swift index 52ae93741..8bf2e3de0 100644 --- a/iOS/MainTimeline/Cells/MainTimelineCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineCell.swift @@ -8,8 +8,7 @@ import UIKit -class MainTimelineCell: UITableViewCell { - +final class MainTimelineCell: UITableViewCell { @IBOutlet var articleContent: UILabel! @IBOutlet var articleByLine: UILabel! @IBOutlet var feedIcon: IconView? @@ -25,6 +24,8 @@ class MainTimelineCell: UITableViewCell { var isPreview: Bool = false + private static let indicatorAnimationDuration = 0.25 + override func awakeFromNib() { MainActor.assumeIsolated { super.awakeFromNib() @@ -34,31 +35,66 @@ class MainTimelineCell: UITableViewCell { } } - private func configure(_ cellData: MainTimelineCellData) { + func setIconImage(_ iconImage: IconImage?) { + guard let feedIcon else { + return + } + if feedIcon.iconImage !== iconImage { + feedIcon.iconImage = iconImage + } + } + + 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) + } + + articleContent.textColor = titleTextColor(for: state) + if isActive(state) { + backgroundConfig.backgroundColor = Assets.Colors.primaryAccent + articleDate.textColor = .lightText + articleByLine.textColor = .lightText + } else { + articleDate.textColor = .secondaryLabel + articleByLine.textColor = .secondaryLabel + } + + updateIndicatorView(state) + + backgroundConfiguration = backgroundConfig + } +} + +private extension MainTimelineCell { + func configure(_ cellData: MainTimelineCellData) { articleContent.numberOfLines = cellData.numberOfLines addArticleContent(configurationState) - if cellData.showFeedName == .feed { + switch cellData.showFeedName { + case .feed: articleByLine.text = cellData.feedName - } else if cellData.showFeedName == .byline { + case .byline: articleByLine.text = cellData.byline - } else if cellData.showFeedName == .none { + case .none: articleByLine.text = "" } - if feedIcon != nil { - setIconImage(cellData.iconImage, with: cellData.iconSize) - } + setIconImage(cellData.iconImage, with: cellData.iconSize) articleDate.text = cellData.dateString } - private func updateIndicatorView(_ state: UICellConfigurationState) { + func updateIndicatorView(_ state: UICellConfigurationState) { if cellData.read == false { if indicatorView.alpha == 0.0 { indicatorView.alpha = 1.0 } - UIView.animate(withDuration: 0.25) { + UIView.animate(withDuration: Self.indicatorAnimationDuration) { self.indicatorView.iconImage = Assets.Images.unreadCellIndicator self.indicatorView.tintColor = self.isActive(state) ? .white : Assets.Colors.secondaryAccent } @@ -67,20 +103,20 @@ class MainTimelineCell: UITableViewCell { if indicatorView.alpha == 0.0 { indicatorView.alpha = 1.0 } - UIView.animate(withDuration: 0.25) { + UIView.animate(withDuration: Self.indicatorAnimationDuration) { self.indicatorView.iconImage = Assets.Images.starredFeed self.indicatorView.tintColor = self.isActive(state) ? .white : Assets.Colors.star } return } else if indicatorView.alpha == 1.0 { - UIView.animate(withDuration: 0.25) { + UIView.animate(withDuration: Self.indicatorAnimationDuration) { self.indicatorView.alpha = 0.0 self.indicatorView.iconImage = nil } } } - private func configureStackView() { + func configureStackView() { switch traitCollection.preferredContentSizeCategory { case .accessibilityMedium, .accessibilityLarge, .accessibilityExtraLarge, .accessibilityExtraExtraLarge, .accessibilityExtraExtraExtraLarge: metaDataStackView.axis = .vertical @@ -93,67 +129,63 @@ class MainTimelineCell: UITableViewCell { } } - private func isActive(_ state: UICellConfigurationState) -> Bool { - let active = state.isSwiped || state.isSelected || state.isEditing || state.isHighlighted - if cellData.title.contains("Vouchers") { - print("Vouchers: \(active)") - } - return active + func isActive(_ state: UICellConfigurationState) -> Bool { + state.isSwiped || state.isSelected || state.isEditing || state.isHighlighted } - func setIconImage(_ iconImage: IconImage?) { - if feedIcon!.iconImage !== iconImage { - feedIcon!.iconImage = iconImage - } - } - - private func setIconImage(_ iconImage: IconImage?, with size: IconSize) { - feedIcon!.iconImage = iconImage + func setIconImage(_ iconImage: IconImage?, with size: IconSize) { + setIconImage(iconImage) updateIconViewSizeConstraints(to: size.size) } - private func updateIconViewSizeConstraints(to size: CGSize) { - guard feedIcon != nil else { return } + func updateIconViewSizeConstraints(to size: CGSize) { + guard let feedIcon else { + return + } - for constraint in feedIcon!.constraints.compactMap({ $0 }) { + for constraint in feedIcon.constraints { constraint.isActive = false } NSLayoutConstraint.activate([ - feedIcon!.widthAnchor.constraint(equalToConstant: size.width), - feedIcon!.heightAnchor.constraint(equalToConstant: size.height) + feedIcon.widthAnchor.constraint(equalToConstant: size.width), + feedIcon.heightAnchor.constraint(equalToConstant: size.height) ]) setNeedsLayout() } - private func addArticleContent(_ state: UICellConfigurationState) { + func paragraphStyle(lineHeight: CGFloat) -> NSParagraphStyle { + let paragraphStyle = NSMutableParagraphStyle() + + paragraphStyle.minimumLineHeight = lineHeight + paragraphStyle.maximumLineHeight = lineHeight + paragraphStyle.lineBreakMode = .byWordWrapping + paragraphStyle.lineBreakStrategy = [] + paragraphStyle.hyphenationFactor = 1.0 + paragraphStyle.allowsDefaultTighteningForTruncation = true + + return paragraphStyle + } + + func attributes(textStyle: UIFont.TextStyle, color: UIColor) -> [NSAttributedString.Key: Any] { + let font = UIFont.preferredFont(forTextStyle: textStyle) + let paragraphStyle = paragraphStyle(lineHeight: font.lineHeight) + return [.font: font, .paragraphStyle: paragraphStyle, .foregroundColor: color] + } + + func addArticleContent(_ state: UICellConfigurationState) { let attributedCellText = NSMutableAttributedString() // 1. Prepare Title - if cellData.title != "" { - let titleFont = UIFont.preferredFont(forTextStyle: .headline) - let lineHeight = titleFont.lineHeight - - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.minimumLineHeight = lineHeight - paragraphStyle.maximumLineHeight = lineHeight - paragraphStyle.lineBreakMode = .byWordWrapping - paragraphStyle.lineBreakStrategy = [] - paragraphStyle.hyphenationFactor = 1.0 - paragraphStyle.allowsDefaultTighteningForTruncation = true - - let titleAttributes: [NSAttributedString.Key: Any] = [ - .font: titleFont, - .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) - ] - + if !cellData.title.isEmpty { + let titleAttributes = attributes(textStyle: .headline, color: titleTextColor(for: state)) let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes) - let linesUsed = countLines(of: titleAttributed, width: articleContent.bounds.width) + // 2. Measure Title Height + let linesUsed = countLines(of: titleAttributed, width: articleContent.bounds.width) if linesUsed >= cellData.numberOfLines { - // The title already fills 3 lines, set it and exit + // The title already fills numberOfLines lines, set it and exit articleContent.attributedText = titleAttributed articleContent.lineBreakMode = .byTruncatingTail return @@ -162,52 +194,23 @@ class MainTimelineCell: UITableViewCell { attributedCellText.append(titleAttributed) } - // 3. Prepare Summary (Only reached if Title < 3 lines) - if cellData.summary != "" { - let summaryFont = UIFont.preferredFont(forTextStyle: .body) - let summaryLineHeight = summaryFont.lineHeight - - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.minimumLineHeight = summaryLineHeight - paragraphStyle.maximumLineHeight = summaryLineHeight - paragraphStyle.lineBreakMode = .byWordWrapping - paragraphStyle.lineBreakStrategy = [] - paragraphStyle.hyphenationFactor = 1.0 - paragraphStyle.allowsDefaultTighteningForTruncation = true - - let summaryAttributes: [NSAttributedString.Key: Any] = [ - .font: summaryFont, - .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) - ] - + // 3. Prepare Summary (only reached if title < numberOfLines lines) + if !cellData.summary.isEmpty { + let summaryAttributes = attributes(textStyle: .body, color: titleTextColor(for: state)) let prefix = cellData.title != "" ? "\n" : "" let summaryAttributed = NSAttributedString(string: prefix + cellData.summary, attributes: summaryAttributes) attributedCellText.append(summaryAttributed) } - let linesUsed = countLines(of: attributedCellText, width: articleContent.bounds.width) - if attributedCellText.string.contains("Bree-folk") { - print("Bree-folk: \(linesUsed), \(self.feedIcon != nil)") - } - if linesUsed >= cellData.numberOfLines { - // The title already fills 3 lines, set it and exit - articleContent.attributedText = attributedCellText - articleContent.lineBreakMode = .byTruncatingTail - return - } else { - articleContent.attributedText = attributedCellText - articleContent.lineBreakMode = .byWordWrapping - } + articleContent.attributedText = attributedCellText + let linesUsed = countLines(of: attributedCellText, width: articleContent.bounds.width) + let lineBreakMode: NSLineBreakMode = linesUsed >= cellData.numberOfLines ? .byTruncatingTail : .byWordWrapping + articleContent.lineBreakMode = lineBreakMode } func titleTextColor(for state: UICellConfigurationState) -> UIColor { - if isActive(state) { - return .white - } else { - return .label - } + isActive(state) ? .white : .label } func countLines(of attributedString: NSAttributedString, width: CGFloat) -> Int { @@ -232,31 +235,4 @@ class MainTimelineCell: UITableViewCell { } return lineCount } - - 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) - } - - if isActive(state) { - backgroundConfig.backgroundColor = Assets.Colors.primaryAccent - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .lightText - articleByLine.textColor = .lightText - } else { - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .secondaryLabel - articleByLine.textColor = .secondaryLabel - } - - updateIndicatorView(state) - - self.backgroundConfiguration = backgroundConfig - } - }