fix: update followState when view will appear

This commit is contained in:
sunxiaojian 2021-04-22 20:32:54 +08:00
parent e664722b13
commit 67f813a946
6 changed files with 47 additions and 19 deletions

View File

@ -5,8 +5,8 @@
// Created by sxiaojian on 2021/4/22.
//
import Foundation
import CoreData
import Foundation
enum SelectedAccountItem {
case accountObjectID(accountObjectID: NSManagedObjectID)

View File

@ -26,7 +26,7 @@ extension SelectedAccountSection {
case .accountObjectID(let objectID):
let user = managedObjectContext.object(with: objectID) as! MastodonUser
cell.config(with: user)
case .placeHolder( _):
case .placeHolder:
cell.configAsPlaceHolder()
}
return cell

View File

@ -5,9 +5,9 @@
// Created by sxiaojian on 2021/4/22.
//
import CoreDataStack
import Foundation
import UIKit
import CoreDataStack
class SuggestionAccountCollectionViewCell: UICollectionViewCell {
let imageView: UIImageView = {
@ -23,6 +23,7 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell {
imageView.tintColor = Asset.Colors.Label.tertiary.color
imageView.image = UIImage.placeholder(color: .systemFill)
}
func config(with mastodonUser: MastodonUser) {
imageView.af.setImage(
withURL: URL(string: mastodonUser.avatar)!,
@ -30,6 +31,7 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell {
imageTransition: .crossDissolve(0.2)
)
}
override func prepareForReuse() {
super.prepareForReuse()
}
@ -46,7 +48,6 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell {
}
extension SuggestionAccountCollectionViewCell {
private func configure() {
contentView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false

View File

@ -103,7 +103,9 @@ extension SuggestionAccountViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.deselectRow(with: transitionCoordinator, animated: animated)
viewModel.checkAccountsFollowState()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let avatarImageViewHeight: Double = 56
@ -111,6 +113,7 @@ extension SuggestionAccountViewController {
viewModel.headerPlaceholderCount = avatarImageViewCount
viewModel.applySelectedCollectionViewDataSource(accounts: [])
}
func setupHeader(accounts: [NSManagedObjectID]) {
if accounts.isEmpty {
return
@ -138,13 +141,12 @@ extension SuggestionAccountViewController {
}
extension SuggestionAccountViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 15
15
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 56, height: 56)
CGSize(width: 56, height: 56)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
@ -195,7 +197,7 @@ extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegat
cell.button.isSelected = selected
self.viewModel.selectedAccountsDidChange.send()
}
}, receiveValue: { relationShip in
}, receiveValue: { _ in
})
.store(in: &disposeBag)
}

View File

@ -23,6 +23,7 @@ final class SuggestionAccountViewModel: NSObject {
// input
let context: AppContext
let currentMastodonUser = CurrentValueSubject<MastodonUser?, Never>(nil)
weak var delegate: SuggestionAccountViewModelDelegate?
// output
let accounts = CurrentValueSubject<[NSManagedObjectID], Never>([])
@ -60,11 +61,23 @@ final class SuggestionAccountViewModel: NSObject {
selectedAccountsDidChange
.sink { [weak self] _ in
if let selectedAccout = self?.selectedAccounts {
self?.applySelectedCollectionViewDataSource(accounts: selectedAccout)
}
guard let self = self else { return }
self.applyTableViewDataSource(accounts: self.accounts.value)
self.applySelectedCollectionViewDataSource(accounts: self.selectedAccounts)
}
.store(in: &disposeBag)
context.authenticationService.activeMastodonAuthentication
.sink { [weak self] activeMastodonAuthentication in
guard let self = self else { return }
guard let activeMastodonAuthentication = activeMastodonAuthentication else {
self.currentMastodonUser.value = nil
return
}
self.currentMastodonUser.value = activeMastodonAuthentication.user
}
.store(in: &disposeBag)
if accounts == nil || (accounts ?? []).isEmpty {
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
@ -174,4 +187,17 @@ final class SuggestionAccountViewModel: NSObject {
needFeedback: false
)
}
func checkAccountsFollowState() {
guard let currentMastodonUser = currentMastodonUser.value else {
return
}
let users = accounts.value.compactMap { context.managedObjectContext.object(with: $0) as? MastodonUser }
let followingUsers = users.filter { user -> Bool in
let isFollowing = user.followingBy.flatMap { $0.contains(currentMastodonUser) } ?? false
return isFollowing
}.map(\.objectID)
selectedAccounts = followingUsers
selectedAccountsDidChange.send()
}
}

View File

@ -101,14 +101,14 @@ extension SuggestionAccountTableViewCell {
containerStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
containerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
containerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
_imageView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.addArrangedSubview(_imageView)
NSLayoutConstraint.activate([
_imageView.widthAnchor.constraint(equalToConstant: 42).priority(.required - 1),
_imageView.heightAnchor.constraint(equalToConstant: 42).priority(.required - 1)
_imageView.heightAnchor.constraint(equalToConstant: 42).priority(.required - 1),
])
let textStackView = UIStackView()
@ -178,7 +178,6 @@ extension SuggestionAccountTableViewCell {
self?.button.isHidden = !isHidden
}
.store(in: &disposeBag)
}
func startAnimating() {