2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterTableViewCell.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/1/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import RSCore
|
|
|
|
import Account
|
|
|
|
import RSTree
|
|
|
|
|
2019-04-17 17:34:10 +02:00
|
|
|
protocol MasterTableViewCellDelegate: class {
|
|
|
|
func disclosureSelected(_ sender: MasterTableViewCell, expanding: Bool)
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
class MasterTableViewCell : UITableViewCell {
|
|
|
|
|
2019-04-17 17:34:10 +02:00
|
|
|
weak var delegate: MasterTableViewCellDelegate?
|
2019-04-17 21:29:52 +02:00
|
|
|
var allowDisclosureSelection = false
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override var accessibilityLabel: String? {
|
|
|
|
set {}
|
|
|
|
get {
|
|
|
|
if unreadCount > 0 {
|
|
|
|
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessiblity")
|
|
|
|
return "\(name) \(unreadCount) \(unreadLabel)"
|
|
|
|
} else {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 01:16:33 +02:00
|
|
|
var indent = false {
|
|
|
|
didSet {
|
|
|
|
if indent != oldValue {
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var disclosureExpanded = false {
|
|
|
|
didSet {
|
2019-04-18 12:41:27 +02:00
|
|
|
updateDisclosureImage()
|
2019-04-18 01:16:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
var faviconImage: UIImage? {
|
|
|
|
didSet {
|
|
|
|
if let image = faviconImage {
|
|
|
|
faviconImageView.image = shouldShowImage ? image : nil
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
faviconImageView.image = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldShowImage = false {
|
|
|
|
didSet {
|
|
|
|
if shouldShowImage != oldValue {
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
faviconImageView.image = shouldShowImage ? faviconImage : nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var unreadCount: Int {
|
|
|
|
get {
|
|
|
|
return unreadCountView.unreadCount
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if unreadCountView.unreadCount != newValue {
|
|
|
|
unreadCountView.unreadCount = newValue
|
|
|
|
unreadCountView.isHidden = (newValue < 1)
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
get {
|
|
|
|
return titleView.text ?? ""
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if titleView.text != newValue {
|
|
|
|
titleView.text = newValue
|
|
|
|
setNeedsDisplay()
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private let titleView: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.numberOfLines = 1
|
|
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
label.allowsDefaultTighteningForTruncation = false
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
private let faviconImageView: UIImageView = {
|
|
|
|
return UIImageView(image: AppAssets.feedImage)
|
|
|
|
}()
|
|
|
|
|
|
|
|
private let unreadCountView = MasterUnreadCountView(frame: CGRect.zero)
|
2019-04-17 14:48:38 +02:00
|
|
|
private var showingEditControl = false
|
2019-04-18 12:41:27 +02:00
|
|
|
private var disclosureButton: UIButton?
|
2019-04-17 21:29:52 +02:00
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
2019-04-15 22:03:05 +02:00
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
2019-04-17 14:48:38 +02:00
|
|
|
override func willTransition(to state: UITableViewCell.StateMask) {
|
|
|
|
super.willTransition(to: state)
|
|
|
|
showingEditControl = state == .showingEditControl
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
2019-04-18 12:41:27 +02:00
|
|
|
let layout = MasterTableViewCellLayout(cellSize: bounds.size, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indent, shouldShowDisclosure: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
layoutWith(layout)
|
|
|
|
}
|
2019-04-17 14:48:38 +02:00
|
|
|
|
2019-04-17 17:15:44 +02:00
|
|
|
@objc func buttonPressed(_ sender: UIButton) {
|
2019-04-18 12:41:27 +02:00
|
|
|
if allowDisclosureSelection {
|
|
|
|
disclosureExpanded = !disclosureExpanded
|
|
|
|
delegate?.disclosureSelected(self, expanding: disclosureExpanded)
|
2019-04-17 21:29:52 +02:00
|
|
|
}
|
2019-04-17 17:15:44 +02:00
|
|
|
}
|
2019-04-17 17:34:10 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension MasterTableViewCell {
|
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(unreadCountView)
|
|
|
|
addSubviewAtInit(faviconImageView)
|
|
|
|
addSubviewAtInit(titleView)
|
2019-04-18 12:41:27 +02:00
|
|
|
addDisclosureView()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-17 17:15:44 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
func addDisclosureView() {
|
|
|
|
|
|
|
|
disclosureButton = UIButton(type: .roundedRect)
|
|
|
|
disclosureButton!.tintColor = AppAssets.chevronDisclosureColor
|
|
|
|
disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside)
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
updateDisclosureImage()
|
|
|
|
addSubviewAtInit(disclosureButton!)
|
2019-04-18 01:16:33 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateDisclosureImage() {
|
2019-04-18 01:16:33 +02:00
|
|
|
if disclosureExpanded {
|
2019-04-18 12:41:27 +02:00
|
|
|
disclosureButton?.setImage(AppAssets.chevronDownImage, for: .normal)
|
2019-04-18 01:16:33 +02:00
|
|
|
} else {
|
2019-04-18 12:41:27 +02:00
|
|
|
disclosureButton?.setImage(AppAssets.chevronRightImage, for: .normal)
|
2019-04-18 01:16:33 +02:00
|
|
|
}
|
2019-04-17 17:15:44 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
func addSubviewAtInit(_ view: UIView) {
|
|
|
|
addSubview(view)
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func layoutWith(_ layout: MasterTableViewCellLayout) {
|
|
|
|
faviconImageView.rs_setFrameIfNotEqual(layout.faviconRect)
|
|
|
|
titleView.rs_setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
|
2019-04-18 12:41:27 +02:00
|
|
|
disclosureButton?.rs_setFrameIfNotEqual(layout.disclosureButtonRect)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UIView {
|
|
|
|
func rs_setFrameIfNotEqual(_ rect: CGRect) {
|
|
|
|
if !self.frame.equalTo(rect) {
|
|
|
|
self.frame = rect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|