Made the disclosure chevron big enough to actually use.

This commit is contained in:
Maurice Parker 2019-04-18 05:41:27 -05:00
parent 7083b5064b
commit aa9a8f77e2
2 changed files with 54 additions and 47 deletions

View File

@ -42,11 +42,7 @@ class MasterTableViewCell : UITableViewCell {
var disclosureExpanded = false { var disclosureExpanded = false {
didSet { didSet {
if disclosureExpanded { updateDisclosureImage()
accessoryButton?.setImage(AppAssets.chevronDownImage, for: .normal)
} else {
accessoryButton?.setImage(AppAssets.chevronRightImage, for: .normal)
}
} }
} }
@ -110,7 +106,7 @@ class MasterTableViewCell : UITableViewCell {
private let unreadCountView = MasterUnreadCountView(frame: CGRect.zero) private let unreadCountView = MasterUnreadCountView(frame: CGRect.zero)
private var showingEditControl = false private var showingEditControl = false
private var accessoryButton: UIButton? private var disclosureButton: UIButton?
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
@ -124,26 +120,15 @@ class MasterTableViewCell : UITableViewCell {
override func layoutSubviews() { override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
let layout = MasterTableViewCellLayout(cellSize: bounds.size, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indent) let layout = MasterTableViewCellLayout(cellSize: bounds.size, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indent, shouldShowDisclosure: true)
layoutWith(layout) layoutWith(layout)
} }
@objc func buttonPressed(_ sender: UIButton) { @objc func buttonPressed(_ sender: UIButton) {
if allowDisclosureSelection {
guard allowDisclosureSelection else { disclosureExpanded = !disclosureExpanded
return delegate?.disclosureSelected(self, expanding: disclosureExpanded)
} }
if !disclosureExpanded {
sender.setImage(AppAssets.chevronDownImage, for: .normal)
delegate?.disclosureSelected(self, expanding: true)
} else {
sender.setImage(AppAssets.chevronRightImage, for: .normal)
delegate?.disclosureSelected(self, expanding: false)
}
disclosureExpanded = !disclosureExpanded
} }
} }
@ -151,28 +136,29 @@ class MasterTableViewCell : UITableViewCell {
private extension MasterTableViewCell { private extension MasterTableViewCell {
func commonInit() { func commonInit() {
addAccessoryView()
addSubviewAtInit(unreadCountView) addSubviewAtInit(unreadCountView)
addSubviewAtInit(faviconImageView) addSubviewAtInit(faviconImageView)
addSubviewAtInit(titleView) addSubviewAtInit(titleView)
addDisclosureView()
} }
func addAccessoryView() { func addDisclosureView() {
let button = UIButton(type: .roundedRect) disclosureButton = UIButton(type: .roundedRect)
button.frame = CGRect(x: 0, y: 0, width: 25.0, height: 25.0) disclosureButton!.tintColor = AppAssets.chevronDisclosureColor
disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside)
updateDisclosureImage()
addSubviewAtInit(disclosureButton!)
}
func updateDisclosureImage() {
if disclosureExpanded { if disclosureExpanded {
button.setImage(AppAssets.chevronDownImage, for: .normal) disclosureButton?.setImage(AppAssets.chevronDownImage, for: .normal)
} else { } else {
button.setImage(AppAssets.chevronRightImage, for: .normal) disclosureButton?.setImage(AppAssets.chevronRightImage, for: .normal)
} }
button.tintColor = AppAssets.chevronDisclosureColor
button.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside)
accessoryButton = button
accessoryView = button
} }
func addSubviewAtInit(_ view: UIView) { func addSubviewAtInit(_ view: UIView) {
@ -184,6 +170,7 @@ private extension MasterTableViewCell {
faviconImageView.rs_setFrameIfNotEqual(layout.faviconRect) faviconImageView.rs_setFrameIfNotEqual(layout.faviconRect)
titleView.rs_setFrameIfNotEqual(layout.titleRect) titleView.rs_setFrameIfNotEqual(layout.titleRect)
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect) unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
disclosureButton?.rs_setFrameIfNotEqual(layout.disclosureButtonRect)
} }
} }

View File

