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

96 lines
4.2 KiB
Swift
Raw Normal View History

2021-04-12 10:31:53 +02:00
//
// NotificationSection.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/13.
//
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import UIKit
import Combine
enum NotificationSection: Equatable, Hashable {
case main
}
extension NotificationSection {
static func tableViewDiffableDataSource(
for tableView: UITableView,
timestampUpdatePublisher: AnyPublisher<Date, Never>,
managedObjectContext: NSManagedObjectContext
) -> UITableViewDiffableDataSource<NotificationSection, NotificationItem> {
return UITableViewDiffableDataSource(tableView: tableView) { (tableView, indexPath, notificationItem) -> UITableViewCell? in
switch notificationItem {
case .notification(let objectID):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NotificationTableViewCell.self), for: indexPath) as! NotificationTableViewCell
let notification = managedObjectContext.object(with: objectID) as! MastodonNotification
let type = Mastodon.Entity.Notification.NotificationType(rawValue: notification.type)
var actionText: String
var actionImageName: String
2021-04-13 15:31:49 +02:00
var color: UIColor
2021-04-12 10:31:53 +02:00
switch type {
case .follow:
actionText = L10n.Scene.Notification.Action.follow
actionImageName = "person.crop.circle.badge.checkmark"
2021-04-13 15:31:49 +02:00
color = Asset.Colors.brandBlue.color
2021-04-12 10:31:53 +02:00
case .favourite:
actionText = L10n.Scene.Notification.Action.favourite
actionImageName = "star.fill"
2021-04-13 15:31:49 +02:00
color = Asset.Colors.Notification.favourite.color
2021-04-12 10:31:53 +02:00
case .reblog:
actionText = L10n.Scene.Notification.Action.reblog
actionImageName = "arrow.2.squarepath"
2021-04-13 15:31:49 +02:00
color = Asset.Colors.Notification.reblog.color
2021-04-12 10:31:53 +02:00
case .mention:
actionText = L10n.Scene.Notification.Action.mention
actionImageName = "at"
2021-04-13 15:31:49 +02:00
color = Asset.Colors.Notification.mention.color
2021-04-12 10:31:53 +02:00
case .poll:
actionText = L10n.Scene.Notification.Action.poll
actionImageName = "list.bullet"
2021-04-13 15:31:49 +02:00
color = Asset.Colors.brandBlue.color
2021-04-12 10:31:53 +02:00
default:
actionText = ""
actionImageName = ""
2021-04-13 15:31:49 +02:00
color = .clear
2021-04-12 10:31:53 +02:00
}
timestampUpdatePublisher
.sink { _ in
let timeText = notification.createAt.shortTimeAgoSinceNow
cell.actionLabel.text = actionText + " · " + timeText
}
.store(in: &cell.disposeBag)
2021-04-13 15:31:49 +02:00
let timeText = notification.createAt.shortTimeAgoSinceNow
cell.actionImageBackground.backgroundColor = color
cell.actionLabel.text = actionText + " · " + timeText
2021-04-12 10:31:53 +02:00
cell.nameLabel.text = notification.account.displayName
2021-04-13 15:31:49 +02:00
cell.avatatImageView.af.setImage(
withURL: URL(string: notification.account.avatar)!,
placeholderImage: UIImage.placeholder(color: .systemFill),
imageTransition: .crossDissolve(0.2)
)
2021-04-12 10:31:53 +02:00
if let actionImage = UIImage(systemName: actionImageName, withConfiguration: UIImage.SymbolConfiguration(pointSize: 12, weight: .semibold))?.withRenderingMode(.alwaysTemplate) {
cell.actionImageView.image = actionImage
}
2021-04-13 15:31:49 +02:00
if let _ = notification.status {
cell.nameLabelLayoutIn(center: true)
} else {
cell.nameLabelLayoutIn(center: false)
}
2021-04-12 10:31:53 +02:00
return cell
case .bottomLoader:
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SearchBottomLoader.self)) as! SearchBottomLoader
cell.startAnimating()
return cell
}
}
}
}