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 {
|
|
|
|
func disclosureSelected(_ 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var faviconImage: UIImage? {
|
|
|
|
didSet {
|
2019-09-27 03:40:32 +02:00
|
|
|
faviconImageView.image = faviconImage
|
2019-09-30 02:40:12 +02:00
|
|
|
|
|
|
|
if self.traitCollection.userInterfaceStyle == .dark {
|
|
|
|
DispatchQueue.global(qos: .background).async {
|
|
|
|
if self.faviconImage?.isDark() ?? false {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.faviconImageView.backgroundColor = AppAssets.avatarBackgroundColor
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.faviconImageView.backgroundColor = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
label.font = .preferredFont(forTextStyle: .body)
|
2019-04-15 22:03:05 +02:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
private let faviconImageView: UIImageView = {
|
2019-09-22 00:24:50 +02:00
|
|
|
let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage)
|
|
|
|
imageView.layer.cornerRadius = MasterFeedTableViewCellLayout.faviconCornerRadius
|
|
|
|
imageView.clipsToBounds = true
|
|
|
|
return imageView
|
2019-04-15 22:03:05 +02:00
|
|
|
}()
|
|
|
|
|
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-10-22 10:31:25 +02:00
|
|
|
titleView.highlightedTextColor = AppAssets.vibrantTextColor
|
2019-09-03 07:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
|
|
|
|
super.setHighlighted(highlighted, animated: animated)
|
|
|
|
|
2019-10-22 10:31:25 +02:00
|
|
|
let tintColor = isHighlighted || isSelected ? AppAssets.vibrantTextColor : AppAssets.secondaryAccentColor
|
2019-09-27 03:48:18 +02:00
|
|
|
disclosureButton?.tintColor = tintColor
|
2019-09-03 07:39:01 +02:00
|
|
|
faviconImageView.tintColor = tintColor
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-08-03 23:25:35 +02:00
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
|
|
super.setSelected(selected, animated: animated)
|
2019-09-03 07:39:01 +02:00
|
|
|
|
2019-10-22 10:31:25 +02:00
|
|
|
let tintColor = isHighlighted || isSelected ? AppAssets.vibrantTextColor : AppAssets.secondaryAccentColor
|
2019-09-27 03:48:18 +02:00
|
|
|
disclosureButton?.tintColor = tintColor
|
2019-09-03 07:39:01 +02:00
|
|
|
faviconImageView.tintColor = tintColor
|
2019-08-03 23:25:35 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
delegate?.disclosureSelected(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-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)
|
|
|
|
addSubviewAtInit(faviconImageView)
|
|
|
|
addSubviewAtInit(titleView)
|
2019-04-18 12:41:27 +02:00
|
|
|
addDisclosureView()
|
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-09-27 12:42:16 +02:00
|
|
|
disclosureButton?.setImage(AppAssets.chevronBaseImage, for: .normal)
|
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-04-20 16:50:44 +02:00
|
|
|
faviconImageView.setFrameIfNotEqual(layout.faviconRect)
|
|
|
|
titleView.setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.setFrameIfNotEqual(layout.unreadCountRect)
|
|
|
|
disclosureButton?.setFrameIfNotEqual(layout.disclosureButtonRect)
|
2019-09-27 02:43:17 +02:00
|
|
|
disclosureButton?.isHidden = !isDisclosureAvailable
|
2019-04-28 21:56:36 +02:00
|
|
|
separatorInset = layout.separatorInsets
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|