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 17:58:16 +02:00
|
|
|
private static let editingControlIndent = CGFloat(integerLiteral: 40)
|
2019-04-28 23:54:45 +02:00
|
|
|
private static let imageSize = CGSize(width: 20, height: 20)
|
2019-09-28 19:11:33 +02:00
|
|
|
private static let imageMarginRight = CGFloat(integerLiteral: 11)
|
2019-04-15 22:03:05 +02:00
|
|
|
private static let unreadCountMarginLeft = 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-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-04-28 21:56:36 +02:00
|
|
|
let separatorInsets: UIEdgeInsets
|
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-04-29 16:20:45 +02:00
|
|
|
initialIndent += MasterFeedTableViewCellLayout.imageSize.width + MasterFeedTableViewCellLayout.imageMarginRight
|
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-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 {
|
|
|
|
let x = bounds.origin.x + ((MasterFeedTableViewCellLayout.disclosureButtonSize.width - MasterFeedTableViewCellLayout.imageSize.width) / 2)
|
2019-04-28 23:51:34 +02:00
|
|
|
let y = UIFontMetrics.default.scaledValue(for: CGFloat(integerLiteral: 4))
|
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-28 21:56:36 +02:00
|
|
|
// Separator Insets
|
2019-09-28 19:11:33 +02:00
|
|
|
if shouldShowDisclosure {
|
|
|
|
separatorInsets = UIEdgeInsets(top: 0, left: MasterFeedTableViewCellLayout.disclosureButtonSize.width, bottom: 0, right: 0)
|
|
|
|
} else {
|
|
|
|
separatorInsets = UIEdgeInsets(top: 0, left: rFavicon.maxX + MasterFeedTableViewCellLayout.imageMarginRight, bottom: 0, right: 0)
|
|
|
|
}
|
2019-04-28 21:56:36 +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)
|
|
|
|
if showingEditingControl {
|
|
|
|
rUnread.origin.x = rUnread.origin.x - MasterFeedTableViewCellLayout.editingControlIndent
|
|
|
|
}
|
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-09-27 02:43:17 +02:00
|
|
|
let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.disclosureButtonSize.width + MasterFeedTableViewCellLayout.unreadCountMarginRight)
|
2019-04-28 17:31:35 +02:00
|
|
|
let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth)))
|
|
|
|
|
2019-09-27 03:40:32 +02:00
|
|
|
let rLabelx = bounds.minX + MasterFeedTableViewCellLayout.disclosureButtonSize.width
|
|
|
|
var rLabel = CGRect(x: rLabelx, y: 0.0, width: labelSizeInfo.size.width, height: labelSizeInfo.size.height)
|
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)
|
|
|
|
|
2019-09-27 03:40:32 +02:00
|
|
|
if !shouldShowDisclosure && labelSizeInfo.numberOfLinesUsed == 1 {
|
2019-04-28 17:31:35 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|