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.
|
|
|
|
//
|
|
|
|
|
2021-04-08 06:47:13 +02:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-03-31 13:29:54 +02:00
|
|
|
import Foundation
|
2021-04-01 14:54:57 +02:00
|
|
|
import MastodonSDK
|
2021-04-02 06:10:12 +02:00
|
|
|
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-02 06:10:12 +02:00
|
|
|
|
2021-04-07 15:01:32 +02:00
|
|
|
hashtagCollectionView.register(SearchRecommendTagsCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: SearchRecommendTagsCollectionViewCell.self))
|
|
|
|
hashtagCollectionView.delegate = self
|
2021-04-02 06:10:12 +02:00
|
|
|
|
2021-04-07 15:01:32 +02:00
|
|
|
stackView.addArrangedSubview(hashtagCollectionView)
|
|
|
|
hashtagCollectionView.constrain([
|
|
|
|
hashtagCollectionView.frameLayoutGuide.heightAnchor.constraint(equalToConstant: 130)
|
2021-04-01 14:54:57 +02:00
|
|
|
])
|
2021-03-31 14:56:11 +02:00
|
|
|
}
|
2021-04-02 06:10:12 +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
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(accountsCollectionView)
|
|
|
|
accountsCollectionView.constrain([
|
|
|
|
accountsCollectionView.frameLayoutGuide.heightAnchor.constraint(equalToConstant: 202)
|
|
|
|
])
|
|
|
|
}
|
2021-04-02 06:10:12 +02:00
|
|
|
|
2021-03-31 14:56:11 +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) {
|
2021-04-02 06:10:12 +02:00
|
|
|
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)
|
2021-04-08 06:47:13 +02:00
|
|
|
switch collectionView {
|
|
|
|
case self.accountsCollectionView:
|
|
|
|
guard let diffableDataSource = viewModel.accountDiffableDataSource else { return }
|
2021-04-09 07:09:30 +02:00
|
|
|
guard let accountObjectID = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
let user = context.managedObjectContext.object(with: accountObjectID) as! MastodonUser
|
|
|
|
viewModel.accountCollectionViewItemDidSelected(mastodonUser: user, from: self)
|
2021-04-08 06:47:13 +02:00
|
|
|
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
|
|
|
|
|
2021-04-02 06:10:12 +02:00
|
|
|
extension SearchViewController: UICollectionViewDelegateFlowLayout {
|
2021-04-01 14:54:57 +02:00
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
2021-04-02 06:10:12 +02:00
|
|
|
UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
|
2021-03-31 14:56:11 +02:00
|
|
|
}
|
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 14:56:11 +02:00
|
|
|
}
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
2021-04-02 06:10:12 +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-01 14:54:57 +02:00
|
|
|
return CGSize(width: 228, height: 130)
|
|
|
|
} else {
|
|
|
|
return CGSize(width: 257, height: 202)
|
|
|
|
}
|
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-02 06:10:12 +02:00
|
|
|
|
|
|
|
@objc func accountSeeAllButtonPressed(_ sender: UIButton) {}
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|