mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Fix SwiftLint issues.
This commit is contained in:
1 parent
4a581e69df
commit
e80a44a080
5 files changed
+139
-166
No files matched your search
@@ -16,20 +16,20 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
@IBOutlet var articleDate: UILabel!
|
||||
@IBOutlet var metaDataStackView: UIStackView!
|
||||
@IBOutlet var topSeparator: UIView!
|
||||
|
||||
|
||||
var cellData: MainTimelineCellData! {
|
||||
didSet {
|
||||
configure(cellData)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var isPreview: Bool = false
|
||||
|
||||
|
||||
private var rangeOfTitle: NSRange?
|
||||
private var rangeOfSummary: NSRange?
|
||||
|
||||
|
||||
private static let indicatorAnimationDuration = 0.25
|
||||
|
||||
|
||||
override func awakeFromNib() {
|
||||
MainActor.assumeIsolated {
|
||||
super.awakeFromNib()
|
||||
@@ -38,21 +38,17 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
configureStackView()
|
||||
}
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
}
|
||||
|
||||
|
||||
override func prepareForReuse() {
|
||||
super.prepareForReuse()
|
||||
rangeOfTitle = nil
|
||||
rangeOfSummary = nil
|
||||
}
|
||||
|
||||
|
||||
private func configure(_ cellData: MainTimelineCellData) {
|
||||
articleContent.numberOfLines = cellData.numberOfLines
|
||||
addArticleContent(configurationState)
|
||||
|
||||
|
||||
if cellData.showFeedName == .feed {
|
||||
articleByLine.text = cellData.feedName
|
||||
} else if cellData.showFeedName == .byline {
|
||||
@@ -60,14 +56,14 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
} else if cellData.showFeedName == .none {
|
||||
articleByLine.text = ""
|
||||
}
|
||||
|
||||
|
||||
if feedIcon != nil {
|
||||
setIconImage(cellData.iconImage, with: cellData.iconSize)
|
||||
}
|
||||
|
||||
|
||||
articleDate.text = cellData.dateString
|
||||
}
|
||||
|
||||
|
||||
private func updateIndicatorView(_ state: UICellConfigurationState) {
|
||||
if cellData.read == false {
|
||||
if indicatorView.alpha == 0.0 {
|
||||
@@ -78,8 +74,7 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
self.indicatorView.tintColor = (state.isSelected && !state.isSwiped) ? .white : Assets.Colors.secondaryAccent
|
||||
}
|
||||
return
|
||||
}
|
||||
else if cellData.starred {
|
||||
} else if cellData.starred {
|
||||
if indicatorView.alpha == 0.0 {
|
||||
indicatorView.alpha = 1.0
|
||||
}
|
||||
@@ -88,15 +83,14 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
self.indicatorView.tintColor = (state.isSelected && !state.isSwiped) ? .white : Assets.Colors.star
|
||||
}
|
||||
return
|
||||
}
|
||||
else if indicatorView.alpha == 1.0 {
|
||||
} else if indicatorView.alpha == 1.0 {
|
||||
UIView.animate(withDuration: Self.indicatorAnimationDuration) {
|
||||
self.indicatorView.alpha = 0.0
|
||||
self.indicatorView.iconImage = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func configureStackView() {
|
||||
switch traitCollection.preferredContentSizeCategory {
|
||||
case .accessibilityMedium, .accessibilityLarge, .accessibilityExtraLarge, .accessibilityExtraExtraLarge, .accessibilityExtraExtraExtraLarge:
|
||||
@@ -109,37 +103,37 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
metaDataStackView.distribution = .fillEqually
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func setIconImage(_ iconImage: IconImage?, with size: IconSize) {
|
||||
if feedIcon != nil {
|
||||
updateIconViewSizeConstraints(to: size.size)
|
||||
feedIcon!.iconImage = iconImage
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func updateIconViewSizeConstraints(to size: CGSize) {
|
||||
guard feedIcon != nil else { return }
|
||||
|
||||
|
||||
for constraint in feedIcon!.constraints.compactMap({ $0 }) {
|
||||
constraint.isActive = false
|
||||
}
|
||||
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
feedIcon!.widthAnchor.constraint(equalToConstant: size.width),
|
||||
feedIcon!.heightAnchor.constraint(equalToConstant: size.height)
|
||||
])
|
||||
|
||||
|
||||
setNeedsLayout()
|
||||
}
|
||||
|
||||
|
||||
private 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
|
||||
@@ -147,18 +141,18 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
paragraphStyle.lineBreakStrategy = []
|
||||
paragraphStyle.hyphenationFactor = 1.0
|
||||
paragraphStyle.allowsDefaultTighteningForTruncation = true
|
||||
|
||||
|
||||
let titleAttributes: [NSAttributedString.Key: Any] = [
|
||||
.font: titleFont,
|
||||
.paragraphStyle: paragraphStyle,
|
||||
.foregroundColor: UIColor.label
|
||||
]
|
||||
|
||||
|
||||
let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes)
|
||||
let titleLength = titleAttributed.length
|
||||
let tentativeTitleRange = NSRange(location: 0, length: titleLength)
|
||||
rangeOfTitle = tentativeTitleRange
|
||||
|
||||
|
||||
let linesUsed = countLines(of: titleAttributed, width: articleContent.bounds.width)
|
||||
// 2. Measure Title Height
|
||||
if linesUsed >= cellData.numberOfLines {
|
||||
@@ -169,16 +163,16 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
rangeOfTitle = NSRange(location: 0, length: titleAttributed.length)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
attributedCellText.append(titleAttributed)
|
||||
rangeOfTitle = NSRange(location: 0, length: titleAttributed.length)
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
@@ -186,23 +180,23 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
paragraphStyle.lineBreakStrategy = []
|
||||
paragraphStyle.hyphenationFactor = 1.0
|
||||
paragraphStyle.allowsDefaultTighteningForTruncation = true
|
||||
|
||||
|
||||
let summaryAttributes: [NSAttributedString.Key: Any] = [
|
||||
.font: summaryFont,
|
||||
.paragraphStyle: paragraphStyle,
|
||||
.foregroundColor: cellData.title != "" ? UIColor.secondaryLabel : UIColor.label
|
||||
]
|
||||
|
||||
|
||||
let prefix = cellData.title != "" ? "\n" : ""
|
||||
let summaryAttributed = NSAttributedString(string: prefix + cellData.summary, attributes: summaryAttributes)
|
||||
let currentLength = attributedCellText.length
|
||||
let start = currentLength
|
||||
let length = summaryAttributed.length
|
||||
rangeOfSummary = NSRange(location: start, length: length)
|
||||
|
||||
|
||||
attributedCellText.append(summaryAttributed)
|
||||
}
|
||||
|
||||
|
||||
let linesUsed = countLines(of: attributedCellText, width: articleContent.bounds.width)
|
||||
if linesUsed >= cellData.numberOfLines {
|
||||
// The title already fills 3 lines, set it and exit
|
||||
@@ -215,7 +209,7 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func adjustArticleContentColor() {
|
||||
func applyTitleColour() {
|
||||
guard let titleRange = rangeOfTitle, titleRange.location != NSNotFound, titleRange.length > 0 else { return }
|
||||
@@ -252,22 +246,22 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
articleDate.textColor = .secondaryLabel
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func countLines(of attributedString: NSAttributedString, width: CGFloat) -> Int {
|
||||
let textStorage = NSTextStorage(attributedString: attributedString)
|
||||
let textContainer = NSTextContainer(size: CGSize(width: width, height: .greatestFiniteMagnitude))
|
||||
let layoutManager = NSLayoutManager()
|
||||
|
||||
|
||||
layoutManager.addTextContainer(textContainer)
|
||||
textStorage.addLayoutManager(layoutManager)
|
||||
|
||||
|
||||
textContainer.lineFragmentPadding = 0 // UILabel uses 0 or very small
|
||||
layoutManager.ensureLayout(for: textContainer)
|
||||
|
||||
|
||||
var lineCount = 0
|
||||
var index = 0
|
||||
var lineRange = NSRange()
|
||||
|
||||
|
||||
while index < layoutManager.numberOfGlyphs {
|
||||
layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
|
||||
index = NSMaxRange(lineRange)
|
||||
@@ -275,18 +269,18 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
}
|
||||
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 ? -12 : -12, bottom: 0, trailing: !isPreview ? -12 : -12)
|
||||
}
|
||||
|
||||
|
||||
if state.isSwiped && state.isSelected {
|
||||
backgroundConfig.backgroundColor = Assets.Colors.primaryAccent
|
||||
topSeparator.alpha = 0.0
|
||||
@@ -302,9 +296,8 @@ class MainTimelineCollectionViewCell: UICollectionViewCell {
|
||||
}
|
||||
adjustArticleContentColor()
|
||||
updateIndicatorView(state)
|
||||
|
||||
|
||||
self.backgroundConfiguration = backgroundConfig
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user