2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterTimelineTableViewCell.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/31/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import RSCore
|
|
|
|
|
|
|
|
class MasterTimelineTableViewCell: UITableViewCell {
|
|
|
|
|
|
|
|
private let titleView = MasterTimelineTableViewCell.multiLineUILabel()
|
2019-04-29 21:40:14 +02:00
|
|
|
private let summaryView = MasterTimelineTableViewCell.multiLineUILabel()
|
2019-04-15 22:03:05 +02:00
|
|
|
private let unreadIndicatorView = MasterUnreadIndicatorView(frame: CGRect.zero)
|
|
|
|
private let dateView = MasterTimelineTableViewCell.singleLineUILabel()
|
|
|
|
private let feedNameView = MasterTimelineTableViewCell.singleLineUILabel()
|
|
|
|
|
2019-04-16 16:05:09 +02:00
|
|
|
private lazy var avatarImageView: UIImageView = {
|
2019-04-29 02:27:58 +02:00
|
|
|
let imageView = NonIntrinsicImageView(image: AppAssets.feedImage)
|
|
|
|
imageView.contentMode = .scaleAspectFit
|
|
|
|
return imageView
|
2019-04-15 22:03:05 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
private lazy var starView = {
|
2019-04-26 13:30:00 +02:00
|
|
|
return NonIntrinsicImageView(image: AppAssets.timelineStarImage)
|
2019-04-15 22:03:05 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
var cellData: MasterTimelineCellData! {
|
|
|
|
didSet {
|
|
|
|
updateSubviews()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
override var frame: CGRect {
|
|
|
|
didSet {
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 23:25:35 +02:00
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
|
|
let selectedTextColor = selected ? AppAssets.selectedTextColor : UIColor.label
|
|
|
|
titleView.textColor = selectedTextColor
|
|
|
|
summaryView.textColor = selectedTextColor
|
|
|
|
dateView.textColor = selectedTextColor
|
|
|
|
feedNameView.textColor = selectedTextColor
|
|
|
|
unreadIndicatorView.isSelected = selected
|
|
|
|
|
|
|
|
super.setSelected(selected, animated: animated)
|
|
|
|
}
|
|
|
|
|
2019-04-29 21:40:14 +02:00
|
|
|
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
2019-05-31 23:59:02 +02:00
|
|
|
let layout = updatedLayout(width: size.width)
|
|
|
|
return CGSize(width: size.width, height: layout.height)
|
2019-04-29 21:40:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func layoutSubviews() {
|
|
|
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
2019-05-31 23:59:02 +02:00
|
|
|
let layout = updatedLayout(width: bounds.width)
|
2019-04-30 00:45:12 +02:00
|
|
|
|
2019-05-31 23:59:02 +02:00
|
|
|
unreadIndicatorView.setFrameIfNotEqual(layout.unreadIndicatorRect)
|
|
|
|
starView.setFrameIfNotEqual(layout.starRect)
|
|
|
|
avatarImageView.setFrameIfNotEqual(layout.avatarImageRect)
|
|
|
|
setFrame(for: titleView, rect: layout.titleRect)
|
|
|
|
setFrame(for: summaryView, rect: layout.summaryRect)
|
|
|
|
feedNameView.setFrameIfNotEqual(layout.feedNameRect)
|
|
|
|
dateView.setFrameIfNotEqual(layout.dateRect)
|
2019-04-29 21:40:14 +02:00
|
|
|
|
2019-05-31 23:59:02 +02:00
|
|
|
separatorInset = layout.separatorInsets
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-26 22:37:15 +02:00
|
|
|
func setAvatarImage(_ image: UIImage) {
|
|
|
|
avatarImageView.image = image
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private extension MasterTimelineTableViewCell {
|
|
|
|
|
|
|
|
static func singleLineUILabel() -> UILabel {
|
2019-04-22 18:49:22 +02:00
|
|
|
let label = NonIntrinsicLabel()
|
2019-04-15 22:03:05 +02:00
|
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
label.allowsDefaultTighteningForTruncation = false
|
2019-04-29 21:40:14 +02:00
|
|
|
label.adjustsFontForContentSizeCategory = true
|
2019-04-15 22:03:05 +02:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
|
|
|
|
static func multiLineUILabel() -> UILabel {
|
2019-04-22 18:49:22 +02:00
|
|
|
let label = NonIntrinsicLabel()
|
2019-04-15 22:03:05 +02:00
|
|
|
label.numberOfLines = 0
|
|
|
|
label.lineBreakMode = .byWordWrapping
|
|
|
|
label.allowsDefaultTighteningForTruncation = false
|
2019-04-29 21:40:14 +02:00
|
|
|
label.adjustsFontForContentSizeCategory = true
|
2019-04-15 22:03:05 +02:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFrame(for label: UILabel, rect: CGRect) {
|
|
|
|
|
|
|
|
if Int(floor(rect.height)) == 0 || Int(floor(rect.width)) == 0 {
|
|
|
|
hideView(label)
|
|
|
|
} else {
|
|
|
|
showView(label)
|
2019-04-20 16:50:44 +02:00
|
|
|
label.setFrameIfNotEqual(rect)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSubviewAtInit(_ view: UIView, hidden: Bool) {
|
|
|
|
addSubview(view)
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.isHidden = hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
|
2019-04-21 00:12:39 +02:00
|
|
|
theme()
|
2019-04-17 17:15:44 +02:00
|
|
|
addAccessoryView()
|
2019-04-15 22:03:05 +02:00
|
|
|
addSubviewAtInit(titleView, hidden: false)
|
|
|
|
addSubviewAtInit(summaryView, hidden: true)
|
|
|
|
addSubviewAtInit(unreadIndicatorView, hidden: true)
|
|
|
|
addSubviewAtInit(dateView, hidden: false)
|
|
|
|
addSubviewAtInit(feedNameView, hidden: true)
|
|
|
|
addSubviewAtInit(avatarImageView, hidden: true)
|
|
|
|
addSubviewAtInit(starView, hidden: true)
|
|
|
|
|
|
|
|
}
|
2019-04-21 00:12:39 +02:00
|
|
|
|
|
|
|
func theme() {
|
|
|
|
let bgView = UIView()
|
2019-06-19 01:31:37 +02:00
|
|
|
bgView.backgroundColor = AppAssets.netNewsWireBlueColor
|
2019-04-21 00:12:39 +02:00
|
|
|
selectedBackgroundView = bgView
|
|
|
|
}
|
2019-04-17 17:15:44 +02:00
|
|
|
|
|
|
|
func addAccessoryView() {
|
|
|
|
accessoryView = UIImageView(image: AppAssets.chevronRightImage)
|
|
|
|
}
|
|
|
|
|
2019-05-31 23:59:02 +02:00
|
|
|
func updatedLayout(width: CGFloat) -> MasterTimelineCellLayout {
|
2019-04-30 00:19:08 +02:00
|
|
|
if UIApplication.shared.preferredContentSizeCategory.isAccessibilityCategory {
|
2019-05-31 23:59:02 +02:00
|
|
|
return MasterTimelineAccessibilityCellLayout(width: width, insets: safeAreaInsets, cellData: cellData)
|
2019-04-30 00:19:08 +02:00
|
|
|
} else {
|
2019-05-31 23:59:02 +02:00
|
|
|
return MasterTimelineDefaultCellLayout(width: width, insets: safeAreaInsets, cellData: cellData)
|
2019-04-30 00:19:08 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTitleView() {
|
2019-04-30 00:19:08 +02:00
|
|
|
titleView.font = MasterTimelineDefaultCellLayout.titleFont
|
2019-06-19 00:47:13 +02:00
|
|
|
titleView.textColor = .label
|
2019-04-15 22:03:05 +02:00
|
|
|
updateTextFieldText(titleView, cellData?.title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSummaryView() {
|
2019-04-30 00:19:08 +02:00
|
|
|
summaryView.font = MasterTimelineDefaultCellLayout.summaryFont
|
2019-06-19 00:47:13 +02:00
|
|
|
summaryView.textColor = .label
|
2019-04-29 21:40:14 +02:00
|
|
|
updateTextFieldText(summaryView, cellData?.summary)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateDateView() {
|
2019-04-30 00:19:08 +02:00
|
|
|
dateView.font = MasterTimelineDefaultCellLayout.dateFont
|
2019-06-19 00:47:13 +02:00
|
|
|
dateView.textColor = .secondaryLabel
|
2019-04-15 22:03:05 +02:00
|
|
|
updateTextFieldText(dateView, cellData.dateString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTextFieldText(_ label: UILabel, _ text: String?) {
|
|
|
|
let s = text ?? ""
|
|
|
|
if label.text != s {
|
|
|
|
label.text = s
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateFeedNameView() {
|
|
|
|
|
|
|
|
if cellData.showFeedName {
|
|
|
|
showView(feedNameView)
|
2019-04-30 00:19:08 +02:00
|
|
|
feedNameView.font = MasterTimelineDefaultCellLayout.feedNameFont
|
2019-06-19 00:47:13 +02:00
|
|
|
feedNameView.textColor = .secondaryLabel
|
2019-04-15 22:03:05 +02:00
|
|
|
updateTextFieldText(feedNameView, cellData.feedName)
|
|
|
|
} else {
|
|
|
|
hideView(feedNameView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUnreadIndicator() {
|
|
|
|
showOrHideView(unreadIndicatorView, cellData.read || cellData.starred)
|
2019-06-25 22:43:30 +02:00
|
|
|
unreadIndicatorView.setNeedsDisplay()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateStarView() {
|
|
|
|
showOrHideView(starView, !cellData.starred)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateAvatar() {
|
|
|
|
|
2019-04-29 12:51:47 +02:00
|
|
|
guard let image = cellData.avatar, cellData.showAvatar else {
|
2019-04-15 22:03:05 +02:00
|
|
|
makeAvatarEmpty()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
showView(avatarImageView)
|
2019-04-30 00:19:08 +02:00
|
|
|
avatarImageView.layer.cornerRadius = MasterTimelineDefaultCellLayout.avatarCornerRadius
|
2019-04-15 22:03:05 +02:00
|
|
|
avatarImageView.clipsToBounds = true
|
|
|
|
|
2019-04-29 12:51:47 +02:00
|
|
|
if avatarImageView.image !== cellData.avatar {
|
2019-04-15 22:03:05 +02:00
|
|
|
avatarImageView.image = image
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAvatarEmpty() {
|
|
|
|
|
|
|
|
if avatarImageView.image != nil {
|
|
|
|
avatarImageView.image = nil
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
hideView(avatarImageView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hideView(_ view: UIView) {
|
|
|
|
if !view.isHidden {
|
|
|
|
view.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showView(_ view: UIView) {
|
|
|
|
if view.isHidden {
|
|
|
|
view.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showOrHideView(_ view: UIView, _ shouldHide: Bool) {
|
|
|
|
shouldHide ? hideView(view) : showView(view)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSubviews() {
|
|
|
|
updateTitleView()
|
|
|
|
updateSummaryView()
|
|
|
|
updateDateView()
|
|
|
|
updateFeedNameView()
|
|
|
|
updateUnreadIndicator()
|
|
|
|
updateStarView()
|
|
|
|
updateAvatar()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|