mastodon-ios/Mastodon/Scene/Search/SearchViewController+Recomm...

112 lines
5.2 KiB
Swift
Raw Normal View History

2021-03-31 13:29:54 +02:00
//
2021-04-08 08:01:22 +02:00
// SearchViewController+Recommend.swift
2021-03-31 13:29:54 +02:00
// Mastodon
//
// Created by sxiaojian on 2021/3/31.
//
import CoreData
import CoreDataStack
2021-03-31 13:29:54 +02:00
import Foundation
2021-04-01 14:54:57 +02:00
import MastodonSDK
import OSLog
import UIKit
2021-03-31 13:29:54 +02:00
extension SearchViewController {
2021-04-01 14:54:57 +02:00
func setupHashTagCollectionView() {
let header = SearchRecommendCollectionHeader()
header.titleLabel.text = L10n.Scene.Search.Recommend.HashTag.title
header.descriptionLabel.text = L10n.Scene.Search.Recommend.HashTag.description
2021-04-07 15:01:32 +02:00
header.seeAllButton.addTarget(self, action: #selector(SearchViewController.hashtagSeeAllButtonPressed(_:)), for: .touchUpInside)
2021-04-01 14:54:57 +02:00
stackView.addArrangedSubview(header)
2021-04-07 15:01:32 +02:00
hashtagCollectionView.register(SearchRecommendTagsCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: SearchRecommendTagsCollectionViewCell.self))
hashtagCollectionView.delegate = self
2021-04-19 05:41:50 +02:00
hashtagCollectionView.translatesAutoresizingMaskIntoConstraints = false
2021-04-07 15:01:32 +02:00
stackView.addArrangedSubview(hashtagCollectionView)
2021-04-19 05:41:50 +02:00
NSLayoutConstraint.activate([
2021-04-22 08:36:29 +02:00
hashtagCollectionView.frameLayoutGuide.heightAnchor.constraint(equalToConstant: CGFloat(SearchViewController.hashtagCardHeight))
2021-04-01 14:54:57 +02:00
])
}
2021-04-01 14:54:57 +02:00
func setupAccountsCollectionView() {
let header = SearchRecommendCollectionHeader()
header.titleLabel.text = L10n.Scene.Search.Recommend.Accounts.title
header.descriptionLabel.text = L10n.Scene.Search.Recommend.Accounts.description
header.seeAllButton.addTarget(self, action: #selector(SearchViewController.accountSeeAllButtonPressed(_:)), for: .touchUpInside)
stackView.addArrangedSubview(header)
accountsCollectionView.register(SearchRecommendAccountsCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: SearchRecommendAccountsCollectionViewCell.self))
accountsCollectionView.delegate = self
2021-04-19 05:41:50 +02:00
accountsCollectionView.translatesAutoresizingMaskIntoConstraints = false
2021-04-01 14:54:57 +02:00
stackView.addArrangedSubview(accountsCollectionView)
2021-04-19 05:41:50 +02:00
NSLayoutConstraint.activate([
2021-04-22 08:36:29 +02:00
accountsCollectionView.frameLayoutGuide.heightAnchor.constraint(equalToConstant: CGFloat(SearchViewController.accountCardHeight))
2021-04-01 14:54:57 +02:00
])
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
2021-04-07 15:01:32 +02:00
hashtagCollectionView.collectionViewLayout.invalidateLayout()
2021-04-01 14:54:57 +02:00
accountsCollectionView.collectionViewLayout.invalidateLayout()
2021-03-31 13:29:54 +02:00
}
}
extension SearchViewController: UICollectionViewDelegate {
2021-04-01 14:54:57 +02:00
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: indexPath: %s", (#file as NSString).lastPathComponent, #line, #function, indexPath.debugDescription)
2021-04-01 14:54:57 +02:00
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
switch collectionView {
case self.accountsCollectionView:
guard let diffableDataSource = viewModel.accountDiffableDataSource else { return }
guard let accountObjectID = diffableDataSource.itemIdentifier(for: indexPath) else { return }
let user = context.managedObjectContext.object(with: accountObjectID) as! MastodonUser
viewModel.accountCollectionViewItemDidSelected(mastodonUser: user, from: self)
case self.hashtagCollectionView:
guard let diffableDataSource = viewModel.hashtagDiffableDataSource else { return }
guard let hashtag = diffableDataSource.itemIdentifier(for: indexPath) else { return }
viewModel.hashtagCollectionViewItemDidSelected(hashtag: hashtag, from: self)
default:
break
}
2021-04-01 14:54:57 +02:00
}
2021-03-31 13:29:54 +02:00
}
2021-04-01 14:54:57 +02:00
// MARK: - UICollectionViewDelegateFlowLayout
extension SearchViewController: UICollectionViewDelegateFlowLayout {
2021-04-01 14:54:57 +02:00
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
}
2021-04-01 14:54:57 +02:00
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
2021-04-07 15:01:32 +02:00
if collectionView == hashtagCollectionView {
2021-04-01 14:54:57 +02:00
return 6
} else {
return 12
}
2021-03-31 13:29:54 +02:00
}
2021-04-01 14:54:57 +02:00
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
2021-04-07 15:01:32 +02:00
if collectionView == hashtagCollectionView {
2021-04-22 08:36:29 +02:00
return CGSize(width: 228, height: SearchViewController.hashtagCardHeight)
2021-04-01 14:54:57 +02:00
} else {
2021-04-22 08:36:29 +02:00
return CGSize(width: 257, height: SearchViewController.accountCardHeight)
2021-04-01 14:54:57 +02:00
}
2021-03-31 13:29:54 +02:00
}
2021-04-01 14:54:57 +02:00
}
extension SearchViewController {
2021-04-07 15:01:32 +02:00
@objc func hashtagSeeAllButtonPressed(_ sender: UIButton) {}
2021-04-21 11:58:56 +02:00
@objc func accountSeeAllButtonPressed(_ sender: UIButton) {
if self.viewModel.recommendAccounts.isEmpty {
return
}
let viewModel = SuggestionAccountViewModel(context: context, accounts: self.viewModel.recommendAccounts)
coordinator.present(scene: .suggestionAccount(viewModel: viewModel), from: self, transition: .modal(animated: true, completion: nil))
}
2021-03-31 13:29:54 +02:00
}