mastodon-ios/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewContro...

164 lines
6.0 KiB
Swift
Raw Normal View History

2021-04-21 08:46:31 +02:00
//
// SuggestionAccountViewController.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/21.
//
import Combine
import CoreData
import CoreDataStack
import Foundation
import OSLog
import UIKit
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonUI
import MastodonLocalization
2021-04-21 08:46:31 +02:00
class SuggestionAccountViewController: UIViewController, NeedsDependency {
2021-04-21 08:46:31 +02:00
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
2021-04-21 11:58:56 +02:00
2021-04-21 08:46:31 +02:00
var disposeBag = Set<AnyCancellable>()
var viewModel: SuggestionAccountViewModel!
private static func createCollectionViewLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(64), heightDimension: .absolute(64))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: itemSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 24, leading: 0, bottom: 24, trailing: 0)
section.orthogonalScrollingBehavior = .continuous
section.contentInsetsReference = .readableContent
section.interGroupSpacing = 16
return UICollectionViewCompositionalLayout(section: section)
}
2021-04-21 11:58:56 +02:00
2021-04-21 08:46:31 +02:00
let tableView: UITableView = {
let tableView = ControlContainableTableView()
tableView.register(SuggestionAccountTableViewCell.self, forCellReuseIdentifier: String(describing: SuggestionAccountTableViewCell.self))
tableView.rowHeight = UITableView.automaticDimension
tableView.tableFooterView = UIView()
tableView.separatorStyle = .singleLine
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
return tableView
}()
2021-04-21 11:58:56 +02:00
2021-04-21 08:46:31 +02:00
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", (#file as NSString).lastPathComponent, #line, #function)
}
}
extension SuggestionAccountViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
ThemeService.shared.currentTheme
2022-02-16 10:25:55 +01:00
.receive(on: DispatchQueue.main)
.sink { [weak self] theme in
guard let self = self else { return }
self.setupBackgroundColor(theme: theme)
}
.store(in: &disposeBag)
title = L10n.Scene.SuggestionAccount.title
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: UIBarButtonItem.SystemItem.done,
target: self,
action: #selector(SuggestionAccountViewController.doneButtonDidClick(_:))
)
2021-04-21 11:58:56 +02:00
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
2021-04-21 08:46:31 +02:00
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),
2021-04-21 08:46:31 +02:00
])
tableView.delegate = self
viewModel.setupDiffableDataSource(
tableView: tableView,
suggestionAccountTableViewCellDelegate: self
)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.deselectRow(with: transitionCoordinator, animated: animated)
}
private func setupBackgroundColor(theme: Theme) {
view.backgroundColor = theme.systemBackgroundColor
}
}
2021-04-21 11:58:56 +02:00
// MARK: - UITableViewDelegate
extension SuggestionAccountViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let tableViewDiffableDataSource = viewModel.tableViewDiffableDataSource else { return }
guard let item = tableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .account(let record):
guard let account = record.object(in: context.managedObjectContext) else { return }
let cachedProfileViewModel = CachedProfileViewModel(context: context, authContext: viewModel.authContext, mastodonUser: account)
2022-11-17 23:02:43 +01:00
_ = coordinator.present(
scene: .profile(viewModel: cachedProfileViewModel),
from: self,
transition: .show
)
}
2021-04-21 08:46:31 +02:00
}
}
// MARK: - AuthContextProvider
extension SuggestionAccountViewController: AuthContextProvider {
var authContext: AuthContext { viewModel.authContext }
}
// MARK: - SuggestionAccountTableViewCellDelegate
2021-04-21 08:46:31 +02:00
extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegate {
2022-02-16 10:25:55 +01:00
func suggestionAccountTableViewCell(
_ cell: SuggestionAccountTableViewCell,
friendshipDidPressed button: UIButton
) {
guard let tableViewDiffableDataSource = viewModel.tableViewDiffableDataSource else { return }
guard let indexPath = tableView.indexPath(for: cell) else { return }
guard let item = tableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .account(let user):
Task { @MainActor in
cell.startAnimating()
do {
try await DataSourceFacade.responseToUserFollowAction(
dependency: self,
user: user
2022-02-16 10:25:55 +01:00
)
} catch {
// do noting
}
cell.stopAnimating()
} // end Task
}
2021-04-21 08:46:31 +02:00
}
}
extension SuggestionAccountViewController {
@objc func doneButtonDidClick(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
// if viewModel.selectedAccounts.value.count > 0 {
// viewModel.delegate?.homeTimelineNeedRefresh.send()
// }
2021-04-21 08:46:31 +02:00
}
}