@ -15,26 +15,19 @@ struct MasterTableViewCellLayout {
private static let imageMarginLeft = CGFloat(integerLiteral: 8) private static let imageMarginLeft = CGFloat(integerLiteral: 8)
private static let imageMarginRight = CGFloat(integerLiteral: 8) private static let imageMarginRight = CGFloat(integerLiteral: 8)
private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8) private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8)
private static let unreadCountMarginRight = CGFloat(integerLiteral: 8) private static let unreadCountMarginRight = CGFloat(integerLiteral: 0)
private static let disclosureButtonSize = CGSize(width: 44, height: 44)
private static let chevronWidth = CGFloat(integerLiteral: 40)
let faviconRect: CGRect let faviconRect: CGRect
let titleRect: CGRect let titleRect: CGRect
let unreadCountRect: CGRect let unreadCountRect: CGRect
let disclosureButtonRect: CGRect
init(cellSize: CGSize, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterUnreadCountView, showingEditingControl: Bool, indent: Bool) { init(cellSize: CGSize, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterUnreadCountView, showingEditingControl: Bool, indent: Bool, shouldShowDisclosure: Bool) {
let adjustedWidth: CGFloat = { let bounds = CGRect(x: 0.0, y: 0.0, width: floor(cellSize.width), height: floor(cellSize.height))
if showingEditingControl {
return floor(cellSize.width)
} else {
return floor(cellSize.width) - MasterTableViewCellLayout.chevronWidth
}
}()
let bounds = CGRect(x: 0.0, y: 0.0, width: adjustedWidth, height: floor(cellSize.height))
// Favicon
var rFavicon = CGRect.zero var rFavicon = CGRect.zero
if shouldShowImage { if shouldShowImage {
var indentX = showingEditingControl ? MasterTableViewCellLayout.imageMarginLeft + 40 : MasterTableViewCellLayout.imageMarginLeft var indentX = showingEditingControl ? MasterTableViewCellLayout.imageMarginLeft + 40 : MasterTableViewCellLayout.imageMarginLeft
@ -44,6 +37,7 @@ struct MasterTableViewCellLayout {
} }
self.faviconRect = rFavicon self.faviconRect = rFavicon
// Title
let labelSize = SingleLineUILabelSizer.size(for: label.text ?? "", font: label.font!) let labelSize = SingleLineUILabelSizer.size(for: label.text ?? "", font: label.font!)
var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSize.width, height: labelSize.height) var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSize.width, height: labelSize.height)
@ -52,26 +46,52 @@ struct MasterTableViewCellLayout {
} }
rLabel = MasterTableViewCellLayout.centerVertically(rLabel, bounds) rLabel = MasterTableViewCellLayout.centerVertically(rLabel, bounds)
// Unread Count
let unreadCountSize = unreadCountView.intrinsicContentSize let unreadCountSize = unreadCountView.intrinsicContentSize
let unreadCountIsHidden = unreadCountView.unreadCount < 1 let unreadCountIsHidden = unreadCountView.unreadCount < 1
var rUnread = CGRect.zero var rUnread = CGRect.zero
if !unreadCountIsHidden { if !unreadCountIsHidden {
rUnread.size = unreadCountSize rUnread.size = unreadCountSize
rUnread.origin.x = (bounds.maxX - unreadCountSize.width) - MasterTableViewCellLayout.unreadCountMarginRight rUnread.origin.x = bounds.maxX -
(unreadCountSize.width + MasterTableViewCellLayout.unreadCountMarginRight + MasterTableViewCellLayout.disclosureButtonSize.width)
rUnread = MasterTableViewCellLayout.centerVertically(rUnread, bounds) rUnread = MasterTableViewCellLayout.centerVertically(rUnread, bounds)
// Cap the Title width based on the unread indicator button
let labelMaxX = rUnread.minX - MasterTableViewCellLayout.unreadCountMarginLeft let labelMaxX = rUnread.minX - MasterTableViewCellLayout.unreadCountMarginLeft
if rLabel.maxX > labelMaxX { if rLabel.maxX > labelMaxX {
rLabel.size.width = labelMaxX - rLabel.minX rLabel.size.width = labelMaxX - rLabel.minX
} }
} }
self.unreadCountRect = rUnread self.unreadCountRect = rUnread
// Disclosure Button
var rDisclosure = CGRect.zero
if shouldShowDisclosure {
rDisclosure.size = MasterTableViewCellLayout.disclosureButtonSize
rDisclosure.origin.x = bounds.maxX - MasterTableViewCellLayout.disclosureButtonSize.width
rDisclosure = MasterTableViewCellLayout.centerVertically(rDisclosure, bounds)
// Cap the Title width based on the disclosure button
let labelMaxX = rDisclosure.minX
if rLabel.maxX > labelMaxX {
rLabel.size.width = labelMaxX - rLabel.minX
}
}
self.disclosureButtonRect = rDisclosure
// Cap the Title width based on total width
if rLabel.maxX > bounds.maxX { if rLabel.maxX > bounds.maxX {
rLabel.size.width = bounds.maxX - rLabel.minX rLabel.size.width = bounds.maxX - rLabel.minX
} }
self.titleRect = rLabel self.titleRect = rLabel
} }
// Ideally this will be implemented in RSCore (see RSGeometry) // Ideally this will be implemented in RSCore (see RSGeometry)