2021-04-13 13:46:42 +02:00
|
|
|
//
|
|
|
|
// ThreadViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-12.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import AVKit
|
2021-07-23 13:10:27 +02:00
|
|
|
import MastodonMeta
|
2021-04-13 13:46:42 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
final class ThreadViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
2021-04-13 13:46:42 +02:00
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: ThreadViewModel!
|
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
let titleView = DoubleTitleLabelNavigationBarTitleView()
|
|
|
|
|
|
|
|
let replyBarButtonItem = AdaptiveUserInterfaceStyleBarButtonItem(
|
|
|
|
lightImage: UIImage(systemName: "arrowshape.turn.up.left")!,
|
|
|
|
darkImage: UIImage(systemName: "arrowshape.turn.up.left.fill")!
|
|
|
|
)
|
|
|
|
|
|
|
|
let tableView: UITableView = {
|
|
|
|
let tableView = ControlContainableTableView()
|
|
|
|
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
|
|
|
|
tableView.register(TimelineMiddleLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self))
|
|
|
|
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
|
|
|
|
tableView.register(ThreadReplyLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: ThreadReplyLoaderTableViewCell.self))
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
|
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ThreadViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-07-06 12:00:39 +02:00
|
|
|
|
|
|
|
view.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
|
2021-07-05 10:07:17 +02:00
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.view.backgroundColor = theme.secondarySystemBackgroundColor
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-13 13:46:42 +02:00
|
|
|
navigationItem.title = L10n.Scene.Thread.backTitle
|
|
|
|
navigationItem.titleView = titleView
|
|
|
|
navigationItem.rightBarButtonItem = replyBarButtonItem
|
|
|
|
replyBarButtonItem.button.addTarget(self, action: #selector(ThreadViewController.replyBarButtonItemPressed(_:)), for: .touchUpInside)
|
|
|
|
|
|
|
|
viewModel.tableView = tableView
|
|
|
|
viewModel.contentOffsetAdjustableTimelineViewControllerDelegate = self
|
|
|
|
tableView.delegate = self
|
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
for: tableView,
|
|
|
|
dependency: self,
|
|
|
|
statusTableViewCellDelegate: self,
|
|
|
|
threadReplyLoaderTableViewCellDelegate: self
|
|
|
|
)
|
|
|
|
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(tableView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
])
|
2021-07-23 13:10:27 +02:00
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
viewModel.navigationBarTitle,
|
|
|
|
viewModel.navigationBarTitleEmojiMeta
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] title, emojiMeta in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let title = title else {
|
|
|
|
self.titleView.update(title: "", subtitle: nil)
|
|
|
|
return
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
2021-08-09 13:44:04 +02:00
|
|
|
let mastodonContent = MastodonContent(content: title, emojis: emojiMeta)
|
2021-07-23 13:10:27 +02:00
|
|
|
do {
|
|
|
|
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
self.titleView.update(titleMetaContent: metaContent, subtitle: nil)
|
|
|
|
} catch {
|
|
|
|
assertionFailure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
aspectViewWillAppear(animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
|
|
|
aspectViewDidDisappear(animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ThreadViewController {
|
|
|
|
@objc private func replyBarButtonItemPressed(_ sender: UIBarButtonItem) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-04-14 09:59:29 +02:00
|
|
|
guard let rootItem = viewModel.rootItem.value,
|
|
|
|
case let .root(statusObjectID, _) = rootItem else { return }
|
|
|
|
let composeViewModel = ComposeViewModel(context: context, composeKind: .reply(repliedToStatusObjectID: statusObjectID))
|
|
|
|
coordinator.present(scene: .compose(viewModel: composeViewModel), from: self, transition: .modal(animated: true, completion: nil))
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - StatusTableViewControllerAspect
|
|
|
|
extension ThreadViewController: StatusTableViewControllerAspect { }
|
|
|
|
|
|
|
|
// MARK: - TableViewCellHeightCacheableContainer
|
|
|
|
extension ThreadViewController: TableViewCellHeightCacheableContainer {
|
|
|
|
var cellFrameCache: NSCache<NSNumber, NSValue> { viewModel.cellFrameCache }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
extension ThreadViewController: UITableViewDelegate {
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
aspectTableView(tableView, estimatedHeightForRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
aspectTableView(tableView, willDisplay: cell, forRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
aspectTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
aspectTableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return nil }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
|
|
|
|
|
|
|
|
// disable root selection
|
|
|
|
switch item {
|
|
|
|
case .root:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return indexPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 13:28:06 +02:00
|
|
|
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
return aspectTableView(tableView, contextMenuConfigurationForRowAt: indexPath, point: point)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
return aspectTableView(tableView, previewForHighlightingContextMenuWithConfiguration: configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
return aspectTableView(tableView, previewForDismissingContextMenuWithConfiguration: configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
aspectTableView(tableView, willPerformPreviewActionForMenuWith: configuration, animator: animator)
|
|
|
|
}
|
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDataSourcePrefetching
|
|
|
|
extension ThreadViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
aspectTableView(tableView, prefetchRowsAt: indexPaths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate
|
|
|
|
extension ThreadViewController: ContentOffsetAdjustableTimelineViewControllerDelegate {
|
|
|
|
func navigationBar() -> UINavigationBar? {
|
|
|
|
return navigationController?.navigationBar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - AVPlayerViewControllerDelegate
|
|
|
|
extension ThreadViewController: AVPlayerViewControllerDelegate {
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
aspectPlayerViewController(playerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
aspectPlayerViewController(playerViewController, willEndFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - statusTableViewCellDelegate
|
|
|
|
extension ThreadViewController: StatusTableViewCellDelegate {
|
|
|
|
weak var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { return self }
|
|
|
|
func parent() -> UIViewController { return self }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - ThreadReplyLoaderTableViewCellDelegate
|
|
|
|
extension ThreadViewController: ThreadReplyLoaderTableViewCellDelegate {
|
|
|
|
func threadReplyLoaderTableViewCell(_ cell: ThreadReplyLoaderTableViewCell, loadMoreButtonDidPressed button: UIButton) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
guard let indexPath = tableView.indexPath(for: cell) else { return }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
guard case let .leafBottomLoader(statusObjectID) = item else { return }
|
|
|
|
|
|
|
|
let nodes = viewModel.descendantNodes.value
|
|
|
|
nodes.forEach { node in
|
|
|
|
expandReply(node: node, statusObjectID: statusObjectID)
|
|
|
|
}
|
|
|
|
viewModel.descendantNodes.value = nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
private func expandReply(node: ThreadViewModel.LeafNode, statusObjectID: NSManagedObjectID) {
|
|
|
|
if node.objectID == statusObjectID {
|
|
|
|
node.isChildrenExpanded = true
|
|
|
|
} else {
|
|
|
|
for child in node.children {
|
|
|
|
expandReply(node: child, statusObjectID: statusObjectID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 09:23:02 +02:00
|
|
|
|
|
|
|
extension ThreadViewController {
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
2021-05-21 10:52:47 +02:00
|
|
|
return navigationKeyCommands + statusNavigationKeyCommands
|
2021-05-21 09:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - StatusTableViewControllerNavigateable
|
|
|
|
extension ThreadViewController: StatusTableViewControllerNavigateable {
|
2021-05-21 10:52:47 +02:00
|
|
|
@objc func navigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
navigateKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func statusKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
statusKeyCommandHandler(sender)
|
2021-05-21 09:23:02 +02:00
|
|
|
}
|
|
|
|
}
|