From 67f813a9466c74757d946f803df33da6b75a0545 Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Thu, 22 Apr 2021 20:32:54 +0800 Subject: [PATCH] fix: update followState when view will appear --- .../Diffiable/Item/SelectedAccountItem.swift | 2 +- .../Section/SelectedAccountSection.swift | 2 +- .../SuggestionAccountCollectionViewCell.swift | 11 ++++--- .../SuggestionAccountViewController.swift | 14 ++++---- .../SuggestionAccountViewModel.swift | 32 +++++++++++++++++-- .../SuggestionAccountTableViewCell.swift | 5 ++- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/Mastodon/Diffiable/Item/SelectedAccountItem.swift b/Mastodon/Diffiable/Item/SelectedAccountItem.swift index 2e85efc16..dbfe25cea 100644 --- a/Mastodon/Diffiable/Item/SelectedAccountItem.swift +++ b/Mastodon/Diffiable/Item/SelectedAccountItem.swift @@ -5,8 +5,8 @@ // Created by sxiaojian on 2021/4/22. // -import Foundation import CoreData +import Foundation enum SelectedAccountItem { case accountObjectID(accountObjectID: NSManagedObjectID) diff --git a/Mastodon/Diffiable/Section/SelectedAccountSection.swift b/Mastodon/Diffiable/Section/SelectedAccountSection.swift index 0efd9aebc..4f18ef873 100644 --- a/Mastodon/Diffiable/Section/SelectedAccountSection.swift +++ b/Mastodon/Diffiable/Section/SelectedAccountSection.swift @@ -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 diff --git a/Mastodon/Scene/SuggestionAccount/CollectionViewCell/SuggestionAccountCollectionViewCell.swift b/Mastodon/Scene/SuggestionAccount/CollectionViewCell/SuggestionAccountCollectionViewCell.swift index bb4e422a4..a973e1c52 100644 --- a/Mastodon/Scene/SuggestionAccount/CollectionViewCell/SuggestionAccountCollectionViewCell.swift +++ b/Mastodon/Scene/SuggestionAccount/CollectionViewCell/SuggestionAccountCollectionViewCell.swift @@ -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 = { @@ -18,11 +18,12 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell { imageView.image = UIImage.placeholder(color: .systemFill) return imageView }() - + func configAsPlaceHolder() { 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,15 +31,16 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell { imageTransition: .crossDissolve(0.2) ) } + override func prepareForReuse() { super.prepareForReuse() } - + override init(frame: CGRect) { super.init(frame: .zero) configure() } - + required init?(coder: NSCoder) { super.init(coder: coder) configure() @@ -46,7 +48,6 @@ class SuggestionAccountCollectionViewCell: UICollectionViewCell { } extension SuggestionAccountCollectionViewCell { - private func configure() { contentView.addSubview(imageView) imageView.translatesAutoresizingMaskIntoConstraints = false diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift index f36dc8da9..017c1ee3d 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift @@ -90,7 +90,7 @@ extension SuggestionAccountViewController { ) viewModel.collectionDiffableDataSource = SelectedAccountSection.collectionViewDiffableDataSource(for: selectedCollectionView, managedObjectContext: context.managedObjectContext) - + viewModel.accounts .receive(on: DispatchQueue.main) .sink { [weak self] accounts in @@ -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,15 +141,14 @@ 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) { guard let diffableDataSource = viewModel.collectionDiffableDataSource else { return } guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return } @@ -195,7 +197,7 @@ extension SuggestionAccountViewController: SuggestionAccountTableViewCellDelegat cell.button.isSelected = selected self.viewModel.selectedAccountsDidChange.send() } - }, receiveValue: { relationShip in + }, receiveValue: { _ in }) .store(in: &disposeBag) } diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift index e08e820e4..3a89d1431 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift @@ -23,6 +23,7 @@ final class SuggestionAccountViewModel: NSObject { // input let context: AppContext + let currentMastodonUser = CurrentValueSubject(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() + } } diff --git a/Mastodon/Scene/SuggestionAccount/TableViewCell/SuggestionAccountTableViewCell.swift b/Mastodon/Scene/SuggestionAccount/TableViewCell/SuggestionAccountTableViewCell.swift index d81d8e0e0..a550fd889 100644 --- a/Mastodon/Scene/SuggestionAccount/TableViewCell/SuggestionAccountTableViewCell.swift +++ b/Mastodon/Scene/SuggestionAccount/TableViewCell/SuggestionAccountTableViewCell.swift @@ -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() {