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-04-20 18:13:02 +02:00
|
|
|
private static let indent = CGFloat(integerLiteral: 14)
|
2019-04-20 17:58:16 +02:00
|
|
|
private static let editingControlIndent = CGFloat(integerLiteral: 40)
|
2019-04-15 22:03:05 +02:00
|
|
|
private static let imageSize = CGSize(width: 16, height: 16)
|
2019-04-18 21:20:47 +02:00
|
|
|
private static let marginLeft = CGFloat(integerLiteral: 8)
|
2019-04-15 22:03:05 +02:00
|
|
|
private static let imageMarginRight = CGFloat(integerLiteral: 8)
|
|
|
|
private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8)
|
2019-04-18 12:41:27 +02:00
|
|
|
private static let unreadCountMarginRight = CGFloat(integerLiteral: 0)
|
|
|
|
private static let disclosureButtonSize = CGSize(width: 44, height: 44)
|
2019-04-28 19:08:50 +02:00
|
|
|
|
|
|
|
private static let minRowHeight = CGFloat(integerLiteral: 44)
|
2019-04-17 17:15:44 +02:00
|
|
|
|
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-04-15 22:03:05 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
let height: CGFloat
|
|
|
|
|
2019-04-28 18:25:21 +02:00
|
|
|
init(cellWidth: CGFloat, insets: UIEdgeInsets, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterFeedUnreadCountView, showingEditingControl: Bool, indent: Bool, shouldShowDisclosure: Bool) {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
var initialIndent = MasterFeedTableViewCellLayout.marginLeft + insets.left
|
2019-04-20 17:58:16 +02:00
|
|
|
if indent {
|
2019-04-23 15:00:27 +02:00
|
|
|
initialIndent += MasterFeedTableViewCellLayout.indent
|
2019-04-20 17:58:16 +02:00
|
|
|
}
|
|
|
|
if showingEditingControl {
|
2019-04-23 15:00:27 +02:00
|
|
|
initialIndent += MasterFeedTableViewCellLayout.editingControlIndent
|
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-04-18 12:41:27 +02:00
|
|
|
// Favicon
|
2019-04-15 22:03:05 +02:00
|
|
|
var rFavicon = CGRect.zero
|
|
|
|
if shouldShowImage {
|
2019-04-23 15:00:27 +02:00
|
|
|
rFavicon = CGRect(x: bounds.origin.x, y: 0.0, 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-04-15 22:03:05 +02:00
|
|
|
let unreadCountSize = unreadCountView.intrinsicContentSize
|
|
|
|
let unreadCountIsHidden = unreadCountView.unreadCount < 1
|
|
|
|
|
|
|
|
var rUnread = CGRect.zero
|
|
|
|
if !unreadCountIsHidden {
|
|
|
|
rUnread.size = unreadCountSize
|
2019-04-18 12:41:27 +02:00
|
|
|
rUnread.origin.x = bounds.maxX -
|
2019-04-23 15:00:27 +02:00
|
|
|
(unreadCountSize.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + MasterFeedTableViewCellLayout.disclosureButtonSize.width)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-18 12:41:27 +02:00
|
|
|
|
|
|
|
// Disclosure Button
|
|
|
|
var rDisclosure = CGRect.zero
|
|
|
|
if shouldShowDisclosure {
|
2019-04-23 15:00:27 +02:00
|
|
|
rDisclosure.size = MasterFeedTableViewCellLayout.disclosureButtonSize
|
|
|
|
rDisclosure.origin.x = bounds.maxX - MasterFeedTableViewCellLayout.disclosureButtonSize.width
|
2019-04-18 12:41:27 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Title
|
2019-04-28 19:08:50 +02:00
|
|
|
let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + MasterFeedTableViewCellLayout.disclosureButtonSize.width)
|
2019-04-28 17:31:35 +02:00
|
|
|
let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth)))
|
|
|
|
|
|
|
|
var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSizeInfo.size.width, height: labelSizeInfo.size.height)
|
|
|
|
if shouldShowImage {
|
|
|
|
rLabel.origin.x = rFavicon.maxX + MasterFeedTableViewCellLayout.imageMarginRight
|
|
|
|
} else {
|
|
|
|
rLabel.origin.x = bounds.minX
|
|
|
|
}
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
// Determine cell height
|
2019-04-28 19:08:50 +02:00
|
|
|
var cellHeight = [rFavicon, rLabel, rUnread, rDisclosure].maxY()
|
|
|
|
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 shouldShowImage {
|
|
|
|
rFavicon = MasterFeedTableViewCellLayout.centerVertically(rFavicon, newBounds)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
if !unreadCountIsHidden {
|
|
|
|
rUnread = MasterFeedTableViewCellLayout.centerVertically(rUnread, newBounds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldShowDisclosure {
|
|
|
|
rDisclosure = MasterFeedTableViewCellLayout.centerVertically(rDisclosure, newBounds)
|
|
|
|
}
|
|
|
|
|
|
|
|
rLabel = MasterFeedTableViewCellLayout.centerVertically(rLabel, newBounds)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|