2021-04-14 10:24:40 +02:00
|
|
|
//
|
|
|
|
// NotificationStatusTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/14.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import UIKit
|
2021-04-20 07:18:27 +02:00
|
|
|
import ActiveLabel
|
2021-04-14 10:24:40 +02:00
|
|
|
|
2021-04-19 12:06:02 +02:00
|
|
|
final class NotificationStatusTableViewCell: UITableViewCell, StatusCell {
|
2021-04-14 10:24:40 +02:00
|
|
|
static let actionImageBorderWidth: CGFloat = 2
|
2021-04-15 04:16:30 +02:00
|
|
|
static let statusPadding = UIEdgeInsets(top: 50, left: 73, bottom: 24, right: 24)
|
2021-04-14 10:24:40 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-04-14 11:37:58 +02:00
|
|
|
var pollCountdownSubscription: AnyCancellable?
|
2021-04-14 10:24:40 +02:00
|
|
|
var delegate: NotificationTableViewCellDelegate?
|
|
|
|
|
2021-06-15 10:36:42 +02:00
|
|
|
let avatarImageView: UIImageView = {
|
2021-04-14 10:24:40 +02:00
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.layer.cornerRadius = 4
|
|
|
|
imageView.layer.cornerCurve = .continuous
|
|
|
|
imageView.clipsToBounds = true
|
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let actionImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
2021-04-16 16:58:36 +02:00
|
|
|
imageView.tintColor = Asset.Colors.Background.systemBackground.color
|
2021-04-14 10:24:40 +02:00
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let actionImageBackground: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.layer.cornerRadius = (24 + NotificationStatusTableViewCell.actionImageBorderWidth) / 2
|
|
|
|
view.layer.cornerCurve = .continuous
|
|
|
|
view.clipsToBounds = true
|
|
|
|
view.layer.borderWidth = NotificationStatusTableViewCell.actionImageBorderWidth
|
2021-04-16 16:58:36 +02:00
|
|
|
view.layer.borderColor = Asset.Colors.Background.systemBackground.color.cgColor
|
|
|
|
view.tintColor = Asset.Colors.Background.systemBackground.color
|
2021-04-14 10:24:40 +02:00
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
2021-04-16 15:55:09 +02:00
|
|
|
let avatarContainer: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
2021-04-14 10:24:40 +02:00
|
|
|
let actionLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.textColor = Asset.Colors.Label.secondary.color
|
2021-05-10 12:48:04 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .regular), maximumPointSize: 20)
|
2021-04-14 10:24:40 +02:00
|
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let nameLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.textColor = Asset.Colors.brandBlue.color
|
2021-05-10 12:48:04 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 15, weight: .semibold), maximumPointSize: 20)
|
2021-04-14 10:24:40 +02:00
|
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2021-04-15 06:35:40 +02:00
|
|
|
let statusBorder: UIView = {
|
2021-04-14 10:24:40 +02:00
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = .clear
|
|
|
|
view.layer.cornerRadius = 6
|
|
|
|
view.layer.borderWidth = 2
|
|
|
|
view.layer.cornerCurve = .continuous
|
|
|
|
view.layer.borderColor = Asset.Colors.Border.notification.color.cgColor
|
|
|
|
view.clipsToBounds = true
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
let statusView = StatusView()
|
|
|
|
|
2021-05-13 10:21:06 +02:00
|
|
|
let separatorLine = UIView.separatorLine
|
|
|
|
|
|
|
|
var separatorLineToEdgeLeadingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
var separatorLineToEdgeTrailingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
|
|
|
|
var separatorLineToMarginLeadingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
var separatorLineToMarginTrailingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
|
2021-04-14 10:24:40 +02:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
2021-06-15 10:36:42 +02:00
|
|
|
avatarImageView.af.cancelImageRequest()
|
2021-04-20 07:18:27 +02:00
|
|
|
statusView.updateContentWarningDisplay(isHidden: true, animated: false)
|
2021-04-14 10:24:40 +02:00
|
|
|
statusView.pollTableView.dataSource = nil
|
|
|
|
statusView.playerContainerView.reset()
|
|
|
|
statusView.playerContainerView.isHidden = true
|
2021-04-14 14:02:41 +02:00
|
|
|
|
2021-04-14 10:24:40 +02:00
|
|
|
disposeBag.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
configure()
|
|
|
|
}
|
2021-06-23 14:47:49 +02:00
|
|
|
|
2021-04-14 10:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationStatusTableViewCell {
|
|
|
|
func configure() {
|
2021-04-20 07:18:27 +02:00
|
|
|
backgroundColor = Asset.Colors.Background.systemBackground.color
|
|
|
|
|
2021-04-16 15:55:09 +02:00
|
|
|
let containerStackView = UIStackView()
|
|
|
|
containerStackView.axis = .horizontal
|
|
|
|
containerStackView.alignment = .top
|
|
|
|
containerStackView.spacing = 4
|
|
|
|
containerStackView.layoutMargins = UIEdgeInsets(top: 14, left: 0, bottom: 12, right: 0)
|
|
|
|
containerStackView.isLayoutMarginsRelativeArrangement = true
|
|
|
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(containerStackView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
containerStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
|
|
containerStackView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
|
|
|
|
contentView.readableContentGuide.trailingAnchor.constraint(equalTo: containerStackView.trailingAnchor),
|
|
|
|
contentView.bottomAnchor.constraint(equalTo: containerStackView.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
containerStackView.addArrangedSubview(avatarContainer)
|
|
|
|
avatarContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
avatarContainer.heightAnchor.constraint(equalToConstant: 47).priority(.required - 1),
|
|
|
|
avatarContainer.widthAnchor.constraint(equalToConstant: 47).priority(.required - 1)
|
|
|
|
])
|
|
|
|
|
2021-06-15 10:36:42 +02:00
|
|
|
avatarContainer.addSubview(avatarImageView)
|
|
|
|
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
|
2021-04-16 15:55:09 +02:00
|
|
|
NSLayoutConstraint.activate([
|
2021-06-15 10:36:42 +02:00
|
|
|
avatarImageView.heightAnchor.constraint(equalToConstant: 35).priority(.required - 1),
|
|
|
|
avatarImageView.widthAnchor.constraint(equalToConstant: 35).priority(.required - 1),
|
|
|
|
avatarImageView.topAnchor.constraint(equalTo: avatarContainer.topAnchor),
|
|
|
|
avatarImageView.leadingAnchor.constraint(equalTo: avatarContainer.leadingAnchor)
|
2021-04-15 04:37:46 +02:00
|
|
|
])
|
|
|
|
|
2021-04-16 15:55:09 +02:00
|
|
|
avatarContainer.addSubview(actionImageBackground)
|
|
|
|
actionImageBackground.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
actionImageBackground.heightAnchor.constraint(equalToConstant: 24 + NotificationTableViewCell.actionImageBorderWidth).priority(.required - 1),
|
|
|
|
actionImageBackground.widthAnchor.constraint(equalToConstant: 24 + NotificationTableViewCell.actionImageBorderWidth).priority(.required - 1),
|
|
|
|
actionImageBackground.bottomAnchor.constraint(equalTo: avatarContainer.bottomAnchor),
|
|
|
|
actionImageBackground.trailingAnchor.constraint(equalTo: avatarContainer.trailingAnchor)
|
2021-04-14 10:24:40 +02:00
|
|
|
])
|
2021-04-16 15:55:09 +02:00
|
|
|
|
|
|
|
avatarContainer.addSubview(actionImageView)
|
|
|
|
actionImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
actionImageView.centerXAnchor.constraint(equalTo: actionImageBackground.centerXAnchor),
|
|
|
|
actionImageView.centerYAnchor.constraint(equalTo: actionImageBackground.centerYAnchor)
|
|
|
|
])
|
|
|
|
|
|
|
|
let actionStackView = UIStackView()
|
|
|
|
actionStackView.axis = .horizontal
|
2021-04-16 16:58:36 +02:00
|
|
|
actionStackView.distribution = .fill
|
2021-04-16 15:55:09 +02:00
|
|
|
actionStackView.spacing = 4
|
|
|
|
actionStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
nameLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
actionStackView.addArrangedSubview(nameLabel)
|
|
|
|
actionLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
actionStackView.addArrangedSubview(actionLabel)
|
|
|
|
nameLabel.setContentCompressionResistancePriority(.required - 1, for: .vertical)
|
2021-04-16 16:58:36 +02:00
|
|
|
nameLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
|
|
|
actionLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
2021-04-16 15:55:09 +02:00
|
|
|
let statusStackView = UIStackView()
|
|
|
|
statusStackView.axis = .vertical
|
|
|
|
|
|
|
|
statusStackView.distribution = .fill
|
|
|
|
statusStackView.spacing = 4
|
|
|
|
statusStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
statusView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
statusStackView.addArrangedSubview(actionStackView)
|
2021-04-14 10:24:40 +02:00
|
|
|
|
2021-04-16 15:55:09 +02:00
|
|
|
statusBorder.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
statusView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
statusBorder.addSubview(statusView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
statusView.topAnchor.constraint(equalTo: statusBorder.topAnchor, constant: 12),
|
|
|
|
statusView.leadingAnchor.constraint(equalTo: statusBorder.leadingAnchor, constant: 12),
|
|
|
|
statusBorder.bottomAnchor.constraint(equalTo: statusView.bottomAnchor, constant: 12),
|
|
|
|
statusBorder.trailingAnchor.constraint(equalTo: statusView.trailingAnchor, constant: 12),
|
2021-04-14 10:24:40 +02:00
|
|
|
])
|
|
|
|
|
2021-04-20 07:18:27 +02:00
|
|
|
statusView.delegate = self
|
2021-04-16 15:55:09 +02:00
|
|
|
|
|
|
|
statusStackView.addArrangedSubview(statusBorder)
|
|
|
|
|
|
|
|
containerStackView.addArrangedSubview(statusStackView)
|
|
|
|
|
2021-05-13 10:21:06 +02:00
|
|
|
separatorLine.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(separatorLine)
|
|
|
|
separatorLineToEdgeLeadingLayoutConstraint = separatorLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint = separatorLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
|
|
|
|
separatorLineToMarginLeadingLayoutConstraint = separatorLine.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor)
|
|
|
|
separatorLineToMarginTrailingLayoutConstraint = separatorLine.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLine.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
|
|
separatorLine.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: contentView)),
|
|
|
|
])
|
|
|
|
resetSeparatorLineLayout()
|
|
|
|
|
2021-04-14 14:02:41 +02:00
|
|
|
// remove item don't display
|
2021-04-16 15:55:09 +02:00
|
|
|
statusView.actionToolbarContainer.removeFromStackView()
|
2021-04-19 04:10:46 +02:00
|
|
|
// it affect stackView's height,need remove
|
2021-04-16 15:55:09 +02:00
|
|
|
statusView.avatarView.removeFromStackView()
|
|
|
|
statusView.usernameLabel.removeFromStackView()
|
2021-04-14 10:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
2021-05-13 10:21:06 +02:00
|
|
|
|
|
|
|
resetSeparatorLineLayout()
|
2021-04-15 06:35:40 +02:00
|
|
|
statusBorder.layer.borderColor = Asset.Colors.Border.notification.color.cgColor
|
2021-04-16 16:58:36 +02:00
|
|
|
actionImageBackground.layer.borderColor = Asset.Colors.Background.systemBackground.color.cgColor
|
2021-04-14 10:24:40 +02:00
|
|
|
}
|
2021-06-23 14:47:49 +02:00
|
|
|
|
2021-04-20 07:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - StatusViewDelegate
|
|
|
|
extension NotificationStatusTableViewCell: StatusViewDelegate {
|
2021-06-23 14:47:49 +02:00
|
|
|
|
2021-04-20 07:18:27 +02:00
|
|
|
func statusView(_ statusView: StatusView, headerInfoLabelDidPressed label: UILabel) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2021-06-23 14:47:49 +02:00
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, avatarImageViewDidPressed imageView: UIImageView) {
|
2021-04-20 07:18:27 +02:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, revealContentWarningButtonDidPressed button: UIButton) {
|
|
|
|
delegate?.notificationStatusTableViewCell(self, statusView: statusView, revealContentWarningButtonDidPressed: button)
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
|
|
|
|
delegate?.notificationStatusTableViewCell(self, statusView: statusView, contentWarningOverlayViewDidPressed: contentWarningOverlayView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, playerContainerView: PlayerContainerView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
|
|
|
|
delegate?.notificationStatusTableViewCell(self, statusView: statusView, playerContainerView: playerContainerView, contentWarningOverlayViewDidPressed: contentWarningOverlayView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, pollVoteButtonPressed button: UIButton) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusView(_ statusView: StatusView, activeLabel: ActiveLabel, didSelectActiveEntity entity: ActiveEntity) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2021-05-13 10:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationStatusTableViewCell {
|
|
|
|
|
|
|
|
private func resetSeparatorLineLayout() {
|
|
|
|
separatorLineToEdgeLeadingLayoutConstraint.isActive = false
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint.isActive = false
|
|
|
|
separatorLineToMarginLeadingLayoutConstraint.isActive = false
|
|
|
|
separatorLineToMarginTrailingLayoutConstraint.isActive = false
|
|
|
|
|
|
|
|
if traitCollection.userInterfaceIdiom == .phone {
|
|
|
|
// to edge
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToEdgeLeadingLayoutConstraint,
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
if traitCollection.horizontalSizeClass == .compact {
|
|
|
|
// to edge
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToEdgeLeadingLayoutConstraint,
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
// to margin
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToMarginLeadingLayoutConstraint,
|
|
|
|
separatorLineToMarginTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 07:18:27 +02:00
|
|
|
|
2021-04-14 10:24:40 +02:00
|
|
|
}
|