NetNewsWire/iOS/MasterTimeline/Cell/MasterTimelineCellLayout.swift

222 lines
6.9 KiB
Swift
Raw Normal View History

//
// MasterTimelineCellLayout.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/6/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import UIKit
import RSCore
struct MasterTimelineCellLayout {
2019-04-29 21:40:14 +02:00
static let maxNumberOfLines = 2
static let cellPadding = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
2019-04-29 21:40:14 +02:00
static let unreadCircleMarginLeft = CGFloat(integerLiteral: 8)
static let unreadCircleDimension = CGFloat(integerLiteral: 8)
static let unreadCircleMarginRight = CGFloat(integerLiteral: 8)
2019-04-29 21:40:14 +02:00
static let starDimension = CGFloat(integerLiteral: 13)
2019-04-29 21:40:14 +02:00
static let avatarSize = CGSize(width: 48.0, height: 48.0)
static let avatarMarginRight = CGFloat(integerLiteral: 8)
static let avatarCornerRadius = CGFloat(integerLiteral: 4)
static let titleColor = AppAssets.timelineTextPrimaryColor
static var titleFont: UIFont {
return UIFont.preferredFont(forTextStyle: .headline)
}
static let titleBottomMargin = CGFloat(integerLiteral: 1)
2019-04-29 21:40:14 +02:00
static let feedColor = AppAssets.timelineTextSecondaryColor
static var feedNameFont: UIFont {
return UIFont.preferredFont(forTextStyle: .footnote)
}
2019-04-29 21:40:14 +02:00
static let feedRightMargin = CGFloat(integerLiteral: 8)
2019-04-29 21:40:14 +02:00
static let dateColor = AppAssets.timelineTextSecondaryColor
static var dateFont: UIFont {
return UIFont.preferredFont(forTextStyle: .footnote)
}
2019-04-29 21:40:14 +02:00
static let dateMarginBottom = CGFloat(integerLiteral: 1)
2019-04-29 21:40:14 +02:00
static let summaryColor = AppAssets.timelineTextPrimaryColor
static var summaryFont: UIFont {
return UIFont.preferredFont(forTextStyle: .body)
}
2019-04-17 17:15:44 +02:00
static let chevronWidth = CGFloat(integerLiteral: 28)
let width: CGFloat
2019-04-29 21:40:14 +02:00
let insets: UIEdgeInsets
let height: CGFloat
let unreadIndicatorRect: CGRect
let starRect: CGRect
let avatarImageRect: CGRect
2019-04-29 21:40:14 +02:00
let titleRect: CGRect
let summaryRect: CGRect
let feedNameRect: CGRect
let dateRect: CGRect
let separatorInsets: UIEdgeInsets
2019-04-29 21:40:14 +02:00
init(width: CGFloat, insets: UIEdgeInsets, cellData: MasterTimelineCellData, showAvatar: Bool) {
self.width = width
self.insets = insets
2019-04-29 21:40:14 +02:00
var currentPoint = CGPoint.zero
currentPoint.x = MasterTimelineCellLayout.cellPadding.left + insets.left + MasterTimelineCellLayout.unreadCircleMarginLeft
currentPoint.y = MasterTimelineCellLayout.cellPadding.top
// Unread Indicator and Star
self.unreadIndicatorRect = MasterTimelineCellLayout.rectForUnreadIndicator(currentPoint)
self.starRect = MasterTimelineCellLayout.rectForStar(currentPoint)
// Start the point at the beginning position of the main block
currentPoint.x += MasterTimelineCellLayout.unreadCircleDimension + MasterTimelineCellLayout.unreadCircleMarginRight
// Separator Insets
self.separatorInsets = UIEdgeInsets(top: 0, left: currentPoint.x, bottom: 0, right: 0)
// Avatar
if showAvatar {
self.avatarImageRect = MasterTimelineCellLayout.rectForAvatar(currentPoint)
currentPoint.x = self.avatarImageRect.maxX + MasterTimelineCellLayout.avatarMarginRight
} else {
2019-04-29 21:40:14 +02:00
self.avatarImageRect = CGRect.zero
}
2019-04-29 21:40:14 +02:00
let textAreaWidth = width - (currentPoint.x + MasterTimelineCellLayout.chevronWidth + MasterTimelineCellLayout.cellPadding.right + insets.right)
2019-04-17 17:15:44 +02:00
2019-04-29 21:40:14 +02:00
// Title Text Block
let (titleRect, numberOfLinesForTitle) = MasterTimelineCellLayout.rectForTitle(cellData, currentPoint, textAreaWidth)
self.titleRect = titleRect
// Summary Text Block
if self.titleRect != CGRect.zero {
currentPoint.y = self.titleRect.maxY + MasterTimelineCellLayout.titleBottomMargin
}
2019-04-29 21:40:14 +02:00
self.summaryRect = MasterTimelineCellLayout.rectForSummary(cellData, currentPoint, textAreaWidth, numberOfLinesForTitle)
2019-04-29 21:40:14 +02:00
currentPoint.y = [self.titleRect, self.summaryRect].maxY()
2019-04-29 21:40:14 +02:00
// Feed Name and Pub Date
self.dateRect = MasterTimelineCellLayout.rectForDate(cellData, currentPoint, textAreaWidth)
2019-04-29 21:40:14 +02:00
let feedNameWidth = textAreaWidth - (MasterTimelineCellLayout.feedRightMargin + self.dateRect.size.width)
self.feedNameRect = MasterTimelineCellLayout.rectForFeedName(cellData, currentPoint, feedNameWidth)
self.height = [self.avatarImageRect, self.feedNameRect].maxY() + MasterTimelineCellLayout.cellPadding.bottom
}
}
// MARK: - Calculate Rects
private extension MasterTimelineCellLayout {
2019-04-29 21:40:14 +02:00
static func rectForUnreadIndicator(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
r.size = CGSize(width: MasterTimelineCellLayout.unreadCircleDimension, height: MasterTimelineCellLayout.unreadCircleDimension)
r.origin.x = point.x
r.origin.y = point.y + 9
return r
}
2019-04-29 21:40:14 +02:00
static func rectForStar(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
r.size.width = MasterTimelineCellLayout.starDimension
r.size.height = MasterTimelineCellLayout.starDimension
r.origin.x = floor(point.x - ((MasterTimelineCellLayout.starDimension - MasterTimelineCellLayout.unreadCircleDimension) / 2.0))
r.origin.y = point.y + 5
return r
}
2019-04-29 21:40:14 +02:00
static func rectForAvatar(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
r.size = MasterTimelineCellLayout.avatarSize
r.origin = point
return r
}
static func rectForTitle(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> (CGRect, Int) {
var r = CGRect.zero
if cellData.title.isEmpty {
return (r, 0)
}
2019-04-29 21:40:14 +02:00
r.origin = point
let sizeInfo = MultilineUILabelSizer.size(for: cellData.title, font: MasterTimelineCellLayout.titleFont, numberOfLines: MasterTimelineCellLayout.maxNumberOfLines, width: Int(textAreaWidth))
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return (r, sizeInfo.numberOfLinesUsed)
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
static func rectForSummary(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat, _ linesUsed: Int) -> CGRect {
let linesLeft = MasterTimelineCellLayout.maxNumberOfLines - linesUsed
var r = CGRect.zero
if cellData.summary.isEmpty || linesLeft < 1 {
return r
}
2019-04-29 21:40:14 +02:00
r.origin = point
let sizeInfo = MultilineUILabelSizer.size(for: cellData.summary, font: MasterTimelineCellLayout.summaryFont, numberOfLines: linesLeft, width: Int(textAreaWidth))
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
static func rectForDate(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect {
var r = CGRect.zero
2019-04-29 21:40:14 +02:00
let size = SingleLineUILabelSizer.size(for: cellData.dateString, font: MasterTimelineCellLayout.dateFont)
r.size = size
r.origin.x = (point.x + textAreaWidth) - size.width
r.origin.y = point.y
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
static func rectForFeedName(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect {
var r = CGRect.zero
2019-04-29 21:40:14 +02:00
r.origin = point
let size = SingleLineUILabelSizer.size(for: cellData.feedName, font: MasterTimelineCellLayout.feedNameFont)
r.size = size
if r.size.width > textAreaWidth {
r.size.width = textAreaWidth
}
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
}