mastodon-ios/Mastodon/Diffiable/Section/NotificationSection.swift

131 lines
6.0 KiB
Swift
Raw Normal View History

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-02 13:48:56 +02:00
import Nuke
2021-04-12 10:31:53 +02:00
enum NotificationSection: Equatable, Hashable {
case main
}
extension NotificationSection {
static func tableViewDiffableDataSource(
for tableView: UITableView,
2021-04-14 09:00:48 +02:00
managedObjectContext: NSManagedObjectContext,
delegate: NotificationTableViewCellDelegate,
dependency: NeedsDependency
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 {
case .notification(let objectID, let attribute):
guard let notification = try? managedObjectContext.existingObject(with: objectID) as? MastodonNotification else {
return UITableViewCell()
}
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NotificationStatusTableViewCell.self), for: indexPath) as! NotificationStatusTableViewCell
cell.delegate = delegate
// configure author
cell.avatarImageViewTask = Nuke.loadImage(
with: notification.account.avatarImageURL(),
options: ImageLoadingOptions(
placeholder: UIImage.placeholder(color: .systemFill),
transition: .fadeIn(duration: 0.2)
),
into: cell.avatarImageView
)
cell.actionImageView.image = UIImage(
systemName: notification.notificationType.actionImageName,
withConfiguration: UIImage.SymbolConfiguration(
pointSize: 12, weight: .semibold
)
)?.withRenderingMode(.alwaysTemplate)
cell.actionImageBackground.backgroundColor = notification.notificationType.color
// configure author name, notification description, timestamp
cell.nameLabel.configure(content: notification.account.displayNameWithFallback, emojiDict: notification.account.emojiDict)
let createAt = notification.createAt
let actionText = notification.notificationType.actionText
cell.actionLabel.text = actionText + " · " + createAt.timeAgoSinceNow
AppContext.shared.timestampUpdatePublisher
.receive(on: DispatchQueue.main)
.sink { [weak cell] _ in
guard let cell = cell else { return }
cell.actionLabel.text = actionText + " · " + createAt.timeAgoSinceNow
}
.store(in: &cell.disposeBag)
// configure follow request (if exist)
if case .followRequest = notification.notificationType {
2021-04-27 10:54:23 +02:00
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 }
2021-04-27 13:41:55 +02:00
cell.delegate?.notificationTableViewCell(cell, notification: notification, rejectButtonDidPressed: cell.rejectButton)
2021-04-27 10:54:23 +02:00
}
.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,
2021-07-09 13:07:12 +02:00
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
2021-04-13 15:31:49 +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