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
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-10-10 13:14:52 +02:00
|
|
|
import MastodonUI
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-04-21 08:46:31 +02:00
|
|
|
|
|
|
|
class SuggestionAccountViewController: UIViewController, NeedsDependency {
|
2023-05-12 21:04:47 +02:00
|
|
|
|
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!
|
2022-02-10 12:30:41 +01:00
|
|
|
|
|
|
|
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()
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
2022-02-16 10:25:55 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-02-10 12:30:41 +01:00
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-22 14:32:54 +02:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
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
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(tableView)
|
2021-04-21 08:46:31 +02:00
|
|
|
NSLayoutConstraint.activate([
|
2023-05-12 21:04:47 +02:00
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
2022-02-10 12:30:41 +01:00
|
|
|
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
|
|
|
])
|
2022-02-10 12:30:41 +01:00
|
|
|
|
|
|
|
tableView.delegate = self
|
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
tableView: tableView,
|
|
|
|
suggestionAccountTableViewCellDelegate: self
|
|
|
|
)
|
|
|
|
}
|
2021-04-22 13:58:42 +02:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
tableView.deselectRow(with: transitionCoordinator, animated: animated)
|
2021-04-22 13:58:42 +02:00
|
|
|
}
|
2021-07-20 13:24:24 +02:00
|
|
|
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
|
|
|
view.backgroundColor = theme.systemBackgroundColor
|
2021-04-22 13:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 11:58:56 +02:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
// MARK: - UITableViewDelegate
|
2021-04-22 13:58:42 +02:00
|
|
|
extension SuggestionAccountViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2022-02-10 12:30:41 +01:00
|
|
|
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 }
|
2022-10-09 14:07:57 +02:00
|
|
|
let cachedProfileViewModel = CachedProfileViewModel(context: context, authContext: viewModel.authContext, mastodonUser: account)
|
2022-11-17 23:02:43 +01:00
|
|
|
_ = coordinator.present(
|
2022-02-10 12:30:41 +01:00
|
|
|
scene: .profile(viewModel: cachedProfileViewModel),
|
|
|
|
from: self,
|
|
|
|
transition: .show
|
|
|
|
)
|
2021-04-22 13:58:42 +02:00
|
|
|
}
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 14:07:57 +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,
|
2022-10-09 14:07:57 +02:00
|
|
|
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)
|
2022-02-10 12:30:41 +01:00
|
|
|
// if viewModel.selectedAccounts.value.count > 0 {
|
|
|
|
// viewModel.delegate?.homeTimelineNeedRefresh.send()
|
|
|
|
// }
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|
|
|
|
}
|