chore: observe Follow state
This commit is contained in:
parent
0418ec1470
commit
c74314ef11
|
@ -18,12 +18,15 @@ enum RecommendAccountSection: Equatable, Hashable {
|
|||
extension RecommendAccountSection {
|
||||
static func collectionViewDiffableDataSource(
|
||||
for collectionView: UICollectionView,
|
||||
managedObjectContext: NSManagedObjectContext
|
||||
context: AppContext!
|
||||
) -> UICollectionViewDiffableDataSource<RecommendAccountSection, NSManagedObjectID> {
|
||||
UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, objectID -> UICollectionViewCell? in
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: SearchRecommendAccountsCollectionViewCell.self), for: indexPath) as! SearchRecommendAccountsCollectionViewCell
|
||||
let account = managedObjectContext.object(with: objectID) as! MastodonUser
|
||||
cell.config(with: account)
|
||||
let user = context.managedObjectContext.object(with: objectID) as! MastodonUser
|
||||
cell.config(with: user)
|
||||
if let currentUser = context.authenticationService.activeMastodonAuthentication.value?.user {
|
||||
cell.configFollowButton(with: user, currentMastodonUser: currentUser)
|
||||
}
|
||||
return cell
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,12 @@ import Foundation
|
|||
import MastodonSDK
|
||||
import UIKit
|
||||
import CoreDataStack
|
||||
import Combine
|
||||
|
||||
class SearchRecommendAccountsCollectionViewCell: UICollectionViewCell {
|
||||
|
||||
var disposeBag = Set<AnyCancellable>()
|
||||
|
||||
let avatarImageView: UIImageView = {
|
||||
let imageView = UIImageView()
|
||||
imageView.layer.cornerRadius = 8.4
|
||||
|
@ -123,16 +127,16 @@ extension SearchRecommendAccountsCollectionViewCell {
|
|||
])
|
||||
}
|
||||
|
||||
func config(with account: MastodonUser) {
|
||||
displayNameLabel.text = account.displayName.isEmpty ? account.username : account.displayName
|
||||
acctLabel.text = account.acct
|
||||
func config(with mastodonUser: MastodonUser) {
|
||||
displayNameLabel.text = mastodonUser.displayName.isEmpty ? mastodonUser.username : mastodonUser.displayName
|
||||
acctLabel.text = mastodonUser.acct
|
||||
avatarImageView.af.setImage(
|
||||
withURL: URL(string: account.avatar)!,
|
||||
withURL: URL(string: mastodonUser.avatar)!,
|
||||
placeholderImage: UIImage.placeholder(color: .systemFill),
|
||||
imageTransition: .crossDissolve(0.2)
|
||||
)
|
||||
headerImageView.af.setImage(
|
||||
withURL: URL(string: account.header)!,
|
||||
withURL: URL(string: mastodonUser.header)!,
|
||||
placeholderImage: UIImage.placeholder(color: .systemFill),
|
||||
imageTransition: .crossDissolve(0.2)) { [weak self] _ in
|
||||
guard let self = self else { return }
|
||||
|
@ -140,6 +144,44 @@ extension SearchRecommendAccountsCollectionViewCell {
|
|||
self.visualEffectView.pin(top: 0, left: 0, bottom: 0, right: 0)
|
||||
}
|
||||
}
|
||||
|
||||
func configFollowButton(with mastodonUser: MastodonUser, currentMastodonUser: MastodonUser) {
|
||||
self._configFollowButton(with: mastodonUser, currentMastodonUser: currentMastodonUser)
|
||||
ManagedObjectObserver.observe(object: currentMastodonUser)
|
||||
.sink { _ in
|
||||
|
||||
} receiveValue: { change in
|
||||
guard case .update(let object) = change.changeType,
|
||||
let newUser = object as? MastodonUser else { return }
|
||||
self._configFollowButton(with: mastodonUser, currentMastodonUser: newUser)
|
||||
}
|
||||
.store(in: &disposeBag)
|
||||
}
|
||||
|
||||
func _configFollowButton(with mastodonUser: MastodonUser, currentMastodonUser: MastodonUser) {
|
||||
var relationshipActionSet = ProfileViewModel.RelationshipActionOptionSet([.follow])
|
||||
|
||||
let isFollowing = mastodonUser.followingBy.flatMap { $0.contains(currentMastodonUser) } ?? false
|
||||
if isFollowing {
|
||||
relationshipActionSet.insert(.following)
|
||||
}
|
||||
|
||||
let isPending = mastodonUser.followRequestedBy.flatMap { $0.contains(currentMastodonUser) } ?? false
|
||||
if isPending {
|
||||
relationshipActionSet.insert(.pending)
|
||||
}
|
||||
|
||||
let isBlocking = mastodonUser.blockingBy.flatMap { $0.contains(currentMastodonUser) } ?? false
|
||||
if isBlocking {
|
||||
relationshipActionSet.insert(.blocking)
|
||||
}
|
||||
|
||||
let isBlockedBy = currentMastodonUser.blockingBy.flatMap { $0.contains(mastodonUser) } ?? false
|
||||
if isBlockedBy {
|
||||
relationshipActionSet.insert(.blocked)
|
||||
}
|
||||
self.followButton.setTitle(relationshipActionSet.title, for: .normal)
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(SwiftUI) && DEBUG
|
||||
|
|
|
@ -150,7 +150,7 @@ extension SearchViewController {
|
|||
|
||||
func setupDataSource() {
|
||||
viewModel.hashtagDiffableDataSource = RecommendHashTagSection.collectionViewDiffableDataSource(for: hashtagCollectionView)
|
||||
viewModel.accountDiffableDataSource = RecommendAccountSection.collectionViewDiffableDataSource(for: accountsCollectionView, managedObjectContext: context.managedObjectContext)
|
||||
viewModel.accountDiffableDataSource = RecommendAccountSection.collectionViewDiffableDataSource(for: accountsCollectionView, context: context)
|
||||
viewModel.searchResultDiffableDataSource = SearchResultSection.tableViewDiffableDataSource(for: searchingTableView, dependency: self)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue