2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// DataSourceProvider+StatusTableViewCellDelegate.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-02-08 12:50:18 +01:00
|
|
|
import CoreDataStack
|
2022-01-27 14:23:39 +01:00
|
|
|
import MetaTextKit
|
|
|
|
import MastodonUI
|
|
|
|
|
|
|
|
// MARK: - header
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
headerDidPressed header: UIView
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
2022-02-15 11:17:55 +01:00
|
|
|
|
|
|
|
switch await statusView.viewModel.header {
|
|
|
|
case .none:
|
|
|
|
break
|
|
|
|
case .reply:
|
|
|
|
let _replyToAuthor: ManagedObjectRecord<MastodonUser>? = try? await context.managedObjectContext.perform {
|
|
|
|
guard let status = status.object(in: self.context.managedObjectContext) else { return nil }
|
|
|
|
guard let inReplyToAccountID = status.inReplyToAccountID else { return nil }
|
|
|
|
let request = MastodonUser.sortedFetchRequest
|
|
|
|
request.predicate = MastodonUser.predicate(domain: status.author.domain, id: inReplyToAccountID)
|
|
|
|
request.fetchLimit = 1
|
|
|
|
guard let author = self.context.managedObjectContext.safeFetch(request).first else { return nil }
|
|
|
|
return .init(objectID: author.objectID)
|
|
|
|
}
|
|
|
|
guard let replyToAuthor = _replyToAuthor else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await DataSourceFacade.coordinateToProfileScene(
|
|
|
|
provider: self,
|
|
|
|
user: replyToAuthor
|
|
|
|
)
|
|
|
|
|
|
|
|
case .repost:
|
|
|
|
await DataSourceFacade.coordinateToProfileScene(
|
|
|
|
provider: self,
|
|
|
|
target: .reblog, // keep the wrapper for header author
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - avatar button
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
authorAvatarButtonDidPressed button: AvatarButton
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await DataSourceFacade.coordinateToProfileScene(
|
|
|
|
provider: self,
|
|
|
|
target: .status,
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - content
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
metaText: MetaText,
|
|
|
|
didSelectMeta meta: Meta
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try await DataSourceFacade.responseToMetaTextAction(
|
|
|
|
provider: self,
|
|
|
|
target: .status,
|
|
|
|
status: status,
|
|
|
|
meta: meta
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - media
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider & MediaPreviewableViewController {
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
mediaGridContainerView: MediaGridContainerView,
|
|
|
|
mediaView: MediaView,
|
|
|
|
didSelectMediaViewAt index: Int
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
2022-01-29 12:51:40 +01:00
|
|
|
|
2022-04-18 09:33:29 +02:00
|
|
|
let needsToggleMediaSensitive = await !statusView.viewModel.isMediaReveal
|
2022-01-29 12:51:40 +01:00
|
|
|
|
|
|
|
guard !needsToggleMediaSensitive else {
|
2022-02-11 13:21:28 +01:00
|
|
|
try await DataSourceFacade.responseToToggleSensitiveAction(
|
2022-01-29 12:51:40 +01:00
|
|
|
dependency: self,
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
try await DataSourceFacade.coordinateToMediaPreviewScene(
|
|
|
|
dependency: self,
|
|
|
|
status: status,
|
|
|
|
previewContext: DataSourceFacade.AttachmentPreviewContext(
|
|
|
|
containerView: .mediaGridContainerView(mediaGridContainerView),
|
|
|
|
mediaView: mediaView,
|
|
|
|
index: index
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - poll
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
pollTableView tableView: UITableView,
|
|
|
|
didSelectRowAt indexPath: IndexPath
|
|
|
|
) {
|
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
guard let pollTableViewDiffableDataSource = statusView.pollTableViewDiffableDataSource else { return }
|
|
|
|
guard let pollItem = pollTableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
|
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
|
|
|
|
Task {
|
|
|
|
guard case let .option(pollOption) = pollItem else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var _poll: ManagedObjectRecord<Poll>?
|
|
|
|
var _isMultiple: Bool?
|
|
|
|
var _choice: Int?
|
|
|
|
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let pollOption = pollOption.object(in: managedObjectContext) else { return }
|
|
|
|
let poll = pollOption.poll
|
|
|
|
_poll = .init(objectID: poll.objectID)
|
|
|
|
|
|
|
|
_isMultiple = poll.multiple
|
|
|
|
guard !poll.isVoting else { return }
|
|
|
|
|
|
|
|
if !poll.multiple {
|
|
|
|
for option in poll.options where option != pollOption {
|
|
|
|
option.update(isSelected: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark voting
|
|
|
|
poll.update(isVoting: true)
|
|
|
|
// set choice
|
|
|
|
_choice = Int(pollOption.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
pollOption.update(isSelected: !pollOption.isSelected)
|
|
|
|
poll.update(updatedAt: Date())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger vote API request for
|
|
|
|
guard let poll = _poll,
|
|
|
|
_isMultiple == false,
|
|
|
|
let choice = _choice
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
do {
|
|
|
|
_ = try await context.apiService.vote(
|
|
|
|
poll: poll,
|
|
|
|
choices: [choice],
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): vote poll for \(choice) success")
|
|
|
|
} catch {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): vote poll fail: \(error.localizedDescription)")
|
|
|
|
|
|
|
|
// restore voting state
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let pollOption = pollOption.object(in: managedObjectContext) else { return }
|
|
|
|
let poll = pollOption.poll
|
|
|
|
poll.update(isVoting: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
pollVoteButtonPressed button: UIButton
|
|
|
|
) {
|
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
guard let pollTableViewDiffableDataSource = statusView.pollTableViewDiffableDataSource else { return }
|
|
|
|
guard let firstPollItem = pollTableViewDiffableDataSource.snapshot().itemIdentifiers.first else { return }
|
|
|
|
guard case let .option(firstPollOption) = firstPollItem else { return }
|
|
|
|
|
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
|
|
|
|
Task {
|
|
|
|
var _poll: ManagedObjectRecord<Poll>?
|
|
|
|
var _choices: [Int]?
|
|
|
|
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let poll = firstPollOption.object(in: managedObjectContext)?.poll else { return }
|
|
|
|
_poll = .init(objectID: poll.objectID)
|
|
|
|
|
|
|
|
guard poll.multiple else { return }
|
|
|
|
|
|
|
|
// mark voting
|
|
|
|
poll.update(isVoting: true)
|
|
|
|
// set choice
|
|
|
|
_choices = poll.options
|
|
|
|
.filter { $0.isSelected }
|
|
|
|
.map { Int($0.index) }
|
|
|
|
|
|
|
|
poll.update(updatedAt: Date())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger vote API request for
|
|
|
|
guard let poll = _poll,
|
|
|
|
let choices = _choices
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
do {
|
|
|
|
_ = try await context.apiService.vote(
|
|
|
|
poll: poll,
|
|
|
|
choices: choices,
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): vote poll for \(choices) success")
|
|
|
|
} catch {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): vote poll fail: \(error.localizedDescription)")
|
|
|
|
|
|
|
|
// restore voting state
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let poll = poll.object(in: managedObjectContext) else { return }
|
|
|
|
poll.update(isVoting: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - toolbar
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
actionToolbarContainer: ActionToolbarContainer,
|
|
|
|
buttonDidPressed button: UIButton,
|
|
|
|
action: ActionToolbarContainer.Action
|
|
|
|
) {
|
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try await DataSourceFacade.responseToActionToolbar(
|
|
|
|
provider: self,
|
|
|
|
status: status,
|
|
|
|
action: action,
|
|
|
|
authenticationBox: authenticationBox,
|
|
|
|
sender: button
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:02:30 +01:00
|
|
|
// MARK: - menu button
|
2022-01-27 14:23:39 +01:00
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
menuButton button: UIButton,
|
|
|
|
didSelectAction action: MastodonMenu.Action
|
|
|
|
) {
|
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let _author: ManagedObjectRecord<MastodonUser>? = try await self.context.managedObjectContext.perform {
|
|
|
|
guard let _status = status.object(in: self.context.managedObjectContext) else { return nil }
|
|
|
|
let author = (_status.reblog ?? _status).author
|
|
|
|
return .init(objectID: author.objectID)
|
|
|
|
}
|
|
|
|
guard let author = _author else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try await DataSourceFacade.responseToMenuAction(
|
|
|
|
dependency: self,
|
|
|
|
action: action,
|
|
|
|
menuContext: .init(
|
|
|
|
author: author,
|
|
|
|
status: status,
|
|
|
|
button: button,
|
|
|
|
barButtonItem: nil
|
|
|
|
),
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-01-29 10:02:30 +01:00
|
|
|
|
|
|
|
// MARK: - content warning
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
2022-02-11 13:21:28 +01:00
|
|
|
|
2022-01-29 10:02:30 +01:00
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
2022-02-11 13:21:28 +01:00
|
|
|
contentSensitiveeToggleButtonDidPressed button: UIButton
|
2022-02-08 12:50:18 +01:00
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try await DataSourceFacade.responseToToggleSensitiveAction(
|
|
|
|
dependency: self,
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
2022-02-11 13:21:28 +01:00
|
|
|
spoilerOverlayViewDidPressed overlayView: SpoilerOverlayView
|
2022-01-29 10:02:30 +01:00
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try await DataSourceFacade.responseToToggleSensitiveAction(
|
|
|
|
dependency: self,
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
2022-02-09 13:35:19 +01:00
|
|
|
|
2022-02-11 13:21:28 +01:00
|
|
|
// func tableViewCell(
|
|
|
|
// _ cell: UITableViewCell,
|
|
|
|
// statusView: StatusView,
|
|
|
|
// spoilerBannerViewDidPressed bannerView: SpoilerBannerView
|
|
|
|
// ) {
|
|
|
|
// Task {
|
|
|
|
// let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
// guard let item = await item(from: source) else {
|
|
|
|
// assertionFailure()
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// guard case let .status(status) = item else {
|
|
|
|
// assertionFailure("only works for status data provider")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// try await DataSourceFacade.responseToToggleSensitiveAction(
|
|
|
|
// dependency: self,
|
|
|
|
// status: status
|
|
|
|
// )
|
|
|
|
// } // end Task
|
|
|
|
// }
|
|
|
|
|
2022-02-09 13:35:19 +01:00
|
|
|
func tableViewCell(
|
|
|
|
_ cell: UITableViewCell,
|
|
|
|
statusView: StatusView,
|
|
|
|
mediaGridContainerView: MediaGridContainerView,
|
|
|
|
mediaSensitiveButtonDidPressed button: UIButton
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard case let .status(status) = item else {
|
|
|
|
assertionFailure("only works for status data provider")
|
|
|
|
return
|
|
|
|
}
|
2022-02-11 13:21:28 +01:00
|
|
|
try await DataSourceFacade.responseToToggleSensitiveAction(
|
2022-02-09 13:35:19 +01:00
|
|
|
dependency: self,
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:02:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:52 +01:00
|
|
|
// MARK: a11y
|
|
|
|
extension StatusTableViewCellDelegate where Self: DataSourceProvider {
|
|
|
|
func tableViewCell(_ cell: UITableViewCell, statusView: StatusView, accessibilityActivate: Void) {
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(tableViewCell: cell, indexPath: nil)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch item {
|
|
|
|
case .status(let status):
|
|
|
|
await DataSourceFacade.coordinateToStatusThreadScene(
|
|
|
|
provider: self,
|
|
|
|
target: .status, // remove reblog wrapper
|
|
|
|
status: status
|
|
|
|
)
|
|
|
|
case .user(let user):
|
|
|
|
await DataSourceFacade.coordinateToProfileScene(
|
|
|
|
provider: self,
|
|
|
|
user: user
|
|
|
|
)
|
2022-05-07 05:42:10 +02:00
|
|
|
case .notification:
|
2022-02-10 13:01:52 +01:00
|
|
|
assertionFailure("TODO")
|
|
|
|
default:
|
|
|
|
assertionFailure("TODO")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|