// // SearchResultViewController.swift // Mastodon // // Created by MainasuK Cirno on 2021-7-14. // import UIKit import Combine import AVKit final class SearchResultViewController: UIViewController, NeedsDependency, MediaPreviewableViewController { weak var context: AppContext! { willSet { precondition(!isViewLoaded) } } weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } } let mediaPreviewTransitionController = MediaPreviewTransitionController() var viewModel: SearchResultViewModel! var disposeBag = Set() let tableView: UITableView = { let tableView = UITableView() tableView.register(SearchResultTableViewCell.self, forCellReuseIdentifier: String(describing: SearchResultTableViewCell.self)) tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: String(describing: StatusTableViewCell.self)) tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self)) return tableView }() } extension SearchResultViewController { override func viewDidLoad() { super.viewDidLoad() setupBackgroundColor(theme: ThemeService.shared.currentTheme.value) ThemeService.shared.currentTheme .receive(on: RunLoop.main) .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), ]) viewModel.setupDiffableDataSource( tableView: tableView, dependency: self, statusTableViewCellDelegate: self ) // listen keyboard events and set content inset let keyboardEventPublishers = Publishers.CombineLatest3( KeyboardResponderService.shared.isShow, KeyboardResponderService.shared.state, KeyboardResponderService.shared.endFrame ) Publishers.CombineLatest( keyboardEventPublishers, viewModel.viewDidAppear ) .sink(receiveValue: { [weak self] keyboardEvents, _ in 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) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) viewModel.viewDidAppear.value = true } } extension SearchResultViewController { private func setupBackgroundColor(theme: Theme) { view.backgroundColor = theme.systemGroupedBackgroundColor tableView.backgroundColor = theme.systemBackgroundColor // searchHeader.backgroundColor = theme.systemGroupedBackgroundColor } } // MARK: - AVPlayerViewControllerDelegate 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) } } // MARK: - StatusTableViewCellDelegate extension SearchResultViewController: StatusTableViewCellDelegate { weak var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { return self } func parent() -> UIViewController { return self } } //extension SearchResultViewController: LoadMoreConfigurableTableViewContainer { // typealias BottomLoaderTableViewCell = TimelineBottomLoaderTableViewCell // typealias LoadingState = SearchViewModel.LoadOldestState.Loading // var loadMoreConfigurableTableView: UITableView { searchingTableView } // var loadMoreConfigurableStateMachine: GKStateMachine { viewModel.loadoldestStateMachine } //}