2021-07-15 14:28:36 +02:00
|
|
|
//
|
|
|
|
// SearchHistoryViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-13.
|
|
|
|
//
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
import os.log
|
2021-07-15 14:28:36 +02:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-07-15 14:28:36 +02:00
|
|
|
|
|
|
|
final class SearchHistoryViewController: UIViewController, NeedsDependency {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "SearchHistoryViewController", category: "ViewController")
|
2021-07-15 14:28:36 +02:00
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-07-15 14:28:36 +02:00
|
|
|
var viewModel: SearchHistoryViewModel!
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let collectionView: UICollectionView = {
|
|
|
|
var configuration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
|
|
|
configuration.backgroundColor = .clear
|
|
|
|
configuration.headerMode = .supplementary
|
|
|
|
let layout = UICollectionViewCompositionalLayout.list(using: configuration)
|
|
|
|
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
|
|
|
return collectionView
|
2021-07-15 14:28:36 +02:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchHistoryViewController {
|
|
|
|
|
|
|
|
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-15 14:28:36 +02:00
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(collectionView)
|
2022-11-17 17:45:27 +01:00
|
|
|
collectionView.pinToParent()
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
collectionView.delegate = self
|
2021-07-15 14:28:36 +02:00
|
|
|
viewModel.setupDiffableDataSource(
|
2022-01-27 14:23:39 +01:00
|
|
|
collectionView: collectionView,
|
|
|
|
searchHistorySectionHeaderCollectionReusableViewDelegate: self
|
2021-07-15 14:28:36 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchHistoryViewController {
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
|
|
|
view.backgroundColor = theme.systemGroupedBackgroundColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// MARK: - UICollectionViewDelegate
|
|
|
|
extension SearchHistoryViewController: UICollectionViewDelegate {
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): select item at: \(indexPath.debugDescription)")
|
|
|
|
|
|
|
|
defer {
|
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
Task {
|
|
|
|
let source = DataSourceItem.Source(indexPath: indexPath)
|
|
|
|
guard let item = await item(from: source) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await DataSourceFacade.responseToCreateSearchHistory(
|
|
|
|
provider: self,
|
|
|
|
item: item
|
|
|
|
)
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .user(let record):
|
|
|
|
await DataSourceFacade.coordinateToProfileScene(
|
|
|
|
provider: self,
|
|
|
|
user: record
|
|
|
|
)
|
|
|
|
case .hashtag(let record):
|
|
|
|
await DataSourceFacade.coordinateToHashtagScene(
|
|
|
|
provider: self,
|
|
|
|
tag: record
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
break
|
|
|
|
}
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
// MARK: - AuthContextProvider
|
|
|
|
extension SearchHistoryViewController: AuthContextProvider {
|
|
|
|
var authContext: AuthContext { viewModel.authContext }
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// MARK: - SearchHistorySectionHeaderCollectionReusableViewDelegate
|
|
|
|
extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusableViewDelegate {
|
|
|
|
func searchHistorySectionHeaderCollectionReusableView(
|
|
|
|
_ searchHistorySectionHeaderCollectionReusableView: SearchHistorySectionHeaderCollectionReusableView,
|
|
|
|
clearButtonDidPressed button: UIButton
|
|
|
|
) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
|
|
|
|
Task {
|
|
|
|
try await DataSourceFacade.responseToDeleteSearchHistory(
|
|
|
|
provider: self
|
|
|
|
)
|
|
|
|
}
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
|
|
|
}
|