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
|
|
|
|
unreadCountView.isHidden = (newValue < 1)
|
|
|
|
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 {
|
|
|
|
updateDisclosureImage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.lineBreakMode = .byTruncatingTail
|
|
|
|
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 = {
|
|
|
|
let iView = UIImageView()
|
|
|
|
iView.contentMode = .center
|
|
|
|
return iView
|
|
|
|
}()
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
2019-04-23 15:00:27 +02:00
|
|
|
let layout = MasterFeedTableViewCellLayout(cellSize: bounds.size, insets: safeAreaInsets, shouldShowImage: false, label: titleView, unreadCountView: unreadCountView, showingEditingControl: false, indent: true, shouldShowDisclosure: true)
|
2019-04-18 14:24:55 +02:00
|
|
|
layoutWith(layout)
|
|
|
|
}
|
|
|
|
|
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() {
|
2019-04-20 18:43:05 +02:00
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = AppAssets.tableSectionHeaderColor
|
|
|
|
backgroundView = view
|
2019-04-18 14:24:55 +02:00
|
|
|
addSubviewAtInit(unreadCountView)
|
|
|
|
addSubviewAtInit(titleView)
|
2019-04-21 01:20:25 +02:00
|
|
|
updateDisclosureImage()
|
|
|
|
addSubviewAtInit(disclosureView)
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 01:20:25 +02:00
|
|
|
func updateDisclosureImage() {
|
|
|
|
if disclosureExpanded {
|
|
|
|
disclosureView.image = AppAssets.chevronDownImage
|
|
|
|
} else {
|
|
|
|
disclosureView.image = AppAssets.chevronRightImage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:24:55 +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
|
|
|
titleView.setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.setFrameIfNotEqual(layout.unreadCountRect)
|
2019-04-21 01:20:25 +02:00
|
|
|
disclosureView.setFrameIfNotEqual(layout.disclosureButtonRect)
|
2019-04-18 14:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|