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
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
class MasterFeedTableViewSectionHeader: UITableViewHeaderFooterView {
|
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")
|
|
|
|
return "\(name) \(unreadCount) \(unreadLabel)"
|
|
|
|
} else {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
setNeedsDisplay()
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:20:25 +02:00
|
|
|
var disclosureExpanded = false {
|
|
|
|
didSet {
|
2019-11-01 17:40:52 +01:00
|
|
|
updateExpandedState()
|
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)
|
2019-04-21 01:20:25 +02:00
|
|
|
private var disclosureView: UIImageView = {
|
2019-08-26 03:06:45 +02:00
|
|
|
let iView = NonIntrinsicImageView()
|
2019-09-27 12:42:16 +02:00
|
|
|
iView.tintColor = UIColor.tertiaryLabel
|
2019-10-25 21:57:01 +02:00
|
|
|
iView.image = AppAssets.disclosureImage
|
2019-04-21 01:20:25 +02:00
|
|
|
iView.contentMode = .center
|
|
|
|
return iView
|
|
|
|
}()
|
|
|
|
|
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()
|
2019-11-05 16:11:43 +01:00
|
|
|
let layout = MasterFeedTableViewSectionHeaderLayout(cellWidth: bounds.size.width, insets: 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
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(unreadCountView)
|
|
|
|
addSubviewAtInit(titleView)
|
2019-11-01 17:40:52 +01:00
|
|
|
updateExpandedState()
|
2019-04-21 01:20:25 +02:00
|
|
|
addSubviewAtInit(disclosureView)
|
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
|
|
|
}
|
|
|
|
|
2019-11-01 17:40:52 +01:00
|
|
|
func updateExpandedState() {
|
|
|
|
if !isLastSection && self.disclosureExpanded {
|
|
|
|
self.bottomSeparatorView.isHidden = false
|
2019-04-21 01:20:25 +02:00
|
|
|
}
|
2019-11-01 17:40:52 +01:00
|
|
|
UIView.animate(
|
|
|
|
withDuration: 0.3,
|
|
|
|
animations: {
|
|
|
|
if self.disclosureExpanded {
|
|
|
|
self.disclosureView.transform = CGAffineTransform(rotationAngle: 1.570796)
|
|
|
|
} else {
|
|
|
|
self.disclosureView.transform = CGAffineTransform(rotationAngle: 0)
|
|
|
|
}
|
|
|
|
}, 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) {
|
|
|
|
addSubview(view)
|
|
|
|
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)
|
2019-04-21 01:20:25 +02:00
|
|
|
disclosureView.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
|
|
|
}
|