2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// DiscoveryNewsViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonUI
|
|
|
|
|
|
|
|
final class DiscoveryNewsViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "TrendPostsViewController", category: "ViewController")
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: DiscoveryNewsViewModel!
|
|
|
|
|
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
|
|
|
lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.estimatedRowHeight = 100
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
2022-11-14 23:09:59 +01:00
|
|
|
let refreshControl = RefreshControl()
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryNewsViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
view.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
|
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.view.backgroundColor = theme.secondarySystemBackgroundColor
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(tableView)
|
2022-11-17 17:45:27 +01:00
|
|
|
tableView.pinToParent()
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
tableView.delegate = self
|
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
tableView: tableView
|
|
|
|
)
|
|
|
|
|
|
|
|
tableView.refreshControl = refreshControl
|
|
|
|
refreshControl.addTarget(self, action: #selector(DiscoveryNewsViewController.refreshControlValueChanged(_:)), for: .valueChanged)
|
|
|
|
viewModel.didLoadLatest
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
guard self.view.window != nil else { return }
|
|
|
|
self.viewModel.stateMachine.enter(DiscoveryNewsViewModel.State.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryNewsViewController {
|
|
|
|
|
2022-11-14 23:09:59 +01:00
|
|
|
@objc private func refreshControlValueChanged(_ sender: RefreshControl) {
|
2022-04-13 14:43:16 +02:00
|
|
|
guard viewModel.stateMachine.enter(DiscoveryNewsViewModel.State.Reloading.self) else {
|
|
|
|
sender.endRefreshing()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
extension DiscoveryNewsViewController: UITableViewDelegate {
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(indexPath)")
|
|
|
|
guard case let .link(link) = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else { return }
|
|
|
|
guard let url = URL(string: link.url) else { return }
|
2022-11-17 23:02:43 +01:00
|
|
|
_ = coordinator.present(
|
2022-04-13 14:43:16 +02:00
|
|
|
scene: .safari(url: url),
|
|
|
|
from: self,
|
|
|
|
transition: .safariPresent(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: ScrollViewContainer
|
|
|
|
extension DiscoveryNewsViewController: ScrollViewContainer {
|
2022-05-13 11:23:35 +02:00
|
|
|
var scrollView: UIScrollView { tableView }
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|
2022-05-11 12:44:17 +02:00
|
|
|
|
|
|
|
extension DiscoveryNewsViewController {
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return navigationKeyCommands
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryNewsViewController: TableViewControllerNavigateable {
|
|
|
|
|
|
|
|
func navigate(direction: TableViewNavigationDirection) {
|
|
|
|
if let indexPathForSelectedRow = tableView.indexPathForSelectedRow {
|
|
|
|
// navigate up/down on the current selected item
|
|
|
|
navigateToLink(direction: direction, indexPath: indexPathForSelectedRow)
|
|
|
|
} else {
|
|
|
|
// set first visible item selected
|
|
|
|
navigateToFirstVisibleLink()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func navigateToLink(direction: TableViewNavigationDirection, indexPath: IndexPath) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let items = diffableDataSource.snapshot().itemIdentifiers
|
|
|
|
guard let selectedItem = diffableDataSource.itemIdentifier(for: indexPath),
|
|
|
|
let selectedItemIndex = items.firstIndex(of: selectedItem) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let _navigateToItem: DiscoveryItem? = {
|
|
|
|
var index = selectedItemIndex
|
|
|
|
while 0..<items.count ~= index {
|
|
|
|
index = {
|
|
|
|
switch direction {
|
|
|
|
case .up: return index - 1
|
|
|
|
case .down: return index + 1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
guard 0..<items.count ~= index else { return nil }
|
|
|
|
let item = items[index]
|
|
|
|
|
|
|
|
guard Self.validNavigateableItem(item) else { continue }
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
guard let item = _navigateToItem, let indexPath = diffableDataSource.indexPath(for: item) else { return }
|
|
|
|
let scrollPosition: UITableView.ScrollPosition = overrideNavigationScrollPosition ?? Self.navigateScrollPosition(tableView: tableView, indexPath: indexPath)
|
|
|
|
tableView.selectRow(at: indexPath, animated: true, scrollPosition: scrollPosition)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func navigateToFirstVisibleLink() {
|
|
|
|
guard let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows else { return }
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
|
|
|
|
var visibleItems: [DiscoveryItem] = indexPathsForVisibleRows.sorted().compactMap { indexPath in
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
|
|
|
|
guard Self.validNavigateableItem(item) else { return nil }
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
if indexPathsForVisibleRows.first?.row != 0, visibleItems.count > 1 {
|
|
|
|
// drop first when visible not the first cell of table
|
|
|
|
visibleItems.removeFirst()
|
|
|
|
}
|
|
|
|
guard let item = visibleItems.first, let indexPath = diffableDataSource.indexPath(for: item) else { return }
|
|
|
|
let scrollPosition: UITableView.ScrollPosition = overrideNavigationScrollPosition ?? Self.navigateScrollPosition(tableView: tableView, indexPath: indexPath)
|
|
|
|
tableView.selectRow(at: indexPath, animated: true, scrollPosition: scrollPosition)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func validNavigateableItem(_ item: DiscoveryItem) -> Bool {
|
|
|
|
switch item {
|
|
|
|
case .link:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func open() {
|
|
|
|
guard let indexPathForSelectedRow = tableView.indexPathForSelectedRow else { return }
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPathForSelectedRow) else { return }
|
|
|
|
|
|
|
|
guard case let .link(link) = item else { return }
|
|
|
|
guard let url = URL(string: link.url) else { return }
|
2022-11-17 23:02:43 +01:00
|
|
|
_ = coordinator.present(
|
2022-05-11 12:44:17 +02:00
|
|
|
scene: .safari(url: url),
|
|
|
|
from: self,
|
|
|
|
transition: .safariPresent(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func navigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
navigateKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|