2019-04-18 14:24:55 +02:00
|
|
|
//
|
|
|
|
// MasterTableViewSectionHeader.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/18/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2020-03-25 14:55:02 +01:00
|
|
|
protocol MasterFeedTableViewSectionHeaderDelegate {
|
|
|
|
func masterFeedTableViewSectionHeaderDisclosureDidToggle(_ sender: MasterFeedTableViewSectionHeader)
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
class MasterFeedTableViewSectionHeader: UITableViewHeaderFooterView {
|
2019-04-18 14:24:55 +02:00
|
|
|
|
2020-03-25 14:55:02 +01:00
|
|
|
var delegate: MasterFeedTableViewSectionHeaderDelegate?
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
override var accessibilityLabel: String? {
|
|
|
|
set {}
|
|
|
|
get {
|
|
|
|
if unreadCount > 0 {
|
|
|
|
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessiblity")
|
2020-07-14 14:37:29 +02:00
|
|
|
return "\(name) \(unreadCount) \(unreadLabel) \(expandedStateMessage) "
|
2019-04-18 14:24:55 +02:00
|
|
|
} else {
|
2020-07-14 14:37:29 +02:00
|
|
|
return "\(name) \(expandedStateMessage) "
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 14:37:29 +02:00
|
|
|
|
|
|
|
private var expandedStateMessage: String {
|
|
|
|
set {}
|
|
|
|
get {
|
|
|
|
if disclosureExpanded {
|
|
|
|
return NSLocalizedString("Expanded", comment: "Disclosure button expanded state for accessibility")
|
|
|
|
}
|
2020-07-14 18:34:20 +02:00
|
|
|
return NSLocalizedString("Collapsed", comment: "Disclosure button collapsed state for accessibility")
|
2020-07-14 14:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 14:24:55 +02:00
|
|
|
|
|
|
|
var unreadCount: Int {
|
|
|
|
get {
|
|
|
|
return unreadCountView.unreadCount
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if unreadCountView.unreadCount != newValue {
|
|
|
|
unreadCountView.unreadCount = newValue
|
2019-10-23 15:53:09 +02:00
|
|
|
updateUnreadCountView()
|
2019-04-18 14:24:55 +02:00
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
get {
|
|
|
|
return titleView.text ?? ""
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if titleView.text != newValue {
|
|
|
|
titleView.text = newValue
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:20:25 +02:00
|
|
|
var disclosureExpanded = false {
|
|
|
|
didSet {
|
2020-01-30 21:44:31 +01:00
|
|
|
updateExpandedState(animate: true)
|
2019-10-23 15:53:09 +02:00
|
|
|
updateUnreadCountView()
|
2019-04-21 01:20:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:40:52 +01:00
|
|
|
var isLastSection = false
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
private let titleView: UILabel = {
|
2019-04-27 16:49:26 +02:00
|
|
|
let label = NonIntrinsicLabel()
|
|
|
|
label.numberOfLines = 0
|
2019-04-18 14:24:55 +02:00
|
|
|
label.allowsDefaultTighteningForTruncation = false
|
2019-04-27 16:49:26 +02:00
|
|
|
label.adjustsFontForContentSizeCategory = true
|
|
|
|
label.font = .preferredFont(forTextStyle: .body)
|
2019-04-18 14:24:55 +02:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
private let unreadCountView = MasterFeedUnreadCountView(frame: CGRect.zero)
|
2020-05-01 23:34:19 +02:00
|
|
|
|
2020-03-25 14:55:02 +01:00
|
|
|
private lazy var disclosureButton: UIButton = {
|
|
|
|
let button = NonIntrinsicButton()
|
|
|
|
button.tintColor = UIColor.tertiaryLabel
|
|
|
|
button.setImage(AppAssets.disclosureImage, for: .normal)
|
|
|
|
button.contentMode = .center
|
|
|
|
if #available(iOS 13.4, *) {
|
2020-05-06 21:58:47 +02:00
|
|
|
button.addInteraction(UIPointerInteraction())
|
2020-03-25 14:55:02 +01:00
|
|
|
}
|
|
|
|
button.addTarget(self, action: #selector(toggleDisclosure), for: .touchUpInside)
|
|
|
|
return button
|
2019-04-21 01:20:25 +02:00
|
|
|
}()
|
|
|
|
|
2019-09-03 20:00:31 +02:00
|
|
|
private let topSeparatorView: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = UIColor.separator
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
private let bottomSeparatorView: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = UIColor.separator
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
override init(reuseIdentifier: String?) {
|
|
|
|
super.init(reuseIdentifier: reuseIdentifier)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
2019-11-05 16:11:43 +01:00
|
|
|
let layout = MasterFeedTableViewSectionHeaderLayout(cellWidth: size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView)
|
2019-04-28 18:25:21 +02:00
|
|
|
return CGSize(width: bounds.width, height: layout.height)
|
|
|
|
|
2019-04-28 17:31:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
2020-01-31 01:39:21 +01:00
|
|
|
let layout = MasterFeedTableViewSectionHeaderLayout(cellWidth: contentView.bounds.size.width,
|
|
|
|
insets: contentView.safeAreaInsets,
|
|
|
|
label: titleView,
|
|
|
|
unreadCountView: unreadCountView)
|
2019-04-28 18:25:21 +02:00
|
|
|
layoutWith(layout)
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
2019-04-21 01:20:25 +02:00
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
private extension MasterFeedTableViewSectionHeader {
|
2019-04-18 14:24:55 +02:00
|
|
|
|
2020-03-25 14:55:02 +01:00
|
|
|
@objc func toggleDisclosure() {
|
|
|
|
delegate?.masterFeedTableViewSectionHeaderDisclosureDidToggle(self)
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(unreadCountView)
|
|
|
|
addSubviewAtInit(titleView)
|
2020-03-25 14:55:02 +01:00
|
|
|
addSubviewAtInit(disclosureButton)
|
2020-01-30 21:44:31 +01:00
|
|
|
updateExpandedState(animate: false)
|
2019-06-20 11:24:23 +02:00
|
|
|
addBackgroundView()
|
2019-09-03 20:00:31 +02:00
|
|
|
addSubviewAtInit(topSeparatorView)
|
|
|
|
addSubviewAtInit(bottomSeparatorView)
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 21:44:31 +01:00
|
|
|
func updateExpandedState(animate: Bool) {
|
2019-11-01 17:40:52 +01:00
|
|
|
if !isLastSection && self.disclosureExpanded {
|
|
|
|
self.bottomSeparatorView.isHidden = false
|
2019-04-21 01:20:25 +02:00
|
|
|
}
|
2020-01-30 21:44:31 +01:00
|
|
|
|
|
|
|
let duration = animate ? 0.3 : 0.0
|
|
|
|
|
2019-11-01 17:40:52 +01:00
|
|
|
UIView.animate(
|
2020-01-30 21:44:31 +01:00
|
|
|
withDuration: duration,
|
2019-11-01 17:40:52 +01:00
|
|
|
animations: {
|
|
|
|
if self.disclosureExpanded {
|
2020-03-25 14:55:02 +01:00
|
|
|
self.disclosureButton.transform = CGAffineTransform(rotationAngle: 1.570796)
|
2019-11-01 17:40:52 +01:00
|
|
|
} else {
|
2020-03-25 14:55:02 +01:00
|
|
|
self.disclosureButton.transform = CGAffineTransform(rotationAngle: 0)
|
2019-11-01 17:40:52 +01:00
|
|
|
}
|
|
|
|
}, completion: { _ in
|
|
|
|
if !self.isLastSection && !self.disclosureExpanded {
|
|
|
|
self.bottomSeparatorView.isHidden = true
|
|
|
|
}
|
|
|
|
})
|
2019-04-21 01:20:25 +02:00
|
|
|
}
|
2019-10-23 15:53:09 +02:00
|
|
|
|
|
|
|
func updateUnreadCountView() {
|
|
|
|
if !disclosureExpanded && unreadCount > 0 {
|
2019-11-01 23:48:27 +01:00
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
self.unreadCountView.alpha = 1
|
|
|
|
}
|
2019-10-23 15:53:09 +02:00
|
|
|
} else {
|
2019-11-01 23:48:27 +01:00
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
self.unreadCountView.alpha = 0
|
|
|
|
}
|
2019-10-23 15:53:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 01:20:25 +02:00
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
func addSubviewAtInit(_ view: UIView) {
|
2020-01-30 21:27:17 +01:00
|
|
|
contentView.addSubview(view)
|
2019-04-18 14:24:55 +02:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:11:43 +01:00
|
|
|
func layoutWith(_ layout: MasterFeedTableViewSectionHeaderLayout) {
|
2019-04-20 16:50:44 +02:00
|
|
|
titleView.setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.setFrameIfNotEqual(layout.unreadCountRect)
|
2020-03-25 14:55:02 +01:00
|
|
|
disclosureButton.setFrameIfNotEqual(layout.disclosureButtonRect)
|
2019-09-03 20:00:31 +02:00
|
|
|
|
2019-11-04 01:29:06 +01:00
|
|
|
let top = CGRect(x: safeAreaInsets.left, y: 0, width: frame.width - safeAreaInsets.right - safeAreaInsets.left, height: 0.33)
|
2019-09-03 20:00:31 +02:00
|
|
|
topSeparatorView.setFrameIfNotEqual(top)
|
2019-11-04 01:29:06 +01:00
|
|
|
let bottom = CGRect(x: safeAreaInsets.left, y: frame.height - 0.33, width: frame.width - safeAreaInsets.right - safeAreaInsets.left, height: 0.33)
|
2019-09-03 20:00:31 +02:00
|
|
|
bottomSeparatorView.setFrameIfNotEqual(bottom)
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-20 11:24:23 +02:00
|
|
|
func addBackgroundView() {
|
|
|
|
self.backgroundView = UIView(frame: self.bounds)
|
2019-11-01 15:55:17 +01:00
|
|
|
self.backgroundView?.backgroundColor = AppAssets.sectionHeaderColor
|
2019-06-20 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|