chore: add navigation to hashtagViewController

This commit is contained in:
sunxiaojian 2021-04-08 12:31:48 +08:00
parent 0dab9acd91
commit 2331867758
3 changed files with 11 additions and 4 deletions

View File

@ -86,6 +86,6 @@ extension SearchViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.searchResultDiffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
viewModel.saveItemToCoreData(item: item)
viewModel.searchResultItemDidSelected(item: item, from: self)
}
}

View File

@ -15,7 +15,7 @@ final class SearchViewController: UIViewController, NeedsDependency {
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
var disposeBag = Set<AnyCancellable>()
private(set) lazy var viewModel = SearchViewModel(context: context)
private(set) lazy var viewModel = SearchViewModel(context: context, coordinator: coordinator)
let searchBar: UISearchBar = {
let searchBar = UISearchBar()

View File

@ -19,6 +19,7 @@ final class SearchViewModel: NSObject {
// input
let context: AppContext
weak var coordinator: SceneCoordinator!
// output
let searchText = CurrentValueSubject<String, Never>("")
@ -51,7 +52,8 @@ final class SearchViewModel: NSObject {
lazy var loadOldestStateMachinePublisher = CurrentValueSubject<LoadOldestState?, Never>(nil)
init(context: AppContext) {
init(context: AppContext,coordinator: SceneCoordinator) {
self.coordinator = coordinator
self.context = context
super.init()
@ -233,7 +235,7 @@ final class SearchViewModel: NSObject {
}
}
func saveItemToCoreData(item: SearchResultItem) {
func searchResultItemDidSelected(item: SearchResultItem,from: UIViewController) {
let searchHistories = self.fetchSearchHistory()
_ = context.managedObjectContext.performChanges { [weak self] in
guard let self = self else { return }
@ -285,6 +287,8 @@ final class SearchViewModel: NSObject {
} else {
SearchHistory.insert(into: self.context.managedObjectContext, hashtag: tagInCoreData)
}
let viewModel = HashtagTimelineViewModel(context: self.context, hashtag: tagInCoreData.name)
self.coordinator.present(scene: .hashtagTimeline(viewModel: viewModel), from: from, transition: .show)
case .accountObjectID(let accountObjectID):
if let searchHistories = searchHistories {
let history = searchHistories.first { history -> Bool in
@ -305,6 +309,9 @@ final class SearchViewModel: NSObject {
history.update(updatedAt: Date())
}
}
let tagInCoreData = self.context.managedObjectContext.object(with: hashtagObjectID) as! Tag
let viewModel = HashtagTimelineViewModel(context: self.context, hashtag: tagInCoreData.name)
self.coordinator.present(scene: .hashtagTimeline(viewModel: viewModel), from: from, transition: .show)
default:
break
}