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. // Created by sxiaojian on 2021/4/22.
// //
import Foundation
import CoreData import CoreData
import Foundation
enum SelectedAccountItem { enum SelectedAccountItem {
case accountObjectID(accountObjectID: NSManagedObjectID) case accountObjectID(accountObjectID: NSManagedObjectID)

View File

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

View File

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

View File

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

View File

@ -23,6 +23,7 @@ final class SuggestionAccountViewModel: NSObject {
// input // input
let context: AppContext let context: AppContext
let currentMastodonUser = CurrentValueSubject<MastodonUser?, Never>(nil)
weak var delegate: SuggestionAccountViewModelDelegate? weak var delegate: SuggestionAccountViewModelDelegate?
// output // output
let accounts = CurrentValueSubject<[NSManagedObjectID], Never>([]) let accounts = CurrentValueSubject<[NSManagedObjectID], Never>([])
@ -60,11 +61,23 @@ final class SuggestionAccountViewModel: NSObject {
selectedAccountsDidChange selectedAccountsDidChange
.sink { [weak self] _ in .sink { [weak self] _ in
if let selectedAccout = self?.selectedAccounts { guard let self = self else { return }
self?.applySelectedCollectionViewDataSource(accounts: selectedAccout) self.applyTableViewDataSource(accounts: self.accounts.value)
} self.applySelectedCollectionViewDataSource(accounts: self.selectedAccounts)
} }
.store(in: &disposeBag) .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 { if accounts == nil || (accounts ?? []).isEmpty {
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return } guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
@ -174,4 +187,17 @@ final class SuggestionAccountViewModel: NSObject {
needFeedback: false 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.topAnchor.constraint(equalTo: contentView.topAnchor),
containerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), containerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
containerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), containerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
]) ])
_imageView.translatesAutoresizingMaskIntoConstraints = false _imageView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.addArrangedSubview(_imageView) containerStackView.addArrangedSubview(_imageView)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
_imageView.widthAnchor.constraint(equalToConstant: 42).priority(.required - 1), _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() let textStackView = UIStackView()
@ -178,7 +178,6 @@ extension SuggestionAccountTableViewCell {
self?.button.isHidden = !isHidden self?.button.isHidden = !isHidden
} }
.store(in: &disposeBag) .store(in: &disposeBag)
} }
func startAnimating() { func startAnimating() {