metatext/Views/UIKit/Content Views/NotificationView.swift

265 lines
11 KiB
Swift
Raw Normal View History

2020-10-30 07:11:24 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
2021-02-22 23:59:33 +00:00
import SDWebImage
2020-10-30 07:11:24 +00:00
import UIKit
2021-01-20 02:47:21 +00:00
import ViewModels
2020-10-30 07:11:24 +00:00
2020-10-31 00:29:43 +00:00
final class NotificationView: UIView {
2020-10-30 07:11:24 +00:00
private let iconImageView = UIImageView()
2021-02-22 23:59:33 +00:00
private let avatarImageView = SDAnimatedImageView()
2020-10-31 00:29:43 +00:00
private let avatarButton = UIButton()
2021-02-22 07:10:34 +00:00
private let typeLabel = AnimatedAttachmentLabel()
2021-01-27 02:31:00 +00:00
private let timeLabel = UILabel()
2021-02-22 07:10:34 +00:00
private let displayNameLabel = AnimatedAttachmentLabel()
2020-10-31 00:29:43 +00:00
private let accountLabel = UILabel()
private let statusBodyView = StatusBodyView()
2020-10-30 07:11:24 +00:00
private var notificationConfiguration: NotificationContentConfiguration
init(configuration: NotificationContentConfiguration) {
notificationConfiguration = configuration
super.init(frame: .zero)
initialSetup()
applyNotificationConfiguration()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
2021-01-20 02:47:21 +00:00
extension NotificationView {
static func estimatedHeight(width: CGFloat,
2021-01-26 00:06:35 +00:00
identityContext: IdentityContext,
2021-01-20 02:47:21 +00:00
notification: MastodonNotification,
configuration: CollectionItem.StatusConfiguration?) -> CGFloat {
let bodyWidth = width - .defaultSpacing - .avatarDimension
var height = CGFloat.defaultSpacing * 2
+ UIFont.preferredFont(forTextStyle: .headline).lineHeight
+ .compactSpacing
if let status = notification.status {
height += StatusBodyView.estimatedHeight(
width: bodyWidth,
2021-01-26 00:06:35 +00:00
identityContext: identityContext,
2021-01-20 02:47:21 +00:00
status: status,
configuration: configuration ?? .default)
} else {
height += UIFont.preferredFont(forTextStyle: .headline).lineHeight
+ .compactSpacing
+ UIFont.preferredFont(forTextStyle: .subheadline).lineHeight
}
return height
}
}
2020-10-30 07:11:24 +00:00
extension NotificationView: UIContentView {
var configuration: UIContentConfiguration {
get { notificationConfiguration }
set {
guard let notificationConfiguration = newValue as? NotificationContentConfiguration else { return }
self.notificationConfiguration = notificationConfiguration
applyNotificationConfiguration()
}
}
}
private extension NotificationView {
2020-10-31 00:29:43 +00:00
// swiftlint:disable function_body_length
2020-10-30 07:11:24 +00:00
func initialSetup() {
2020-10-31 00:29:43 +00:00
let containerStackView = UIStackView()
let sideStackView = UIStackView()
2021-01-27 02:31:00 +00:00
let typeTimeStackView = UIStackView()
2020-10-31 00:29:43 +00:00
let mainStackView = UIStackView()
addSubview(containerStackView)
containerStackView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.spacing = .defaultSpacing
2021-01-27 02:31:00 +00:00
containerStackView.alignment = .top
2020-10-31 00:29:43 +00:00
sideStackView.axis = .vertical
sideStackView.alignment = .trailing
sideStackView.spacing = .compactSpacing
sideStackView.addArrangedSubview(iconImageView)
sideStackView.addArrangedSubview(avatarImageView)
containerStackView.addArrangedSubview(sideStackView)
2021-01-27 02:31:00 +00:00
typeTimeStackView.spacing = .compactSpacing
typeTimeStackView.alignment = .top
2020-10-31 00:29:43 +00:00
mainStackView.axis = .vertical
mainStackView.spacing = .compactSpacing
2021-01-27 02:31:00 +00:00
typeTimeStackView.addArrangedSubview(typeLabel)
typeTimeStackView.addArrangedSubview(timeLabel)
mainStackView.addArrangedSubview(typeTimeStackView)
2020-10-31 00:29:43 +00:00
mainStackView.addArrangedSubview(statusBodyView)
mainStackView.addArrangedSubview(displayNameLabel)
mainStackView.addArrangedSubview(accountLabel)
containerStackView.addArrangedSubview(mainStackView)
iconImageView.contentMode = .scaleAspectFit
iconImageView.setContentHuggingPriority(.required, for: .horizontal)
2020-10-30 07:11:24 +00:00
2020-10-31 00:29:43 +00:00
avatarImageView.layer.cornerRadius = .avatarDimension / 2
avatarImageView.clipsToBounds = true
2020-10-30 07:11:24 +00:00
2020-10-31 00:29:43 +00:00
let avatarHeightConstraint = avatarImageView.heightAnchor.constraint(equalToConstant: .avatarDimension)
avatarHeightConstraint.priority = .justBelowMax
2020-10-30 07:11:24 +00:00
2020-10-31 00:29:43 +00:00
avatarButton.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.addSubview(avatarButton)
avatarImageView.isUserInteractionEnabled = true
avatarButton.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
avatarButton.addAction(
UIAction { [weak self] _ in self?.notificationConfiguration.viewModel.accountSelected() },
for: .touchUpInside)
typeLabel.font = .preferredFont(forTextStyle: .headline)
2020-10-30 07:11:24 +00:00
typeLabel.adjustsFontForContentSizeCategory = true
2020-10-31 00:29:43 +00:00
typeLabel.numberOfLines = 0
2021-01-27 02:31:00 +00:00
timeLabel.font = .preferredFont(forTextStyle: .subheadline)
timeLabel.adjustsFontForContentSizeCategory = true
timeLabel.textColor = .secondaryLabel
timeLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
timeLabel.setContentHuggingPriority(.required, for: .horizontal)
2020-10-31 00:29:43 +00:00
statusBodyView.alpha = 0.5
statusBodyView.isUserInteractionEnabled = false
displayNameLabel.font = .preferredFont(forTextStyle: .headline)
displayNameLabel.adjustsFontForContentSizeCategory = true
displayNameLabel.numberOfLines = 0
accountLabel.font = .preferredFont(forTextStyle: .subheadline)
accountLabel.adjustsFontForContentSizeCategory = true
accountLabel.textColor = .secondaryLabel
accountLabel.numberOfLines = 0
2020-10-30 07:11:24 +00:00
NSLayoutConstraint.activate([
2020-10-31 00:29:43 +00:00
containerStackView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
containerStackView.topAnchor.constraint(equalTo: readableContentGuide.topAnchor),
containerStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
containerStackView.bottomAnchor.constraint(equalTo: readableContentGuide.bottomAnchor),
avatarImageView.widthAnchor.constraint(equalToConstant: .avatarDimension),
avatarHeightConstraint,
sideStackView.widthAnchor.constraint(equalToConstant: .avatarDimension),
iconImageView.centerYAnchor.constraint(equalTo: typeLabel.centerYAnchor),
avatarButton.leadingAnchor.constraint(equalTo: avatarImageView.leadingAnchor),
avatarButton.topAnchor.constraint(equalTo: avatarImageView.topAnchor),
avatarButton.bottomAnchor.constraint(equalTo: avatarImageView.bottomAnchor),
avatarButton.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor)
2020-10-30 07:11:24 +00:00
])
2021-02-02 21:04:11 +00:00
isAccessibilityElement = true
2020-10-30 07:11:24 +00:00
}
func applyNotificationConfiguration() {
let viewModel = notificationConfiguration.viewModel
2021-02-22 23:59:33 +00:00
avatarImageView.sd_setImage(with: viewModel.accountViewModel.avatarURL())
2020-10-31 00:29:43 +00:00
2020-10-30 07:11:24 +00:00
switch viewModel.type {
case .follow:
2021-03-24 01:39:04 +00:00
typeLabel.attributedText = "notifications.followed-you-%@".localizedBolding(
2020-10-30 07:11:24 +00:00
displayName: viewModel.accountViewModel.displayName,
2021-01-12 07:33:35 +00:00
emojis: viewModel.accountViewModel.emojis,
2021-02-22 07:10:34 +00:00
label: typeLabel,
identityContext: viewModel.identityContext)
2020-10-30 07:11:24 +00:00
iconImageView.tintColor = nil
case .reblog:
2021-03-24 01:39:04 +00:00
typeLabel.attributedText = "notifications.reblogged-your-status-%@".localizedBolding(
2020-10-30 07:11:24 +00:00
displayName: viewModel.accountViewModel.displayName,
2021-01-12 07:33:35 +00:00
emojis: viewModel.accountViewModel.emojis,
2021-02-22 07:10:34 +00:00
label: typeLabel,
identityContext: viewModel.identityContext)
2020-10-30 07:11:24 +00:00
iconImageView.tintColor = .systemGreen
case .favourite:
2021-03-24 01:39:04 +00:00
typeLabel.attributedText = "notifications.favourited-your-status-%@".localizedBolding(
2020-10-30 07:11:24 +00:00
displayName: viewModel.accountViewModel.displayName,
2021-01-12 07:33:35 +00:00
emojis: viewModel.accountViewModel.emojis,
2021-02-22 07:10:34 +00:00
label: typeLabel,
identityContext: viewModel.identityContext)
2020-10-30 07:11:24 +00:00
iconImageView.tintColor = .systemYellow
case .poll:
typeLabel.text = NSLocalizedString(
viewModel.accountViewModel.isSelf
? "notifications.your-poll-ended"
: "notifications.poll-ended",
comment: "")
iconImageView.tintColor = nil
default:
2021-03-24 01:39:04 +00:00
typeLabel.attributedText = "notifications.unknown-%@".localizedBolding(
2020-10-30 07:11:24 +00:00
displayName: viewModel.accountViewModel.displayName,
2021-01-12 07:33:35 +00:00
emojis: viewModel.accountViewModel.emojis,
2021-02-22 07:10:34 +00:00
label: typeLabel,
identityContext: viewModel.identityContext)
2020-10-30 07:11:24 +00:00
iconImageView.tintColor = nil
}
2020-10-31 00:29:43 +00:00
if viewModel.statusViewModel == nil {
let mutableDisplayName = NSMutableAttributedString(string: viewModel.accountViewModel.displayName)
2021-02-22 07:10:34 +00:00
mutableDisplayName.insert(emojis: viewModel.accountViewModel.emojis,
view: displayNameLabel,
identityContext: viewModel.identityContext)
2020-10-31 00:29:43 +00:00
mutableDisplayName.resizeAttachments(toLineHeight: displayNameLabel.font.lineHeight)
displayNameLabel.attributedText = mutableDisplayName
accountLabel.text = viewModel.accountViewModel.accountName
statusBodyView.isHidden = true
displayNameLabel.isHidden = false
accountLabel.isHidden = false
} else {
statusBodyView.viewModel = viewModel.statusViewModel
statusBodyView.isHidden = false
displayNameLabel.isHidden = true
accountLabel.isHidden = true
}
2021-01-27 02:31:00 +00:00
timeLabel.text = viewModel.time
2021-02-02 21:04:11 +00:00
timeLabel.accessibilityLabel = viewModel.accessibilityTime
2021-01-27 02:31:00 +00:00
2020-10-30 07:11:24 +00:00
iconImageView.image = UIImage(
systemName: viewModel.type.systemImageName,
withConfiguration: UIImage.SymbolConfiguration(scale: .medium))
2021-02-02 21:04:11 +00:00
let accessibilityAttributedLabel = NSMutableAttributedString(string: "")
if let typeText = typeLabel.attributedText {
accessibilityAttributedLabel.appendWithSeparator(typeText)
}
if !statusBodyView.isHidden,
let statusBodyAccessibilityAttributedLabel = statusBodyView.accessibilityAttributedLabel {
accessibilityAttributedLabel.appendWithSeparator(statusBodyAccessibilityAttributedLabel)
} else if !accountLabel.isHidden, let accountText = accountLabel.text {
accessibilityAttributedLabel.appendWithSeparator(accountText)
}
if let accessibilityTime = viewModel.accessibilityTime {
accessibilityAttributedLabel.appendWithSeparator(accessibilityTime)
}
self.accessibilityAttributedLabel = accessibilityAttributedLabel
accessibilityCustomActions = [
UIAccessibilityCustomAction(
name: NSLocalizedString("notification.accessibility.view-profile", comment: "")) { [weak self] _ in
self?.notificationConfiguration.viewModel.accountSelected()
return true
}
]
2020-10-30 07:11:24 +00:00
}
2020-10-31 00:29:43 +00:00
// swiftlint:enable function_body_length
2020-10-30 07:11:24 +00:00
}