2021-01-28 09:10:30 +01:00
|
|
|
//
|
|
|
|
// PublicTimelineViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/1/27.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AVKit
|
|
|
|
import Combine
|
2021-02-04 08:09:58 +01:00
|
|
|
import CoreData
|
2021-01-28 09:10:30 +01:00
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
2021-02-02 12:04:24 +01:00
|
|
|
import os.log
|
|
|
|
import UIKit
|
2021-01-28 09:10:30 +01:00
|
|
|
|
2021-02-25 06:47:30 +01:00
|
|
|
final class PublicTimelineViewController: UIViewController, NeedsDependency {
|
2021-01-28 09:10:30 +01:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: PublicTimelineViewModel!
|
|
|
|
|
2021-02-02 12:04:24 +01:00
|
|
|
let refreshControl = UIRefreshControl()
|
|
|
|
|
2021-01-28 09:10:30 +01:00
|
|
|
lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
2021-02-23 08:16:55 +01:00
|
|
|
tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self))
|
2021-02-04 08:09:58 +01:00
|
|
|
tableView.register(TimelineMiddleLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self))
|
2021-02-03 06:01:50 +01:00
|
|
|
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
|
2021-01-28 09:10:30 +01:00
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
2021-02-05 10:53:00 +01:00
|
|
|
tableView.backgroundColor = .clear
|
2021-01-28 09:10:30 +01:00
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
deinit {
|
2021-02-02 12:04:24 +01:00
|
|
|
os_log("%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
|
2021-01-28 09:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PublicTimelineViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-02-25 04:48:16 +01:00
|
|
|
title = "Public"
|
2021-02-23 08:16:55 +01:00
|
|
|
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
2021-02-05 04:53:21 +01:00
|
|
|
|
2021-02-02 12:04:24 +01:00
|
|
|
tableView.refreshControl = refreshControl
|
|
|
|
refreshControl.addTarget(self, action: #selector(PublicTimelineViewController.refreshControlValueChanged(_:)), for: .valueChanged)
|
|
|
|
// bind refresh control
|
|
|
|
viewModel.isFetchingLatestTimeline
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] isFetching in
|
|
|
|
guard let self = self else { return }
|
|
|
|
if !isFetching {
|
|
|
|
UIView.animate(withDuration: 0.5) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-01-28 09:10:30 +01:00
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
2021-02-02 07:10:25 +01:00
|
|
|
view.addSubview(tableView)
|
2021-01-28 09:10:30 +01:00
|
|
|
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),
|
|
|
|
])
|
|
|
|
|
|
|
|
viewModel.tableView = tableView
|
2021-02-05 09:50:40 +01:00
|
|
|
viewModel.contentOffsetAdjustableTimelineViewControllerDelegate = self
|
2021-01-28 09:10:30 +01:00
|
|
|
tableView.delegate = self
|
2021-03-10 12:12:53 +01:00
|
|
|
tableView.prefetchDataSource = self
|
2021-01-28 09:10:30 +01:00
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
for: tableView,
|
|
|
|
dependency: self,
|
2021-03-03 09:12:48 +01:00
|
|
|
statusTableViewCellDelegate: self,
|
2021-02-04 08:09:58 +01:00
|
|
|
timelineMiddleLoaderTableViewCellDelegate: self
|
2021-01-28 09:10:30 +01:00
|
|
|
)
|
|
|
|
}
|
2021-02-02 12:04:24 +01:00
|
|
|
|
2021-03-10 14:19:56 +01:00
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
context.videoPlaybackService.viewDidDisappear(from: self)
|
2021-03-11 06:11:13 +01:00
|
|
|
context.audioPlaybackService.viewDidDisappear(from: self)
|
2021-03-10 14:19:56 +01:00
|
|
|
}
|
2021-02-03 06:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIScrollViewDelegate
|
|
|
|
extension PublicTimelineViewController {
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
handleScrollViewDidScroll(scrollView)
|
2021-01-28 09:10:30 +01:00
|
|
|
}
|
2021-02-02 12:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Selector
|
|
|
|
extension PublicTimelineViewController {
|
|
|
|
@objc private func refreshControlValueChanged(_ sender: UIRefreshControl) {
|
|
|
|
guard viewModel.stateMachine.enter(PublicTimelineViewModel.State.Loading.self) else {
|
|
|
|
sender.endRefreshing()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 09:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
extension PublicTimelineViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
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 {
|
|
|
|
return 200
|
|
|
|
}
|
|
|
|
|
|
|
|
return ceil(frame.height)
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:04:24 +01:00
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
|
2021-03-10 14:19:56 +01:00
|
|
|
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
handleTableView(tableView, willDisplay: cell, forRowAt: indexPath)
|
|
|
|
}
|
2021-01-28 09:10:30 +01:00
|
|
|
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
2021-03-10 14:19:56 +01:00
|
|
|
handleTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
|
2021-01-28 09:10:30 +01:00
|
|
|
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-02-05 09:50:40 +01:00
|
|
|
|
2021-03-10 12:12:53 +01:00
|
|
|
// MARK: - UITableViewDataSourcePrefetching
|
|
|
|
extension PublicTimelineViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
handleTableView(tableView, prefetchRowsAt: indexPaths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:50:40 +01:00
|
|
|
// MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate
|
|
|
|
extension PublicTimelineViewController: ContentOffsetAdjustableTimelineViewControllerDelegate {
|
|
|
|
func navigationBar() -> UINavigationBar? {
|
|
|
|
return navigationController?.navigationBar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 06:01:50 +01:00
|
|
|
// MARK: - LoadMoreConfigurableTableViewContainer
|
|
|
|
extension PublicTimelineViewController: LoadMoreConfigurableTableViewContainer {
|
|
|
|
typealias BottomLoaderTableViewCell = TimelineBottomLoaderTableViewCell
|
|
|
|
typealias LoadingState = PublicTimelineViewModel.State.LoadingMore
|
|
|
|
|
|
|
|
var loadMoreConfigurableTableView: UITableView { return tableView }
|
|
|
|
var loadMoreConfigurableStateMachine: GKStateMachine { return viewModel.stateMachine }
|
|
|
|
}
|
2021-02-04 08:09:58 +01:00
|
|
|
|
|
|
|
// MARK: - TimelineMiddleLoaderTableViewCellDelegate
|
|
|
|
extension PublicTimelineViewController: TimelineMiddleLoaderTableViewCellDelegate {
|
2021-04-01 08:39:15 +02:00
|
|
|
func configure(cell: TimelineMiddleLoaderTableViewCell, upperTimelineStatusID: String?, timelineIndexobjectID: NSManagedObjectID?) {
|
|
|
|
guard let upperTimelineStatusID = upperTimelineStatusID else {return}
|
2021-02-04 08:09:58 +01:00
|
|
|
viewModel.loadMiddleSateMachineList
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] ids in
|
|
|
|
guard let _ = self else { return }
|
2021-04-01 08:39:15 +02:00
|
|
|
if let stateMachine = ids[upperTimelineStatusID] {
|
2021-02-04 08:09:58 +01:00
|
|
|
guard let state = stateMachine.currentState else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make success state same as loading due to snapshot updating delay
|
|
|
|
let isLoading = state is PublicTimelineViewModel.LoadMiddleState.Loading || state is PublicTimelineViewModel.LoadMiddleState.Success
|
|
|
|
if isLoading {
|
2021-03-16 12:28:52 +01:00
|
|
|
cell.startAnimating()
|
2021-02-04 08:09:58 +01:00
|
|
|
} else {
|
2021-03-16 12:28:52 +01:00
|
|
|
cell.stopAnimating()
|
2021-02-04 08:09:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-16 12:28:52 +01:00
|
|
|
cell.stopAnimating()
|
2021-02-04 08:09:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
|
|
|
|
var dict = viewModel.loadMiddleSateMachineList.value
|
2021-04-01 08:39:15 +02:00
|
|
|
if let _ = dict[upperTimelineStatusID] {
|
2021-02-04 08:09:58 +01:00
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
2021-04-01 08:39:15 +02:00
|
|
|
PublicTimelineViewModel.LoadMiddleState.Initial(viewModel: viewModel, upperTimelineStatusID: upperTimelineStatusID),
|
|
|
|
PublicTimelineViewModel.LoadMiddleState.Loading(viewModel: viewModel, upperTimelineStatusID: upperTimelineStatusID),
|
|
|
|
PublicTimelineViewModel.LoadMiddleState.Fail(viewModel: viewModel, upperTimelineStatusID: upperTimelineStatusID),
|
|
|
|
PublicTimelineViewModel.LoadMiddleState.Success(viewModel: viewModel, upperTimelineStatusID: upperTimelineStatusID),
|
2021-02-04 08:09:58 +01:00
|
|
|
])
|
|
|
|
stateMachine.enter(PublicTimelineViewModel.LoadMiddleState.Initial.self)
|
2021-04-01 08:39:15 +02:00
|
|
|
dict[upperTimelineStatusID] = stateMachine
|
2021-02-04 08:09:58 +01:00
|
|
|
viewModel.loadMiddleSateMachineList.value = dict
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func timelineMiddleLoaderTableViewCell(_ cell: TimelineMiddleLoaderTableViewCell, 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 }
|
|
|
|
|
|
|
|
switch item {
|
2021-02-07 07:42:50 +01:00
|
|
|
case .publicMiddleLoader(let upper):
|
2021-02-04 08:09:58 +01:00
|
|
|
guard let stateMachine = viewModel.loadMiddleSateMachineList.value[upper] else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stateMachine.enter(PublicTimelineViewModel.LoadMiddleState.Loading.self)
|
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-25 06:47:30 +01:00
|
|
|
|
2021-03-10 07:36:28 +01:00
|
|
|
// MARK: - AVPlayerViewControllerDelegate
|
|
|
|
extension PublicTimelineViewController: AVPlayerViewControllerDelegate {
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
handlePlayerViewController(playerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
handlePlayerViewController(playerViewController, willEndFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-25 06:47:30 +01:00
|
|
|
// MARK: - StatusTableViewCellDelegate
|
2021-03-10 07:36:28 +01:00
|
|
|
extension PublicTimelineViewController: StatusTableViewCellDelegate {
|
|
|
|
weak var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { return self }
|
|
|
|
func parent() -> UIViewController { return self }
|
|
|
|
}
|