151 lines
5.1 KiB
Swift
151 lines
5.1 KiB
Swift
//
|
|
// PollFinishedTableViewCell.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 4/28/21.
|
|
// Copyright © 2021 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
import SwiftSoup
|
|
|
|
class PollFinishedTableViewCell: UITableViewCell {
|
|
|
|
weak var delegate: (TuskerNavigationDelegate & MenuActionProvider)?
|
|
var mastodonController: MastodonController? { delegate?.apiController }
|
|
|
|
@IBOutlet weak var displayNameLabel: EmojiLabel!
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
@IBOutlet weak var statusContentLabel: UILabel!
|
|
@IBOutlet weak var pollView: StatusPollView!
|
|
|
|
var notification: Pachyderm.Notification?
|
|
|
|
private var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
|
|
timestampLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
|
|
.traits: [
|
|
UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue,
|
|
]
|
|
]), size: 0)
|
|
timestampLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
displayNameLabel.font = .preferredFont(forTextStyle: .body).withTraits(.traitBold)!
|
|
displayNameLabel.adjustsFontForContentSizeCategory = true
|
|
}
|
|
|
|
override func updateConfiguration(using state: UICellConfigurationState) {
|
|
var config = UIBackgroundConfiguration.listPlainCell().updated(for: state)
|
|
if state.isHighlighted || state.isSelected {
|
|
config.backgroundColor = .appSelectedCellBackground
|
|
} else {
|
|
config.backgroundColor = .appBackground
|
|
}
|
|
backgroundConfiguration = config
|
|
}
|
|
|
|
func updateUI(notification: Pachyderm.Notification) {
|
|
guard let statusID = notification.status?.id,
|
|
let status = delegate?.apiController.persistentContainer.status(for: statusID),
|
|
let poll = status.poll else {
|
|
return
|
|
}
|
|
|
|
self.notification = notification
|
|
|
|
updateTimestamp()
|
|
|
|
displayNameLabel.text = notification.account.displayName
|
|
displayNameLabel.setEmojis(notification.account.emojis, identifier: notification.account.id)
|
|
|
|
let doc = try! SwiftSoup.parseBodyFragment(status.content)
|
|
statusContentLabel.text = try! doc.text()
|
|
|
|
pollView.mastodonController = mastodonController
|
|
pollView.toastableViewController = delegate
|
|
pollView.updateUI(status: status, poll: poll)
|
|
}
|
|
|
|
private func updateTimestamp() {
|
|
guard let notification = notification else { return }
|
|
|
|
timestampLabel.text = notification.createdAt.timeAgoString()
|
|
|
|
let delay: DispatchTimeInterval?
|
|
switch notification.createdAt.timeAgo().1 {
|
|
case .second:
|
|
delay = .seconds(10)
|
|
case .minute:
|
|
delay = .seconds(60)
|
|
default:
|
|
delay = nil
|
|
}
|
|
if let delay = delay {
|
|
if updateTimestampWorkItem == nil {
|
|
updateTimestampWorkItem = DispatchWorkItem { [weak self] in
|
|
self?.updateTimestamp()
|
|
}
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
}
|
|
} else {
|
|
updateTimestampWorkItem = nil
|
|
}
|
|
}
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
updateTimestampWorkItem = nil
|
|
}
|
|
|
|
// MARK: Accessibility
|
|
|
|
override var accessibilityLabel: String? {
|
|
get {
|
|
guard let notification else { return nil }
|
|
var str = "Poll from "
|
|
str += notification.account.displayNameWithoutCustomEmoji
|
|
str += " finished "
|
|
str += notification.createdAt.formatted(.relative(presentation: .numeric))
|
|
if let poll = notification.status?.poll,
|
|
poll.options.contains(where: { ($0.votesCount ?? 0) > 0 }) {
|
|
let winner = poll.options.max(by: { ($0.votesCount ?? 0) < ($1.votesCount ?? 0) })!
|
|
str += ", winning option: \(winner.title)"
|
|
}
|
|
return str
|
|
}
|
|
set {}
|
|
}
|
|
|
|
}
|
|
|
|
extension PollFinishedTableViewCell: SelectableTableViewCell {
|
|
func didSelectCell() {
|
|
guard let delegate = delegate,
|
|
let status = notification?.status else {
|
|
return
|
|
}
|
|
delegate.selected(status: status.id)
|
|
}
|
|
}
|
|
|
|
extension PollFinishedTableViewCell: MenuPreviewProvider {
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
guard let delegate = delegate,
|
|
let statusID = notification?.status?.id,
|
|
let status = delegate.apiController.persistentContainer.status(for: statusID) else {
|
|
return nil
|
|
}
|
|
return (content: {
|
|
ConversationViewController(for: statusID, state: .unknown, mastodonController: delegate.apiController)
|
|
}, actions: {
|
|
delegate.actionsForStatus(status, source: .view(self))
|
|
})
|
|
}
|
|
}
|