2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterTableViewCellLayout.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/24/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import RSCore
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
struct MasterFeedTableViewCellLayout {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-11-02 17:46:24 +01:00
|
|
|
private static let indentWidth = CGFloat(integerLiteral: 42)
|
2019-04-20 17:58:16 +02:00
|
|
|
private static let editingControlIndent = CGFloat(integerLiteral: 40)
|
2019-10-29 22:39:42 +01:00
|
|
|
private static let imageSize = CGSize(width: 24, height: 24)
|
2019-09-28 19:11:33 +02:00
|
|
|
private static let imageMarginRight = CGFloat(integerLiteral: 11)
|
2019-10-29 23:55:49 +01:00
|
|
|
private static let labelMarginRight = CGFloat(integerLiteral: 8)
|
2019-09-27 02:43:17 +02:00
|
|
|
private static let unreadCountMarginRight = CGFloat(integerLiteral: 16)
|
2019-04-18 12:41:27 +02:00
|
|
|
private static let disclosureButtonSize = CGSize(width: 44, height: 44)
|
2019-10-23 22:31:54 +02:00
|
|
|
private static let verticalPadding = CGFloat(integerLiteral: 11)
|
2019-04-28 19:08:50 +02:00
|
|
|
|
|
|
|
private static let minRowHeight = CGFloat(integerLiteral: 44)
|
2019-04-17 17:15:44 +02:00
|
|
|
|
2019-09-22 00:24:50 +02:00
|
|
|
static let faviconCornerRadius = CGFloat(integerLiteral: 2)
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
let faviconRect: CGRect
|
|
|
|
let titleRect: CGRect
|
|
|
|
let unreadCountRect: CGRect
|
2019-04-18 12:41:27 +02:00
|
|
|
let disclosureButtonRect: CGRect
|
2019-10-29 01:52:50 +01:00
|
|
|
let separatorRect: CGRect
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
let height: CGFloat
|
|
|
|
|
2019-09-27 03:40:32 +02:00
|
|
|
init(cellWidth: CGFloat, insets: UIEdgeInsets, label: UILabel, unreadCountView: MasterFeedUnreadCountView, showingEditingControl: Bool, indent: Bool, shouldShowDisclosure: Bool) {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-09-27 02:43:17 +02:00
|
|
|
var initialIndent = insets.left
|
2019-04-20 17:58:16 +02:00
|
|
|
if indent {
|
2019-11-02 17:46:24 +01:00
|
|
|
initialIndent += MasterFeedTableViewCellLayout.indentWidth
|
2019-04-20 17:58:16 +02:00
|
|
|
}
|
2019-04-28 18:25:21 +02:00
|
|
|
let bounds = CGRect(x: initialIndent, y: 0.0, width: floor(cellWidth - initialIndent - insets.right), height: 0.0)
|
2019-04-20 17:58:16 +02:00
|
|
|
|
2019-09-27 02:43:17 +02:00
|
|
|
// Disclosure Button
|
|
|
|
var rDisclosure = CGRect.zero
|
|
|
|
if shouldShowDisclosure {
|
|
|
|
rDisclosure.size = MasterFeedTableViewCellLayout.disclosureButtonSize
|
|
|
|
rDisclosure.origin.x = bounds.origin.x
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
// Favicon
|
2019-04-15 22:03:05 +02:00
|
|
|
var rFavicon = CGRect.zero
|
2019-09-27 03:40:32 +02:00
|
|
|
if !shouldShowDisclosure {
|
2019-10-29 23:55:49 +01:00
|
|
|
let x = bounds.origin.x
|
2019-10-28 06:44:09 +01:00
|
|
|
let y = UIFontMetrics.default.scaledValue(for: MasterFeedTableViewCellLayout.verticalPadding) +
|
|
|
|
label.font.lineHeight / 2.0 -
|
|
|
|
MasterFeedTableViewCellLayout.imageSize.height / 2.0
|
2019-09-27 02:43:17 +02:00
|
|
|
rFavicon = CGRect(x: x, y: y, width: MasterFeedTableViewCellLayout.imageSize.width, height: MasterFeedTableViewCellLayout.imageSize.height)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
// Unread Count
|
2019-08-26 03:00:34 +02:00
|
|
|
let unreadCountSize = unreadCountView.contentSize
|
2019-04-15 22:03:05 +02:00
|
|
|
let unreadCountIsHidden = unreadCountView.unreadCount < 1
|
|
|
|
|
|
|
|
var rUnread = CGRect.zero
|
|
|
|
if !unreadCountIsHidden {
|
|
|
|
rUnread.size = unreadCountSize
|
2019-09-27 02:43:17 +02:00
|
|
|
rUnread.origin.x = bounds.maxX - (MasterFeedTableViewCellLayout.unreadCountMarginRight + unreadCountSize.width)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Title
|
2019-10-30 08:41:33 +01:00
|
|
|
var rLabelx = insets.left + MasterFeedTableViewCellLayout.disclosureButtonSize.width
|
|
|
|
if !shouldShowDisclosure {
|
|
|
|
rLabelx = rLabelx + MasterFeedTableViewCellLayout.imageSize.width + MasterFeedTableViewCellLayout.imageMarginRight
|
2019-10-29 23:55:49 +01:00
|
|
|
}
|
2019-10-23 22:31:54 +02:00
|
|
|
let rLabely = UIFontMetrics.default.scaledValue(for: MasterFeedTableViewCellLayout.verticalPadding)
|
2019-10-29 23:55:49 +01:00
|
|
|
|
|
|
|
var labelWidth = CGFloat.zero
|
|
|
|
if !unreadCountIsHidden {
|
|
|
|
labelWidth = cellWidth - (rLabelx + MasterFeedTableViewCellLayout.labelMarginRight + (cellWidth - rUnread.minX))
|
|
|
|
} else {
|
|
|
|
labelWidth = cellWidth - (rLabelx + MasterFeedTableViewCellLayout.labelMarginRight)
|
|
|
|
}
|
|
|
|
|
|
|
|
let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth)))
|
2019-10-30 08:41:33 +01:00
|
|
|
|
|
|
|
// Now that we've got everything (especially the label) computed without the editing controls, update for them.
|
|
|
|
// We do this because we don't want the row height to change when the editing controls are brought out. We will
|
|
|
|
// handle the missing space, but removing it from the label and truncating.
|
|
|
|
if showingEditingControl {
|
|
|
|
rDisclosure.origin.x += MasterFeedTableViewCellLayout.editingControlIndent
|
|
|
|
rFavicon.origin.x += MasterFeedTableViewCellLayout.editingControlIndent
|
|
|
|
rLabelx += MasterFeedTableViewCellLayout.editingControlIndent
|
|
|
|
if !unreadCountIsHidden {
|
|
|
|
rUnread.origin.x -= MasterFeedTableViewCellLayout.editingControlIndent
|
|
|
|
labelWidth = cellWidth - (rLabelx + MasterFeedTableViewCellLayout.labelMarginRight + (cellWidth - rUnread.minX))
|
|
|
|
} else {
|
|
|
|
labelWidth = cellWidth - (rLabelx + MasterFeedTableViewCellLayout.labelMarginRight + MasterFeedTableViewCellLayout.editingControlIndent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:20:36 +01:00
|
|
|
var rLabel = CGRect(x: rLabelx, y: rLabely, width: labelWidth, height: labelSizeInfo.size.height)
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Determine cell height
|
2019-10-23 22:31:54 +02:00
|
|
|
let paddedLabelHeight = rLabel.maxY + UIFontMetrics.default.scaledValue(for: MasterFeedTableViewCellLayout.verticalPadding)
|
|
|
|
let maxGraphicsHeight = [rFavicon, rUnread, rDisclosure].maxY()
|
|
|
|
var cellHeight = max(paddedLabelHeight, maxGraphicsHeight)
|
2019-04-28 19:08:50 +02:00
|
|
|
if cellHeight < MasterFeedTableViewCellLayout.minRowHeight {
|
|
|
|
cellHeight = MasterFeedTableViewCellLayout.minRowHeight
|
|
|
|
}
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Center in Cell
|
|
|
|
let newBounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width, height: cellHeight)
|
|
|
|
if !unreadCountIsHidden {
|
|
|
|
rUnread = MasterFeedTableViewCellLayout.centerVertically(rUnread, newBounds)
|
|
|
|
}
|
|
|
|
if shouldShowDisclosure {
|
|
|
|
rDisclosure = MasterFeedTableViewCellLayout.centerVertically(rDisclosure, newBounds)
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:20:36 +01:00
|
|
|
// Small fonts and the Favicon need centered if we hit the minimum row height
|
|
|
|
if cellHeight == MasterFeedTableViewCellLayout.minRowHeight {
|
|
|
|
rLabel = MasterFeedTableViewCellLayout.centerVertically(rLabel, newBounds)
|
|
|
|
rFavicon = MasterFeedTableViewCellLayout.centerVertically(rFavicon, newBounds)
|
|
|
|
}
|
|
|
|
|
2019-10-29 01:52:50 +01:00
|
|
|
// Separator Insets
|
2019-10-29 23:55:49 +01:00
|
|
|
let separatorInset = MasterFeedTableViewCellLayout.disclosureButtonSize.width
|
2019-10-29 01:52:50 +01:00
|
|
|
separatorRect = CGRect(x: separatorInset, y: cellHeight - 0.5, width: cellWidth - separatorInset, height: 0.5)
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Assign the properties
|
|
|
|
self.height = cellHeight
|
|
|
|
self.faviconRect = rFavicon
|
|
|
|
self.unreadCountRect = rUnread
|
|
|
|
self.disclosureButtonRect = rDisclosure
|
2019-04-15 22:03:05 +02:00
|
|
|
self.titleRect = rLabel
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally this will be implemented in RSCore (see RSGeometry)
|
|
|
|
static func centerVertically(_ originalRect: CGRect, _ containerRect: CGRect) -> CGRect {
|
|
|
|
var result = originalRect
|
|
|
|
result.origin.y = containerRect.midY - (result.height / 2.0)
|
|
|
|
result = result.integral
|
|
|
|
result.size = originalRect.size
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|