2021-04-12 10:31:53 +02:00
|
|
|
//
|
|
|
|
// NotificationSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/13.
|
|
|
|
//
|
|
|
|
|
2021-04-15 04:16:30 +02:00
|
|
|
import Combine
|
2021-04-12 10:31:53 +02:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
import UIKit
|
2021-07-23 13:10:27 +02:00
|
|
|
import MetaTextKit
|
|
|
|
import MastodonMeta
|
2021-04-12 10:31:53 +02:00
|
|
|
|
|
|
|
enum NotificationSection: Equatable, Hashable {
|
|
|
|
case main
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationSection {
|
|
|
|
static func tableViewDiffableDataSource(
|
|
|
|
for tableView: UITableView,
|
2021-11-02 07:28:21 +01:00
|
|
|
dependency: NeedsDependency,
|
2021-04-14 09:00:48 +02:00
|
|
|
managedObjectContext: NSManagedObjectContext,
|
|
|
|
delegate: NotificationTableViewCellDelegate,
|
2021-11-02 07:28:21 +01:00
|
|
|
statusTableViewCellDelegate: StatusTableViewCellDelegate
|
2021-04-12 10:31:53 +02:00
|
|
|
) -> UITableViewDiffableDataSource<NotificationSection, NotificationItem> {
|
2021-04-15 04:16:30 +02:00
|
|
|
UITableViewDiffableDataSource(tableView: tableView) {
|
|
|
|
[weak delegate, weak dependency]
|
2021-04-14 09:00:48 +02:00
|
|
|
(tableView, indexPath, notificationItem) -> UITableViewCell? in
|
|
|
|
guard let dependency = dependency else { return nil }
|
2021-04-12 10:31:53 +02:00
|
|
|
switch notificationItem {
|
2021-04-20 07:18:27 +02:00
|
|
|
case .notification(let objectID, let attribute):
|
2021-07-12 13:19:24 +02:00
|
|
|
guard let notification = try? managedObjectContext.existingObject(with: objectID) as? MastodonNotification,
|
2021-11-02 07:28:21 +01:00
|
|
|
!notification.isDeleted
|
|
|
|
else { return UITableViewCell() }
|
2021-07-02 21:22:17 +02:00
|
|
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NotificationStatusTableViewCell.self), for: indexPath) as! NotificationStatusTableViewCell
|
2021-11-02 07:28:21 +01:00
|
|
|
configure(
|
|
|
|
tableView: tableView,
|
|
|
|
cell: cell,
|
|
|
|
notification: notification,
|
|
|
|
dependency: dependency,
|
|
|
|
attribute: attribute
|
2021-07-02 21:22:17 +02:00
|
|
|
)
|
2021-11-02 07:28:21 +01:00
|
|
|
cell.delegate = delegate
|
|
|
|
return cell
|
2021-07-20 13:59:09 +02:00
|
|
|
|
2021-11-02 07:28:21 +01:00
|
|
|
case .notificationStatus(objectID: let objectID, attribute: let attribute):
|
|
|
|
guard let notification = try? managedObjectContext.existingObject(with: objectID) as? MastodonNotification,
|
|
|
|
!notification.isDeleted,
|
|
|
|
let status = notification.status,
|
|
|
|
let requestUserID = dependency.context.authenticationService.activeMastodonAuthenticationBox.value?.userID
|
|
|
|
else { return UITableViewCell() }
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as! StatusTableViewCell
|
2021-08-10 12:33:13 +02:00
|
|
|
|
2021-11-02 07:28:21 +01:00
|
|
|
// configure cell
|
|
|
|
StatusSection.configureStatusTableViewCell(
|
|
|
|
cell: cell,
|
|
|
|
tableView: tableView,
|
|
|
|
timelineContext: .notifications,
|
|
|
|
dependency: dependency,
|
|
|
|
readableLayoutFrame: tableView.readableContentGuide.layoutFrame,
|
|
|
|
status: status,
|
|
|
|
requestUserID: requestUserID,
|
|
|
|
statusItemAttribute: attribute
|
|
|
|
)
|
|
|
|
cell.statusView.headerContainerView.isHidden = true // set header hide
|
|
|
|
cell.statusView.actionToolbarContainer.isHidden = true // set toolbar hide
|
|
|
|
cell.statusView.actionToolbarPlaceholderPaddingView.isHidden = false
|
|
|
|
cell.delegate = statusTableViewCellDelegate
|
|
|
|
cell.isAccessibilityElement = true
|
|
|
|
StatusSection.configureStatusAccessibilityLabel(cell: cell)
|
2021-07-02 21:22:17 +02:00
|
|
|
return cell
|
|
|
|
|
2021-04-12 10:31:53 +02:00
|
|
|
case .bottomLoader:
|
2021-04-16 07:45:54 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self)) as! TimelineBottomLoaderTableViewCell
|
2021-04-12 10:31:53 +02:00
|
|
|
cell.startAnimating()
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 09:00:48 +02:00
|
|
|
|
2021-11-02 07:28:21 +01:00
|
|
|
extension NotificationSection {
|
|
|
|
static func configure(
|
|
|
|
tableView: UITableView,
|
|
|
|
cell: NotificationStatusTableViewCell,
|
|
|
|
notification: MastodonNotification,
|
|
|
|
dependency: NeedsDependency,
|
|
|
|
attribute: Item.StatusAttribute
|
|
|
|
) {
|
|
|
|
// configure author
|
|
|
|
cell.configure(
|
|
|
|
with: AvatarConfigurableViewConfiguration(
|
|
|
|
avatarImageURL: notification.account.avatarImageURL()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func createActionImage() -> UIImage? {
|
|
|
|
return UIImage(
|
|
|
|
systemName: notification.notificationType.actionImageName,
|
|
|
|
withConfiguration: UIImage.SymbolConfiguration(
|
|
|
|
pointSize: 12, weight: .semibold
|
|
|
|
)
|
|
|
|
)?
|
|
|
|
.withTintColor(.systemBackground)
|
|
|
|
.af.imageAspectScaled(toFit: CGSize(width: 14, height: 14))
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.avatarButton.badgeImageView.backgroundColor = notification.notificationType.color
|
|
|
|
cell.avatarButton.badgeImageView.image = createActionImage()
|
|
|
|
cell.traitCollectionDidChange
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak cell] in
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
cell.avatarButton.badgeImageView.image = createActionImage()
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
|
|
|
|
// configure author name, notification description, timestamp
|
|
|
|
let nameText = notification.account.displayNameWithFallback
|
|
|
|
let titleLabelText: String = {
|
|
|
|
switch notification.notificationType {
|
|
|
|
case .favourite: return L10n.Scene.Notification.userFavoritedYourPost(nameText)
|
|
|
|
case .follow: return L10n.Scene.Notification.userFollowedYou(nameText)
|
|
|
|
case .followRequest: return L10n.Scene.Notification.userRequestedToFollowYou(nameText)
|
|
|
|
case .mention: return L10n.Scene.Notification.userMentionedYou(nameText)
|
|
|
|
case .poll: return L10n.Scene.Notification.userYourPollHasEnded(nameText)
|
|
|
|
case .reblog: return L10n.Scene.Notification.userRebloggedYourPost(nameText)
|
|
|
|
default: return ""
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
do {
|
|
|
|
let nameContent = MastodonContent(content: nameText, emojis: notification.account.emojiMeta)
|
|
|
|
let nameMetaContent = try MastodonMetaContent.convert(document: nameContent)
|
|
|
|
|
|
|
|
let mastodonContent = MastodonContent(content: titleLabelText, emojis: notification.account.emojiMeta)
|
|
|
|
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
|
|
|
|
|
|
|
if let nameRange = metaContent.string.range(of: nameMetaContent.string) {
|
|
|
|
let nsRange = NSRange(nameRange, in: metaContent.string)
|
|
|
|
cell.titleLabel.textStorage.addAttributes([
|
|
|
|
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 15, weight: .semibold), maximumPointSize: 20),
|
|
|
|
.foregroundColor: Asset.Colors.brandBlue.color,
|
|
|
|
], range: nsRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
let metaContent = PlaintextMetaContent(string: titleLabelText)
|
|
|
|
cell.titleLabel.configure(content: metaContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
let createAt = notification.createAt
|
|
|
|
cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow
|
|
|
|
AppContext.shared.timestampUpdatePublisher
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak cell] _ in
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
cell.timestampLabel.text = createAt.localizedSlowedTimeAgoSinceNow
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
|
|
|
|
// configure follow request (if exist)
|
|
|
|
if case .followRequest = notification.notificationType {
|
|
|
|
cell.acceptButton.publisher(for: .touchUpInside)
|
|
|
|
.sink { [weak cell] _ in
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
cell.delegate?.notificationTableViewCell(cell, notification: notification, acceptButtonDidPressed: cell.acceptButton)
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
cell.rejectButton.publisher(for: .touchUpInside)
|
|
|
|
.sink { [weak cell] _ in
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
cell.delegate?.notificationTableViewCell(cell, notification: notification, rejectButtonDidPressed: cell.rejectButton)
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
cell.buttonStackView.isHidden = false
|
|
|
|
} else {
|
|
|
|
cell.buttonStackView.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure status (if exist)
|
|
|
|
if let status = notification.status {
|
|
|
|
let frame = CGRect(
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: tableView.readableContentGuide.layoutFrame.width - NotificationStatusTableViewCell.statusPadding.left - NotificationStatusTableViewCell.statusPadding.right,
|
|
|
|
height: tableView.readableContentGuide.layoutFrame.height
|
|
|
|
)
|
|
|
|
StatusSection.configure(
|
|
|
|
cell: cell,
|
|
|
|
tableView: tableView,
|
|
|
|
timelineContext: .notifications,
|
|
|
|
dependency: dependency,
|
|
|
|
readableLayoutFrame: frame,
|
|
|
|
status: status,
|
|
|
|
requestUserID: notification.userID,
|
|
|
|
statusItemAttribute: attribute
|
|
|
|
)
|
|
|
|
cell.statusContainerView.isHidden = false
|
|
|
|
cell.containerStackView.alignment = .top
|
|
|
|
cell.containerStackViewBottomLayoutConstraint.constant = 0
|
|
|
|
} else {
|
|
|
|
if case .followRequest = notification.notificationType {
|
|
|
|
cell.containerStackView.alignment = .top
|
|
|
|
} else {
|
|
|
|
cell.containerStackView.alignment = .center
|
|
|
|
}
|
|
|
|
cell.statusContainerView.isHidden = true
|
|
|
|
cell.containerStackViewBottomLayoutConstraint.constant = 5 // 5pt margin when no status view
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|