mastodon-ios/Mastodon/Scene/PublicTimeline/PublicTimelineViewControlle...

133 lines
4.9 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// PublicTimelineViewController.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
import AVKit
import Combine
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
final class PublicTimelineViewController: UIViewController, NeedsDependency, TimelinePostTableViewCellDelegate {
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()
tableView.register(TimelinePostTableViewCell.self, forCellReuseIdentifier: String(describing: TimelinePostTableViewCell.self))
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
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-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
tableView.backgroundColor = Asset.Colors.tootDark.color
view.addSubview(tableView)
view.backgroundColor = Asset.Colors.tootDark.color
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
tableView.delegate = self
viewModel.setupDiffableDataSource(
for: tableView,
dependency: self,
timelinePostTableViewCellDelegate: self
)
}
2021-02-02 12:04:24 +01:00
2021-01-28 09:10:30 +01:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel.fetchLatest()
.receive(on: DispatchQueue.main)
.sink { completion in
switch completion {
case .failure(let error):
2021-02-02 12:04:24 +01:00
os_log("%{public}s[%{public}ld], %{public}s: fetch user timeline latest response error: %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription)
2021-01-28 09:10:30 +01:00
case .finished:
break
}
} receiveValue: { response in
let tootsIDs = response.value.map { $0.id }
self.viewModel.tootIDs.value = tootsIDs
2021-01-28 09:10:30 +01:00
}
.store(in: &viewModel.disposeBag)
}
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
2021-02-02 12:04:24 +01:00
2021-01-28 09:10:30 +01:00
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-01-28 09:10:30 +01:00
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))
}
}