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
|
|
|
|
|
|
|
|
// TODO: adopt MediaPreviewableViewController
|
|
|
|
final class UserTimelineViewController: UIViewController, NeedsDependency {
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: UserTimelineViewModel!
|
|
|
|
|
|
|
|
// let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
|
|
|
lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
|
|
|
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
|
|
|
|
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
|
2021-04-06 10:43:08 +02:00
|
|
|
tableView.register(TimelineHeaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineHeaderTableViewCell.self))
|
2021-04-01 08:39:15 +02:00
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UserTimelineViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
|
|
|
|
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(
|
|
|
|
for: tableView,
|
|
|
|
dependency: self,
|
|
|
|
statusTableViewCellDelegate: self
|
|
|
|
)
|
|
|
|
|
|
|
|
// trigger user timeline loading
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
viewModel.domain.removeDuplicates().eraseToAnyPublisher(),
|
|
|
|
viewModel.userID.removeDuplicates().eraseToAnyPublisher()
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.viewModel.stateMachine.enter(UserTimelineViewModel.State.Reloading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
|
|
|
context.videoPlaybackService.viewDidDisappear(from: self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIScrollViewDelegate
|
|
|
|
extension UserTimelineViewController {
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
handleScrollViewDidScroll(scrollView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
extension UserTimelineViewController: UITableViewDelegate {
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
2021-04-06 10:43:08 +02:00
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return 100 }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return 100 }
|
|
|
|
|
|
|
|
guard let frame = viewModel.cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else {
|
|
|
|
if case .bottomLoader = item {
|
|
|
|
return TimelineLoaderTableViewCell.cellHeight
|
|
|
|
} else {
|
|
|
|
return 200
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// os_log("%{public}s[%{public}ld], %{public}s: cache cell frame %s", ((#file as NSString).lastPathComponent), #line, #function, frame.debugDescription)
|
|
|
|
|
|
|
|
return ceil(frame.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
|
|
|
|
let key = item.hashValue
|
|
|
|
let frame = cell.frame
|
|
|
|
viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key))
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - AVPlayerViewControllerDelegate
|
|
|
|
extension UserTimelineViewController: AVPlayerViewControllerDelegate {
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
handlePlayerViewController(playerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
handlePlayerViewController(playerViewController, willEndFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - TimelinePostTableViewCellDelegate
|
|
|
|
extension UserTimelineViewController: StatusTableViewCellDelegate {
|
|
|
|
weak var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { return self }
|
|
|
|
func parent() -> UIViewController { return self }
|
|
|
|
}
|
|
|
|
|
|
|
|
//// MARK: - TimelineHeaderTableViewCellDelegate
|
|
|
|
//extension UserTimelineViewController: TimelineHeaderTableViewCellDelegate { }
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - CustomScrollViewContainerController
|
|
|
|
extension UserTimelineViewController: ScrollViewContainer {
|
|
|
|
var scrollView: UIScrollView { return tableView }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - LoadMoreConfigurableTableViewContainer
|
|
|
|
extension UserTimelineViewController: LoadMoreConfigurableTableViewContainer {
|
|
|
|
typealias BottomLoaderTableViewCell = TimelineBottomLoaderTableViewCell
|
|
|
|
typealias LoadingState = UserTimelineViewModel.State.LoadingMore
|
|
|
|
|
|
|
|
var loadMoreConfigurableTableView: UITableView { return tableView }
|
|
|
|
var loadMoreConfigurableStateMachine: GKStateMachine { return viewModel.stateMachine }
|
|
|
|
}
|