2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// NotificationTimelineViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-02-11 12:27:14 +01:00
|
|
|
import CoreDataStack
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
final class NotificationTimelineViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "NotificationTimelineViewController", category: "ViewController")
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var observations = Set<NSKeyValueObservation>()
|
|
|
|
|
|
|
|
var viewModel: NotificationTimelineViewModel!
|
|
|
|
|
|
|
|
private(set) lazy var refreshControl: UIRefreshControl = {
|
|
|
|
let refreshControl = UIRefreshControl()
|
|
|
|
refreshControl.addTarget(self, action: #selector(NotificationTimelineViewController.refreshControlValueChanged(_:)), for: .valueChanged)
|
|
|
|
return refreshControl
|
|
|
|
}()
|
|
|
|
|
|
|
|
private(set) lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationTimelineViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
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(
|
|
|
|
tableView: tableView,
|
|
|
|
notificationTableViewCellDelegate: self
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
self.viewModel.loadOldestStateMachine.enter(NotificationTimelineViewModel.LoadOldestState.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
// setup refresh control
|
|
|
|
tableView.refreshControl = refreshControl
|
|
|
|
viewModel.didLoadLatest
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
UIView.animate(withDuration: 0.5) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
if !viewModel.isLoadingLatest {
|
|
|
|
let now = Date()
|
|
|
|
if let timestamp = viewModel.lastAutomaticFetchTimestamp {
|
|
|
|
if now.timeIntervalSince(timestamp) > 60 {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): auto fetch latest timeline…")
|
|
|
|
Task {
|
|
|
|
await viewModel.loadLatest()
|
|
|
|
}
|
|
|
|
viewModel.lastAutomaticFetchTimestamp = now
|
|
|
|
} else {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): auto fetch latest timeline skip. Reason: updated in recent 60s")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Task {
|
|
|
|
await viewModel.loadLatest()
|
|
|
|
}
|
|
|
|
viewModel.lastAutomaticFetchTimestamp = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationTimelineViewController {
|
|
|
|
|
|
|
|
@objc private func refreshControlValueChanged(_ sender: UIRefreshControl) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
|
|
|
|
Task {
|
|
|
|
await viewModel.loadLatest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
extension NotificationTimelineViewController: UITableViewDelegate, AutoGenerateTableViewDelegate {
|
|
|
|
// sourcery:inline:NotificationTimelineViewController.AutoGenerateTableViewDelegate
|
|
|
|
|
|
|
|
// Generated using Sourcery
|
|
|
|
// DO NOT EDIT
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
aspectTableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
return aspectTableView(tableView, contextMenuConfigurationForRowAt: indexPath, point: point)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
aspectTableView(tableView, willPerformPreviewActionForMenuWith: configuration, animator: animator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sourcery:end
|
2022-02-08 13:07:50 +01:00
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check item type inside `loadMore`
|
|
|
|
Task {
|
|
|
|
await viewModel.loadMore(item: item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NotificationTableViewCellDelegate
|
2022-02-11 13:21:28 +01:00
|
|
|
extension NotificationTimelineViewController: NotificationTableViewCellDelegate { }
|
2022-02-14 07:55:00 +01:00
|
|
|
|
|
|
|
// MARK: - ScrollViewContainer
|
|
|
|
extension NotificationTimelineViewController: ScrollViewContainer {
|
|
|
|
|
|
|
|
var scrollView: UIScrollView? { tableView }
|
|
|
|
|
|
|
|
}
|