diff --git a/iOS/Base.lproj/Main.storyboard b/iOS/Base.lproj/Main.storyboard index aaba26437..7dc8a4786 100644 --- a/iOS/Base.lproj/Main.storyboard +++ b/iOS/Base.lproj/Main.storyboard @@ -128,12 +128,10 @@ - + + - - - @@ -183,17 +181,14 @@ - - - @@ -204,8 +199,8 @@ - + @@ -215,12 +210,10 @@ - + + - - - @@ -260,13 +253,10 @@ - - - @@ -277,8 +267,8 @@ - + diff --git a/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift b/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift index b8f4b4a3e..cae3f44a7 100644 --- a/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineFeedCell.swift @@ -14,7 +14,13 @@ class MainTimelineFeedCell: UITableViewCell { @IBOutlet var indicatorView: IconView! @IBOutlet var articleDate: UILabel! @IBOutlet var metaDataStackView: UIStackView! - @IBOutlet var leadingConstraint: NSLayoutConstraint! + @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 @@ -37,6 +43,16 @@ class MainTimelineFeedCell: UITableViewCell { 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! + ]) } } @@ -76,12 +92,42 @@ class MainTimelineFeedCell: UITableViewCell { } private func configureSeparator(indexRow: Int) { - let constant: CGFloat = (indexRow == 0) ? 15.0 : 39.0 - if leadingConstraint != nil { - leadingConstraint.constant = constant - setNeedsLayout() - layoutIfNeeded() + // 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) { @@ -165,8 +211,6 @@ class MainTimelineFeedCell: UITableViewCell { } private func linesUsedForTitleGreaterThanOrEqualToPreference() -> Bool { - contentView.layoutIfNeeded() - let attributed = articleTitle.attributedText ?? NSAttributedString() let textStorage = NSTextStorage(attributedString: attributed) let containerSize = CGSize(width: articleTitle.bounds.width, height: .greatestFiniteMagnitude) diff --git a/iOS/MainTimeline/Cells/MainTimelineIconFeedCell.swift b/iOS/MainTimeline/Cells/MainTimelineIconFeedCell.swift index b315f5f64..1cb0fc065 100644 --- a/iOS/MainTimeline/Cells/MainTimelineIconFeedCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineIconFeedCell.swift @@ -15,40 +15,57 @@ class MainTimelineIconFeedCell: UITableViewCell { @IBOutlet var indicatorView: IconView! @IBOutlet var articleDate: UILabel! @IBOutlet var metaDataStackView: UIStackView! - @IBOutlet var leadingConstraint: NSLayoutConstraint! - + @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 iconView.translatesAutoresizingMaskIntoConstraints = false configureStackView() + + separatorView.translatesAutoresizingMaskIntoConstraints = false + // Create base constraints once; we'll adjust the leading anchor per index later + 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: @@ -61,11 +78,11 @@ class MainTimelineIconFeedCell: UITableViewCell { 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 { @@ -73,45 +90,69 @@ class MainTimelineIconFeedCell: UITableViewCell { } else if cellData.showFeedName == .none { authorByLine.text = "" } - + setIconImage(cellData.iconImage, with: cellData.iconSize) - + articleDate.text = cellData.dateString } - + private func configureSeparator(indexRow: Int) { - let constant: CGFloat = (indexRow == 0) ? 15.0 : 83.0 - if leadingConstraint != nil { - leadingConstraint.constant = constant - setNeedsLayout() - layoutIfNeeded() + // 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! + ]) } - } + // disable leading constraints + if let existingLeading = separatorLeadingConstraint { + existingLeading.isActive = false + separatorLeadingConstraint = nil + } + + // Give initial row special treatment + if indexRow == 0 { + separatorLeadingConstraint = separatorView.leadingAnchor.constraint(equalTo: indicatorView.leadingAnchor) + } else { + separatorLeadingConstraint = separatorView.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: 8) + } + + separatorLeadingConstraint?.isActive = true + + setNeedsLayout() + layoutIfNeeded() + } + private func setIconImage(_ iconImage: IconImage?, with size: IconSize) { iconView.iconImage = iconImage updateIconViewSizeConstraints(to: size.size) } - + func setIconImage(_ iconImage: IconImage?) { if iconView.iconImage !== iconImage { iconView.iconImage = iconImage } } - + private func updateIconViewSizeConstraints(to size: CGSize) { for constraint in iconView.constraints { constraint.isActive = false } - + NSLayoutConstraint.activate([ iconView.widthAnchor.constraint(equalToConstant: size.width), iconView.heightAnchor.constraint(equalToConstant: size.height) ]) - + setNeedsLayout() } - + private func updateIndicatorView(_ cellData: MainTimelineCellData) { if cellData.read == false { if indicatorView.alpha == 0.0 { @@ -138,7 +179,7 @@ class MainTimelineIconFeedCell: UITableViewCell { } } } - + private func applyTitleTextWithAttributes(_ state: UICellConfigurationState) { let attributedCellText = NSMutableAttributedString() if cellData.title != "" { @@ -153,14 +194,14 @@ class MainTimelineIconFeedCell: UITableViewCell { 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 @@ -178,11 +219,11 @@ class MainTimelineIconFeedCell: UITableViewCell { } attributedCellText.append(summaryAttributed) } - + articleTitle.attributedText = attributedCellText articleTitle.lineBreakMode = .byTruncatingTail } - + private func linesUsedForTitleGreaterThanOrEqualToPreference() -> Bool { let attributed = articleTitle.attributedText ?? NSAttributedString() let textStorage = NSTextStorage(attributedString: attributed) @@ -210,7 +251,7 @@ class MainTimelineIconFeedCell: UITableViewCell { usedTitleLineCount = lineCount return usedTitleLineCount == AppDefaults.shared.timelineNumberOfLines } - + func titleTextColor(for state: UICellConfigurationState) -> UIColor { let isSelected = state.isSelected || state.isHighlighted || state.isEditing || state.isSwiped if isSelected { @@ -219,19 +260,19 @@ class MainTimelineIconFeedCell: UITableViewCell { return .label } } - + 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) @@ -242,9 +283,9 @@ class MainTimelineIconFeedCell: UITableViewCell { articleDate.textColor = .secondaryLabel authorByLine.textColor = .secondaryLabel } - + self.backgroundConfiguration = backgroundConfig - + } - + }