2021-04-01 04:12:57 +02:00
|
|
|
//
|
|
|
|
// HashtagTimelineViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/3/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import AVKit
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import CoreData
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
final class HashtagTimelineViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "HashtagTimelineViewController", category: "ViewController")
|
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: HashtagTimelineViewModel!
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
let composeBarButtonItem: UIBarButtonItem = {
|
|
|
|
let barButtonItem = UIBarButtonItem()
|
2022-05-06 09:17:26 +02:00
|
|
|
barButtonItem.image = Asset.ObjectsAndTools.squareAndPencil.image.withRenderingMode(.alwaysTemplate)
|
2021-04-01 04:12:57 +02:00
|
|
|
return barButtonItem
|
|
|
|
}()
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let titleView = DoubleTitleLabelNavigationBarTitleView()
|
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
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.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 HashtagTimelineViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let _title = "#\(viewModel.hashtag)"
|
|
|
|
title = _title
|
|
|
|
titleView.update(title: _title, subtitle: nil)
|
2021-04-02 10:38:33 +02:00
|
|
|
navigationItem.titleView = titleView
|
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-01 04:12:57 +02:00
|
|
|
|
|
|
|
navigationItem.rightBarButtonItem = composeBarButtonItem
|
|
|
|
composeBarButtonItem.target = self
|
|
|
|
composeBarButtonItem.action = #selector(HashtagTimelineViewController.composeBarButtonItemPressed(_:))
|
|
|
|
|
|
|
|
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),
|
|
|
|
])
|
|
|
|
|
|
|
|
tableView.delegate = self
|
|
|
|
viewModel.setupDiffableDataSource(
|
2022-01-27 14:23:39 +01:00
|
|
|
tableView: tableView,
|
|
|
|
statusTableViewCellDelegate: self
|
2021-04-01 04:12:57 +02:00
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// setup batch fetch
|
|
|
|
viewModel.listBatchFetchViewModel.setup(scrollView: tableView)
|
|
|
|
viewModel.listBatchFetchViewModel.shouldFetch
|
2021-04-01 04:12:57 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-27 14:23:39 +01:00
|
|
|
.sink { [weak self] _ in
|
2021-04-01 04:12:57 +02:00
|
|
|
guard let self = self else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
self.viewModel.loadOldestStateMachine.enter(HashtagTimelineViewModel.LoadOldestState.Loading.self)
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-02 04:21:51 +02:00
|
|
|
|
|
|
|
viewModel.hashtagEntity
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] tag in
|
|
|
|
self?.updatePromptTitle()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
2021-04-08 11:05:10 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
}
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
extension HashtagTimelineViewController {
|
2021-04-02 04:21:51 +02:00
|
|
|
|
|
|
|
private func updatePromptTitle() {
|
2021-04-02 10:38:33 +02:00
|
|
|
var subtitle: String?
|
|
|
|
defer {
|
2021-07-23 13:10:27 +02:00
|
|
|
titleView.update(title: "#" + viewModel.hashtag, subtitle: subtitle)
|
2021-04-02 10:38:33 +02:00
|
|
|
}
|
2021-04-02 04:21:51 +02:00
|
|
|
guard let histories = viewModel.hashtagEntity.value?.history else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if histories.isEmpty {
|
|
|
|
// No tag history, remove the prompt title
|
2021-04-02 10:38:33 +02:00
|
|
|
return
|
2021-04-02 04:21:51 +02:00
|
|
|
} else {
|
|
|
|
let sortedHistory = histories.sorted { (h1, h2) -> Bool in
|
|
|
|
return h1.day > h2.day
|
|
|
|
}
|
2021-04-02 11:05:06 +02:00
|
|
|
let peopleTalkingNumber = sortedHistory
|
|
|
|
.prefix(2)
|
|
|
|
.compactMap({ Int($0.accounts) })
|
|
|
|
.reduce(0, +)
|
2021-08-09 13:44:04 +02:00
|
|
|
subtitle = L10n.Plural.peopleTalking(peopleTalkingNumber)
|
2021-04-02 04:21:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 11:05:10 +02:00
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension HashtagTimelineViewController {
|
|
|
|
|
|
|
|
@objc private func composeBarButtonItemPressed(_ sender: UIBarButtonItem) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2022-01-27 14:23:39 +01:00
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
let composeViewModel = ComposeViewModel(
|
|
|
|
context: context,
|
|
|
|
composeKind: .hashtag(hashtag: viewModel.hashtag),
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
2021-04-01 04:12:57 +02:00
|
|
|
coordinator.present(scene: .compose(viewModel: composeViewModel), from: self, transition: .modal(animated: true, completion: nil))
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2021-04-08 11:05:10 +02:00
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
// MARK: - UITableViewDelegate
|
2022-01-27 14:23:39 +01:00
|
|
|
extension HashtagTimelineViewController: UITableViewDelegate, AutoGenerateTableViewDelegate {
|
|
|
|
// sourcery:inline:HashtagTimelineViewController.AutoGenerateTableViewDelegate
|
|
|
|
|
|
|
|
// Generated using Sourcery
|
|
|
|
// DO NOT EDIT
|
2021-04-14 09:59:29 +02:00
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
aspectTableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
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)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-04-30 13:28:06 +02:00
|
|
|
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)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-04-30 13:28:06 +02:00
|
|
|
func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
aspectTableView(tableView, willPerformPreviewActionForMenuWith: configuration, animator: animator)
|
|
|
|
}
|
2022-01-27 14:50:02 +01:00
|
|
|
// sourcery:end
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// MARK: - StatusTableViewCellDelegate
|
|
|
|
extension HashtagTimelineViewController: StatusTableViewCellDelegate { }
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2022-05-11 12:40:21 +02:00
|
|
|
extension HashtagTimelineViewController {
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return navigationKeyCommands + statusNavigationKeyCommands
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - StatusTableViewControllerNavigateable
|
|
|
|
extension HashtagTimelineViewController: StatusTableViewControllerNavigateable {
|
|
|
|
@objc func navigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
navigateKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func statusKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
statusKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
}
|