2021-07-14 14:28:41 +02:00
|
|
|
//
|
|
|
|
// SearchResultViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-14.
|
|
|
|
//
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
import os.log
|
2021-07-14 14:28:41 +02:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
final class SearchResultViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "SearchResultViewController", category: "ViewController")
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
let mediaPreviewTransitionController = MediaPreviewTransitionController()
|
|
|
|
|
|
|
|
var viewModel: SearchResultViewModel!
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
let tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
2021-07-15 06:01:01 +02:00
|
|
|
tableView.separatorStyle = .none
|
2021-07-15 09:49:30 +02:00
|
|
|
tableView.tableFooterView = UIView()
|
|
|
|
tableView.backgroundColor = .clear
|
2021-07-14 14:28:41 +02:00
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
2022-01-27 14:23:39 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2021-07-14 14:28:41 +02:00
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
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),
|
|
|
|
])
|
|
|
|
|
2021-07-15 06:01:01 +02:00
|
|
|
tableView.delegate = self
|
2022-01-27 14:23:39 +01:00
|
|
|
// tableView.prefetchDataSource = self
|
2021-07-14 14:28:41 +02:00
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
tableView: tableView,
|
|
|
|
statusTableViewCellDelegate: self
|
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// 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(SearchResultViewModel.State.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
// listen keyboard events and set content inset
|
|
|
|
let keyboardEventPublishers = Publishers.CombineLatest3(
|
|
|
|
KeyboardResponderService.shared.isShow,
|
|
|
|
KeyboardResponderService.shared.state,
|
|
|
|
KeyboardResponderService.shared.endFrame
|
|
|
|
)
|
2021-07-14 14:40:27 +02:00
|
|
|
Publishers.CombineLatest3(
|
2021-07-14 14:28:41 +02:00
|
|
|
keyboardEventPublishers,
|
2021-07-14 14:40:27 +02:00
|
|
|
viewModel.viewDidAppear,
|
|
|
|
viewModel.didDataSourceUpdate
|
2021-07-14 14:28:41 +02:00
|
|
|
)
|
2021-07-14 14:40:27 +02:00
|
|
|
.sink(receiveValue: { [weak self] keyboardEvents, _, _ in
|
2021-07-14 14:28:41 +02:00
|
|
|
guard let self = self else { return }
|
|
|
|
let (isShow, state, endFrame) = keyboardEvents
|
|
|
|
|
|
|
|
// update keyboard background color
|
|
|
|
guard isShow, state == .dock else {
|
|
|
|
self.tableView.contentInset.bottom = 0
|
|
|
|
self.tableView.verticalScrollIndicatorInsets.bottom = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// isShow AND dock state
|
|
|
|
|
|
|
|
// adjust inset for tableView
|
|
|
|
let contentFrame = self.view.convert(self.tableView.frame, to: nil)
|
|
|
|
let padding = contentFrame.maxY - endFrame.minY
|
|
|
|
guard padding > 0 else {
|
|
|
|
self.tableView.contentInset.bottom = self.view.safeAreaInsets.bottom
|
|
|
|
self.tableView.verticalScrollIndicatorInsets.bottom = self.view.safeAreaInsets.bottom
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tableView.contentInset.bottom = padding - self.view.safeAreaInsets.bottom
|
|
|
|
self.tableView.verticalScrollIndicatorInsets.bottom = padding - self.view.safeAreaInsets.bottom
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
2022-01-27 14:23:39 +01:00
|
|
|
//
|
2021-07-15 09:49:30 +02:00
|
|
|
// works for already onscreen page
|
|
|
|
viewModel.navigationBarFrame
|
|
|
|
.removeDuplicates()
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] frame in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard self.viewModel.viewDidAppear.value else { return }
|
|
|
|
self.tableView.contentInset.top = frame.height
|
2022-01-27 14:23:39 +01:00
|
|
|
self.tableView.verticalScrollIndicatorInsets.top = frame.height
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
// works for appearing page
|
|
|
|
if !viewModel.viewDidAppear.value {
|
|
|
|
tableView.contentInset.top = viewModel.navigationBarFrame.value.height
|
|
|
|
tableView.contentOffset.y = -viewModel.navigationBarFrame.value.height
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
viewModel.viewDidAppear.value = true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultViewController {
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
|
|
|
view.backgroundColor = theme.systemGroupedBackgroundColor
|
2021-07-15 09:49:30 +02:00
|
|
|
// tableView.backgroundColor = theme.systemBackgroundColor
|
2021-07-14 14:28:41 +02:00
|
|
|
// searchHeader.backgroundColor = theme.systemGroupedBackgroundColor
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - StatusTableViewCellDelegate
|
2022-01-27 14:23:39 +01:00
|
|
|
//extension SearchResultViewController: StatusTableViewCellDelegate {
|
|
|
|
// weak var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { return self }
|
|
|
|
// func parent() -> UIViewController { return self }
|
|
|
|
//}
|
2021-07-15 09:49:30 +02:00
|
|
|
|
2021-07-15 06:01:01 +02:00
|
|
|
// MARK: - UITableViewDelegate
|
2022-01-27 14:23:39 +01:00
|
|
|
extension SearchResultViewController: UITableViewDelegate, AutoGenerateTableViewDelegate {
|
|
|
|
// sourcery:inline:SearchResultViewController.AutoGenerateTableViewDelegate
|
2021-07-15 09:49:30 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// Generated using Sourcery
|
|
|
|
// DO NOT EDIT
|
2021-07-15 09:49:30 +02:00
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2022-01-27 14:23:39 +01:00
|
|
|
aspectTableView(tableView, didSelectRowAt: indexPath)
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
2022-01-27 14:23:39 +01:00
|
|
|
return aspectTableView(tableView, contextMenuConfigurationForRowAt: indexPath, point: point)
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
2022-01-27 14:23:39 +01:00
|
|
|
return aspectTableView(tableView, previewForHighlightingContextMenuWithConfiguration: configuration)
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
2022-01-27 14:23:39 +01:00
|
|
|
return aspectTableView(tableView, previewForDismissingContextMenuWithConfiguration: configuration)
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
aspectTableView(tableView, willPerformPreviewActionForMenuWith: configuration, animator: animator)
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// sourcery:end
|
|
|
|
|
|
|
|
// func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
// aspectTableView(tableView, estimatedHeightForRowAt: indexPath)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
// aspectTableView(tableView, willDisplay: cell, forRowAt: indexPath)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
// aspectTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
// guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
// guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
//
|
|
|
|
// viewModel.persistSearchHistory(for: item)
|
|
|
|
//
|
|
|
|
// switch item {
|
|
|
|
// case .account(let account):
|
|
|
|
// let profileViewModel = RemoteProfileViewModel(context: context, userID: account.id)
|
|
|
|
// coordinator.present(scene: .profile(viewModel: profileViewModel), from: self, transition: .show)
|
|
|
|
// case .hashtag(let hashtag):
|
|
|
|
// let hashtagViewModel = HashtagTimelineViewModel(context: context, hashtag: hashtag.name)
|
|
|
|
// coordinator.present(scene: .hashtagTimeline(viewModel: hashtagViewModel), from: self, transition: .show)
|
|
|
|
// case .status:
|
|
|
|
// aspectTableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
// case .bottomLoader:
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
// aspectTableView(tableView, contextMenuConfigurationForRowAt: indexPath, point: point)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
// aspectTableView(tableView, previewForHighlightingContextMenuWithConfiguration: configuration)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
|
|
|
|
// aspectTableView(tableView, previewForDismissingContextMenuWithConfiguration: configuration)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
// aspectTableView(tableView, willPerformPreviewActionForMenuWith: configuration, animator: animator)
|
|
|
|
// }
|
|
|
|
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDataSourcePrefetching
|
2022-01-27 14:23:39 +01:00
|
|
|
//extension SearchResultViewController: UITableViewDataSourcePrefetching {
|
|
|
|
// func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
// aspectTableView(tableView, cancelPrefetchingForRowsAt: indexPaths)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
|
|
|
// aspectTableView(tableView, cancelPrefetchingForRowsAt: indexPaths)
|
|
|
|
// }
|
|
|
|
//}
|
2021-07-15 09:49:30 +02:00
|
|
|
|
|
|
|
// MARK: - AVPlayerViewControllerDelegate
|
2022-01-27 14:23:39 +01:00
|
|
|
//extension SearchResultViewController: AVPlayerViewControllerDelegate {
|
|
|
|
// func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
// handlePlayerViewController(playerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
// handlePlayerViewController(playerViewController, willEndFullScreenPresentationWithAnimationCoordinator: coordinator)
|
|
|
|
// }
|
|
|
|
//}
|
2021-07-15 09:49:30 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// MARK: - StatusTableViewCellDelegate
|
|
|
|
extension SearchResultViewController: StatusTableViewCellDelegate { }
|