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