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-23 15:00:27 +02:00
|
|
|
protocol MasterFeedTableViewCellDelegate: class {
|
2020-03-25 14:55:02 +01:00
|
|
|
func masterFeedTableViewCellDisclosureDidToggle(_ sender: MasterFeedTableViewCell, expanding: Bool)
|
2019-04-17 17:34:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-22 09:35:47 +02:00
|
|
|
class MasterFeedTableViewCell : VibrantTableViewCell {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
weak var delegate: MasterFeedTableViewCellDelegate?
|
2019-09-27 18:54:50 +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-11-06 01:05:57 +01:00
|
|
|
var iconImage: IconImage? {
|
2019-04-15 22:03:05 +02:00
|
|
|
didSet {
|
2019-11-06 01:05:57 +01:00
|
|
|
iconView.iconImage = iconImage
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 18:54:50 +02:00
|
|
|
var isDisclosureAvailable = false {
|
|
|
|
didSet {
|
|
|
|
if isDisclosureAvailable != oldValue {
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 01:52:50 +01:00
|
|
|
var isSeparatorShown = true {
|
|
|
|
didSet {
|
|
|
|
if isSeparatorShown != oldValue {
|
|
|
|
if isSeparatorShown {
|
|
|
|
showView(bottomSeparatorView)
|
|
|
|
} else {
|
|
|
|
hideView(bottomSeparatorView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
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
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private let titleView: UILabel = {
|
2019-04-22 18:49:22 +02:00
|
|
|
let label = NonIntrinsicLabel()
|
2019-04-27 16:49:26 +02:00
|
|
|
label.numberOfLines = 0
|
2019-04-15 22:03:05 +02:00
|
|
|
label.allowsDefaultTighteningForTruncation = false
|
2019-04-27 16:49:26 +02:00
|
|
|
label.adjustsFontForContentSizeCategory = true
|
2019-10-30 08:41:33 +01:00
|
|
|
label.lineBreakMode = .byTruncatingTail
|
2019-04-27 16:49:26 +02:00
|
|
|
label.font = .preferredFont(forTextStyle: .body)
|
2019-04-15 22:03:05 +02:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
private let iconView = IconView()
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-10-29 01:52:50 +01:00
|
|
|
private let bottomSeparatorView: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = UIColor.separator
|
2019-11-17 23:14:25 +01:00
|
|
|
view.alpha = 0.5
|
2019-10-29 01:52:50 +01:00
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
2019-09-27 18:54:50 +02:00
|
|
|
private var isDisclosureExpanded = false
|
|
|
|
private var disclosureButton: UIButton?
|
2019-04-28 17:31:35 +02:00
|
|
|
private var unreadCountView = MasterFeedUnreadCountView(frame: CGRect.zero)
|
2019-09-27 18:54:50 +02:00
|
|
|
private var isShowingEditControl = false
|
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-09-03 07:39:01 +02:00
|
|
|
|
2019-09-27 18:54:50 +02:00
|
|
|
func setDisclosure(isExpanded: Bool, animated: Bool) {
|
|
|
|
isDisclosureExpanded = isExpanded
|
|
|
|
let duration = animated ? 0.3 : 0.0
|
|
|
|
|
|
|
|
UIView.animate(withDuration: duration) {
|
|
|
|
if self.isDisclosureExpanded {
|
2019-09-30 20:32:54 +02:00
|
|
|
self.disclosureButton?.accessibilityLabel = NSLocalizedString("Collapse Folder", comment: "Collapse Folder")
|
2019-09-27 18:54:50 +02:00
|
|
|
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 1.570796)
|
|
|
|
} else {
|
2019-09-30 20:32:54 +02:00
|
|
|
self.disclosureButton?.accessibilityLabel = NSLocalizedString("Expand Folder", comment: "Expand Folder")
|
2019-09-27 18:54:50 +02:00
|
|
|
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 07:39:01 +02:00
|
|
|
override func applyThemeProperties() {
|
|
|
|
super.applyThemeProperties()
|
|
|
|
}
|
|
|
|
|
2019-04-17 14:48:38 +02:00
|
|
|
override func willTransition(to state: UITableViewCell.StateMask) {
|
|
|
|
super.willTransition(to: state)
|
2019-09-27 18:54:50 +02:00
|
|
|
isShowingEditControl = state.contains(.showingEditControl)
|
2019-04-17 14:48:38 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
2019-09-27 18:54:50 +02:00
|
|
|
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: isShowingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable)
|
2019-04-28 19:08:50 +02:00
|
|
|
return CGSize(width: bounds.width, height: layout.height)
|
2019-04-28 17:31:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
2019-09-27 18:54:50 +02:00
|
|
|
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: isShowingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable)
|
2019-04-28 19:08:50 +02:00
|
|
|
layoutWith(layout)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-17 14:48:38 +02:00
|
|
|
|
2019-04-17 17:15:44 +02:00
|
|
|
@objc func buttonPressed(_ sender: UIButton) {
|
2019-09-27 02:43:17 +02:00
|
|
|
if isDisclosureAvailable {
|
2019-09-27 18:54:50 +02:00
|
|
|
setDisclosure(isExpanded: !isDisclosureExpanded, animated: true)
|
2020-03-25 14:55:02 +01:00
|
|
|
delegate?.masterFeedTableViewCellDisclosureDidToggle(self, expanding: isDisclosureExpanded)
|
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-11-03 01:57:01 +01:00
|
|
|
override func updateVibrancy(animated: Bool) {
|
|
|
|
super.updateVibrancy(animated: animated)
|
2020-08-12 03:41:20 +02:00
|
|
|
|
|
|
|
let iconTintColor: UIColor
|
|
|
|
if isHighlighted || isSelected {
|
|
|
|
iconTintColor = AppAssets.vibrantTextColor
|
|
|
|
} else {
|
|
|
|
if let preferredColor = iconImage?.preferredColor {
|
|
|
|
iconTintColor = UIColor(cgColor: preferredColor)
|
|
|
|
} else {
|
|
|
|
iconTintColor = AppAssets.secondaryAccentColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 01:56:33 +02:00
|
|
|
if animated {
|
|
|
|
UIView.animate(withDuration: Self.duration) {
|
|
|
|
self.iconView.tintColor = iconTintColor
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-06 01:05:57 +01:00
|
|
|
self.iconView.tintColor = iconTintColor
|
2019-11-03 01:57:01 +01:00
|
|
|
}
|
2020-08-12 03:41:20 +02:00
|
|
|
|
2019-11-04 18:47:44 +01:00
|
|
|
updateLabelVibrancy(titleView, color: labelColor, animated: animated)
|
2019-11-03 01:57:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
private extension MasterFeedTableViewCell {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(unreadCountView)
|
2019-11-06 01:05:57 +01:00
|
|
|
addSubviewAtInit(iconView)
|
2019-04-15 22:03:05 +02:00
|
|
|
addSubviewAtInit(titleView)
|
2019-04-18 12:41:27 +02:00
|
|
|
addDisclosureView()
|
2019-10-29 01:52:50 +01:00
|
|
|
addSubviewAtInit(bottomSeparatorView)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-21 00:12:39 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
func addDisclosureView() {
|
2019-08-26 03:00:34 +02:00
|
|
|
disclosureButton = NonIntrinsicButton(type: .roundedRect)
|
2019-04-18 12:41:27 +02:00
|
|
|
disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside)
|
2019-10-25 21:57:01 +02:00
|
|
|
disclosureButton?.setImage(AppAssets.disclosureImage, for: .normal)
|
2019-10-30 01:29:37 +01:00
|
|
|
disclosureButton?.tintColor = AppAssets.controlBackgroundColor
|
2019-10-28 21:08:00 +01:00
|
|
|
disclosureButton?.imageView?.contentMode = .center
|
|
|
|
disclosureButton?.imageView?.clipsToBounds = false
|
2020-03-25 14:55:02 +01:00
|
|
|
if #available(iOS 13.4, *) {
|
2020-03-26 17:20:13 +01:00
|
|
|
disclosureButton?.addInteraction(UIPointerInteraction())
|
2020-03-25 14:55:02 +01:00
|
|
|
}
|
2019-04-18 12:41:27 +02:00
|
|
|
addSubviewAtInit(disclosureButton!)
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
func addSubviewAtInit(_ view: UIView) {
|
|
|
|
addSubview(view)
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
func layoutWith(_ layout: MasterFeedTableViewCellLayout) {
|
2019-11-06 01:05:57 +01:00
|
|
|
iconView.setFrameIfNotEqual(layout.faviconRect)
|
2019-04-20 16:50:44 +02:00
|
|
|
titleView.setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.setFrameIfNotEqual(layout.unreadCountRect)
|
|
|
|
disclosureButton?.setFrameIfNotEqual(layout.disclosureButtonRect)
|
2019-09-27 02:43:17 +02:00
|
|
|
disclosureButton?.isHidden = !isDisclosureAvailable
|
2019-10-29 01:52:50 +01:00
|
|
|
bottomSeparatorView.setFrameIfNotEqual(layout.separatorRect)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-10-23 18:47:21 +02:00
|
|
|
|
2019-10-29 01:52:50 +01:00
|
|
|
func hideView(_ view: UIView) {
|
|
|
|
if !view.isHidden {
|
|
|
|
view.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showView(_ view: UIView) {
|
|
|
|
if view.isHidden {
|
|
|
|
view.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|