2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// UserTimelineViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import AVKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
2022-05-13 11:23:35 +02:00
|
|
|
import TabBarPager
|
|
|
|
import XLPagerTabStrip
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
final class UserTimelineViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "UserTimelineViewController", category: "ViewController")
|
2021-05-21 09:23:02 +02:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: UserTimelineViewModel!
|
|
|
|
|
2021-04-28 09:02:34 +02:00
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
2022-02-11 12:27:14 +01:00
|
|
|
tableView.estimatedRowHeight = 100
|
2021-04-01 08:39:15 +02:00
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
return tableView
|
|
|
|
}()
|
2022-02-14 09:09:39 +01:00
|
|
|
|
|
|
|
let cellFrameCache = NSCache<NSNumber, NSValue>()
|
2021-05-21 09:23:02 +02:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
deinit {
|
2022-04-12 11:32:38 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UserTimelineViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-07-05 10:07:17 +02:00
|
|
|
view.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
|
|
|
|
ThemeService.shared.currentTheme
|
2022-01-27 14:23:39 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2021-07-05 10:07:17 +02:00
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.view.backgroundColor = theme.secondarySystemBackgroundColor
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
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,
|
2021-04-01 08:39:15 +02:00
|
|
|
statusTableViewCellDelegate: self
|
|
|
|
)
|
|
|
|
|
2021-11-10 10:55:12 +01:00
|
|
|
// setup batch fetch
|
|
|
|
viewModel.listBatchFetchViewModel.setup(scrollView: tableView)
|
|
|
|
viewModel.listBatchFetchViewModel.shouldFetch
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
guard self.view.window != nil else { return }
|
2021-11-10 10:55:12 +01:00
|
|
|
self.viewModel.stateMachine.enter(UserTimelineViewModel.State.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-14 09:09:39 +01:00
|
|
|
// MARK: - CellFrameCacheContainer
|
|
|
|
extension UserTimelineViewController: CellFrameCacheContainer {
|
|
|
|
func keyForCache(tableView: UITableView, indexPath: IndexPath) -> NSNumber? {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return nil }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
|
|
|
|
let key = NSNumber(value: item.hashValue)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
// MARK: - UITableViewDelegate
|
2022-01-27 14:23:39 +01:00
|
|
|
extension UserTimelineViewController: UITableViewDelegate, AutoGenerateTableViewDelegate {
|
|
|
|
// sourcery:inline:UserTimelineViewController.AutoGenerateTableViewDelegate
|
|
|
|
|
|
|
|
// Generated using Sourcery
|
|
|
|
// DO NOT EDIT
|
2021-04-13 13:46:42 +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:23:39 +01:00
|
|
|
|
|
|
|
// sourcery:end
|
2021-04-30 13:28:06 +02:00
|
|
|
|
2022-02-14 09:09:39 +01:00
|
|
|
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
guard let frame = retrieveCellFrame(tableView: tableView, indexPath: indexPath) else {
|
|
|
|
return 200
|
|
|
|
}
|
|
|
|
return ceil(frame.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
cacheCellFrame(tableView: tableView, didEndDisplaying: cell, forRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - CustomScrollViewContainerController
|
|
|
|
extension UserTimelineViewController: ScrollViewContainer {
|
2022-05-13 11:23:35 +02:00
|
|
|
var scrollView: UIScrollView { return tableView }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - TabBarPage
|
|
|
|
extension UserTimelineViewController: TabBarPage {
|
|
|
|
var pageScrollView: UIScrollView {
|
|
|
|
scrollView
|
|
|
|
}
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// MARK: - StatusTableViewCellDelegate
|
|
|
|
extension UserTimelineViewController: StatusTableViewCellDelegate { }
|
2022-02-16 12:47:51 +01:00
|
|
|
|
|
|
|
extension UserTimelineViewController {
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return navigationKeyCommands + statusNavigationKeyCommands
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - StatusTableViewControllerNavigateable
|
|
|
|
extension UserTimelineViewController: StatusTableViewControllerNavigateable {
|
|
|
|
@objc func navigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
navigateKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func statusKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
statusKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 11:23:35 +02:00
|
|
|
|
|
|
|
// MARK: - IndicatorInfoProvider
|
|
|
|
extension UserTimelineViewController: IndicatorInfoProvider {
|
|
|
|
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
|
2022-05-26 17:19:47 +02:00
|
|
|
return IndicatorInfo(title: viewModel.title)
|
2022-05-13 11:23:35 +02:00
|
|
|
}
|
|
|
|
}
|