Implement UserView follow button on SearchResultsViewController (IOS-140)

This commit is contained in:
Marcus Kida 2023-04-25 14:00:57 +02:00
parent e2a05cd747
commit 1816c7ce1a
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
7 changed files with 41 additions and 14 deletions

View File

@ -28,7 +28,7 @@ extension SearchHistorySection {
let userCellRegister = UICollectionView.CellRegistration<SearchHistoryUserCollectionViewCell, ManagedObjectRecord<MastodonUser>> { cell, indexPath, item in
context.managedObjectContext.performAndWait {
guard let user = item.object(in: context.managedObjectContext) else { return }
cell.configure(viewModel: .init(value: user))
cell.configure(viewModel: .init(value: user), delegate: configuration.searchHistorySectionHeaderCollectionReusableViewDelegate)
}
}

View File

@ -9,8 +9,9 @@ import os.log
import UIKit
import MastodonAsset
import MastodonLocalization
import MastodonUI
protocol SearchHistorySectionHeaderCollectionReusableViewDelegate: AnyObject {
protocol SearchHistorySectionHeaderCollectionReusableViewDelegate: AnyObject, UserViewDelegate {
func searchHistorySectionHeaderCollectionReusableView(_ searchHistorySectionHeaderCollectionReusableView: SearchHistorySectionHeaderCollectionReusableView, clearButtonDidPressed button: UIButton)
}

View File

@ -7,6 +7,7 @@
import Foundation
import CoreDataStack
import MastodonUI
extension SearchHistoryUserCollectionViewCell {
final class ViewModel {
@ -20,8 +21,9 @@ extension SearchHistoryUserCollectionViewCell {
extension SearchHistoryUserCollectionViewCell {
func configure(
viewModel: ViewModel
viewModel: ViewModel,
delegate: UserViewDelegate?
) {
userView.configure(user: viewModel.value, delegate: nil)
userView.configure(user: viewModel.value, delegate: delegate)
}
}

View File

@ -10,6 +10,7 @@ import UIKit
import Combine
import CoreDataStack
import MastodonCore
import MastodonUI
final class SearchHistoryViewController: UIViewController, NeedsDependency {
@ -123,4 +124,15 @@ extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusa
)
}
}
func userView(_ view: UserView, didTapButtonWith state: UserView.ButtonState, for user: MastodonUser) {
Task {
try await DataSourceFacade.responseToUserViewButtonAction(
dependency: self,
user: user.asRecord,
buttonState: state
)
collectionView.reloadData()
}
}
}

View File

@ -8,6 +8,7 @@
import os.log
import UIKit
import Combine
import CoreDataStack
import MastodonCore
import MastodonUI
@ -55,7 +56,8 @@ extension SearchResultViewController {
// tableView.prefetchDataSource = self
viewModel.setupDiffableDataSource(
tableView: tableView,
statusTableViewCellDelegate: self
statusTableViewCellDelegate: self,
userTableViewCellDelegate: self
)
// setup batch fetch
@ -255,3 +257,17 @@ extension SearchResultViewController: UITableViewDelegate, AutoGenerateTableView
// MARK: - StatusTableViewCellDelegate
extension SearchResultViewController: StatusTableViewCellDelegate { }
// MARK: - UserTableViewCellDelegate
extension SearchResultViewController: UserTableViewCellDelegate {
func userView(_ view: UserView, didTapButtonWith state: UserView.ButtonState, for user: MastodonUser) {
Task {
try await DataSourceFacade.responseToUserViewButtonAction(
dependency: self,
user: user.asRecord,
buttonState: state
)
tableView.reloadData()
}
}
}

View File

@ -12,14 +12,16 @@ extension SearchResultViewModel {
func setupDiffableDataSource(
tableView: UITableView,
statusTableViewCellDelegate: StatusTableViewCellDelegate
statusTableViewCellDelegate: StatusTableViewCellDelegate,
userTableViewCellDelegate: UserTableViewCellDelegate
) {
diffableDataSource = SearchResultSection.tableViewDiffableDataSource(
tableView: tableView,
context: context,
configuration: .init(
authContext: authContext,
statusViewTableViewCellDelegate: statusTableViewCellDelegate
statusViewTableViewCellDelegate: statusTableViewCellDelegate,
userTableViewCellDelegate: userTableViewCellDelegate
)
)

View File

@ -19,13 +19,7 @@ final class UserTableViewCell: UITableViewCell {
weak var delegate: UserTableViewCellDelegate?
let userView: UserView = {
let view = UserView()
[UserView.ButtonState.follow, UserView.ButtonState.unfollow, UserView.ButtonState.blocked].randomElement().map {
view.setButtonState($0)
}
return view
}()
let userView = UserView()
let separatorLine = UIView.separatorLine