mirror of
https://github.com/mastodon/mastodon-ios.git
synced 2025-02-03 02:37:37 +01:00
Use accounts on FavoritedBy/RetootedBy-screens (IOS-214)
This commit is contained in:
parent
195029fc15
commit
9a5b4a3621
@ -11,7 +11,6 @@ import CoreDataStack
|
||||
import MastodonSDK
|
||||
|
||||
enum UserItem: Hashable {
|
||||
case user(record: ManagedObjectRecord<MastodonUser>)
|
||||
case account(account: Mastodon.Entity.Account, relationship: Mastodon.Entity.Relationship?)
|
||||
case bottomLoader
|
||||
case bottomHeader(text: String)
|
||||
|
@ -48,27 +48,6 @@ extension UserSection {
|
||||
delegate: userTableViewCellDelegate
|
||||
)
|
||||
|
||||
return cell
|
||||
|
||||
case .user(let record):
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UserTableViewCell.self), for: indexPath) as! UserTableViewCell
|
||||
context.managedObjectContext.performAndWait {
|
||||
guard let user = record.object(in: context.managedObjectContext) else { return }
|
||||
configure(
|
||||
context: context,
|
||||
authContext: authContext,
|
||||
tableView: tableView,
|
||||
cell: cell,
|
||||
viewModel: UserTableViewCell.ViewModel(
|
||||
user: user,
|
||||
followedUsers: authContext.mastodonAuthenticationBox.inMemoryCache.$followingUserIds.eraseToAnyPublisher(),
|
||||
blockedUsers: authContext.mastodonAuthenticationBox.inMemoryCache.$blockedUserIds.eraseToAnyPublisher(),
|
||||
followRequestedUsers: authContext.mastodonAuthenticationBox.inMemoryCache.$followRequestedUserIDs.eraseToAnyPublisher()
|
||||
),
|
||||
userTableViewCellDelegate: userTableViewCellDelegate
|
||||
)
|
||||
}
|
||||
|
||||
return cell
|
||||
case .bottomLoader:
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
|
||||
@ -82,23 +61,3 @@ extension UserSection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UserSection {
|
||||
|
||||
static func configure(
|
||||
context: AppContext,
|
||||
authContext: AuthContext,
|
||||
tableView: UITableView,
|
||||
cell: UserTableViewCell,
|
||||
viewModel: UserTableViewCell.ViewModel,
|
||||
userTableViewCellDelegate: UserTableViewCellDelegate?
|
||||
) {
|
||||
cell.configure(
|
||||
me: authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext),
|
||||
tableView: tableView,
|
||||
viewModel: viewModel,
|
||||
delegate: userTableViewCellDelegate
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,10 +20,10 @@ extension FavoritedByViewController: DataSourceProvider {
|
||||
}
|
||||
|
||||
switch item {
|
||||
case .user(let record):
|
||||
return .user(record: record)
|
||||
default:
|
||||
return nil
|
||||
case .account(let account, let relationship):
|
||||
return .account(account: account, relationship: relationship)
|
||||
case .bottomHeader(_), .bottomLoader:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,10 +20,10 @@ extension RebloggedByViewController: DataSourceProvider {
|
||||
}
|
||||
|
||||
switch item {
|
||||
case .user(let record):
|
||||
return .user(record: record)
|
||||
default:
|
||||
return nil
|
||||
case .account(let account, let relationship):
|
||||
return .account(account: account, relationship: relationship)
|
||||
case .bottomHeader(_), .bottomLoader:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import UIKit
|
||||
import MastodonAsset
|
||||
import MastodonLocalization
|
||||
import Combine
|
||||
import MastodonSDK
|
||||
|
||||
extension UserListViewModel {
|
||||
@MainActor
|
||||
@ -33,17 +34,24 @@ extension UserListViewModel {
|
||||
// trigger initial loading
|
||||
stateMachine.enter(UserListViewModel.State.Reloading.self)
|
||||
|
||||
userFetchedResultsController.$records
|
||||
$accounts
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] records in
|
||||
guard let self = self else { return }
|
||||
.sink { [weak self] accounts in
|
||||
guard let self else { return }
|
||||
guard let diffableDataSource = self.diffableDataSource else { return }
|
||||
|
||||
var snapshot = NSDiffableDataSourceSnapshot<UserSection, UserItem>()
|
||||
snapshot.appendSections([.main])
|
||||
let items = records.map { UserItem.user(record: $0) }
|
||||
|
||||
let accountsWithRelationship: [(account: Mastodon.Entity.Account, relationship: Mastodon.Entity.Relationship?)] = accounts.compactMap { account in
|
||||
guard let relationship = self.relationships.first(where: {$0.id == account.id }) else { return (account: account, relationship: nil)}
|
||||
|
||||
return (account: account, relationship: relationship)
|
||||
}
|
||||
|
||||
let items = accountsWithRelationship.map { UserItem.account(account: $0.account, relationship: $0.relationship) }
|
||||
snapshot.appendItems(items, toSection: .main)
|
||||
|
||||
|
||||
if let currentState = self.stateMachine.currentState {
|
||||
switch currentState {
|
||||
case is State.Initial, is State.Idle, is State.Reloading, is State.Loading, is State.Fail:
|
||||
|
@ -52,11 +52,12 @@ extension UserListViewModel.State {
|
||||
|
||||
override func didEnter(from previousState: GKState?) {
|
||||
super.didEnter(from: previousState)
|
||||
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
||||
guard let viewModel, let stateMachine else { return }
|
||||
|
||||
// reset
|
||||
viewModel.userFetchedResultsController.userIDs = []
|
||||
|
||||
viewModel.accounts = []
|
||||
viewModel.relationships = []
|
||||
|
||||
stateMachine.enter(Loading.self)
|
||||
}
|
||||
}
|
||||
@ -123,40 +124,61 @@ extension UserListViewModel.State {
|
||||
|
||||
Task {
|
||||
do {
|
||||
let response: Mastodon.Response.Content<[Mastodon.Entity.Account]>
|
||||
let accountResponse: Mastodon.Response.Content<[Mastodon.Entity.Account]>
|
||||
switch viewModel.kind {
|
||||
case .favoritedBy(let status):
|
||||
response = try await viewModel.context.apiService.favoritedBy(
|
||||
accountResponse = try await viewModel.context.apiService.favoritedBy(
|
||||
status: status,
|
||||
query: .init(maxID: maxID, limit: nil),
|
||||
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
||||
)
|
||||
case .rebloggedBy(let status):
|
||||
response = try await viewModel.context.apiService.rebloggedBy(
|
||||
accountResponse = try await viewModel.context.apiService.rebloggedBy(
|
||||
status: status,
|
||||
query: .init(maxID: maxID, limit: nil),
|
||||
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
||||
)
|
||||
}
|
||||
|
||||
if accountResponse.value.isEmpty {
|
||||
await enter(state: NoMore.self)
|
||||
|
||||
viewModel.accounts = []
|
||||
viewModel.relationships = []
|
||||
return
|
||||
}
|
||||
|
||||
var hasNewAppend = false
|
||||
var userIDs = viewModel.userFetchedResultsController.userIDs
|
||||
for user in response.value {
|
||||
guard !userIDs.contains(user.id) else { continue }
|
||||
userIDs.append(user.id)
|
||||
|
||||
let newRelationships = try await viewModel.context.apiService.relationship(forAccounts: accountResponse.value, authenticationBox: viewModel.authContext.mastodonAuthenticationBox)
|
||||
|
||||
var accounts = viewModel.accounts
|
||||
|
||||
for user in accountResponse.value {
|
||||
guard accounts.contains(user) == false else { continue }
|
||||
accounts.append(user)
|
||||
hasNewAppend = true
|
||||
}
|
||||
|
||||
let maxID = response.link?.maxID
|
||||
|
||||
|
||||
var relationships = viewModel.relationships
|
||||
|
||||
for relationship in newRelationships.value {
|
||||
guard relationships.contains(relationship) == false else { continue }
|
||||
relationships.append(relationship)
|
||||
}
|
||||
|
||||
let maxID = accountResponse.link?.maxID
|
||||
|
||||
if hasNewAppend, maxID != nil {
|
||||
await enter(state: Idle.self)
|
||||
} else {
|
||||
await enter(state: NoMore.self)
|
||||
}
|
||||
|
||||
viewModel.accounts = accounts
|
||||
viewModel.relationships = relationships
|
||||
self.maxID = maxID
|
||||
viewModel.userFetchedResultsController.userIDs = userIDs
|
||||
|
||||
|
||||
} catch {
|
||||
await enter(state: Fail.self)
|
||||
}
|
||||
@ -177,9 +199,9 @@ extension UserListViewModel.State {
|
||||
override func didEnter(from previousState: GKState?) {
|
||||
super.didEnter(from: previousState)
|
||||
|
||||
guard let viewModel = viewModel else { return }
|
||||
guard let viewModel else { return }
|
||||
// trigger reload
|
||||
viewModel.userFetchedResultsController.userIDs = viewModel.userFetchedResultsController.userIDs
|
||||
viewModel.accounts = viewModel.accounts
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import Combine
|
||||
import CoreDataStack
|
||||
import GameplayKit
|
||||
import MastodonCore
|
||||
import MastodonSDK
|
||||
|
||||
final class UserListViewModel {
|
||||
var disposeBag = Set<AnyCancellable>()
|
||||
@ -18,7 +19,8 @@ final class UserListViewModel {
|
||||
let context: AppContext
|
||||
let authContext: AuthContext
|
||||
let kind: Kind
|
||||
let userFetchedResultsController: UserFetchedResultsController
|
||||
@Published var accounts: [Mastodon.Entity.Account]
|
||||
@Published var relationships: [Mastodon.Entity.Relationship]
|
||||
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
||||
|
||||
// output
|
||||
@ -43,12 +45,8 @@ final class UserListViewModel {
|
||||
self.context = context
|
||||
self.authContext = authContext
|
||||
self.kind = kind
|
||||
self.userFetchedResultsController = UserFetchedResultsController(
|
||||
managedObjectContext: context.managedObjectContext,
|
||||
domain: authContext.mastodonAuthenticationBox.domain,
|
||||
additionalPredicate: nil
|
||||
)
|
||||
// end init
|
||||
self.accounts = []
|
||||
self.relationships = []
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,111 +0,0 @@
|
||||
//
|
||||
// UserFetchedResultsController.swift
|
||||
// Mastodon
|
||||
//
|
||||
// Created by MainasuK Cirno on 2021-7-7.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import Combine
|
||||
import CoreData
|
||||
import CoreDataStack
|
||||
import MastodonSDK
|
||||
|
||||
public final class UserFetchedResultsController: NSObject {
|
||||
|
||||
var disposeBag = Set<AnyCancellable>()
|
||||
|
||||
let fetchedResultsController: NSFetchedResultsController<MastodonUser>
|
||||
|
||||
// input
|
||||
@Published public var domain: String? = nil
|
||||
@Published public var userIDs: [Mastodon.Entity.Account.ID] = []
|
||||
@Published public var additionalPredicate: NSPredicate?
|
||||
|
||||
// output
|
||||
let _objectIDs = CurrentValueSubject<[NSManagedObjectID], Never>([])
|
||||
@Published public private(set) var records: [ManagedObjectRecord<MastodonUser>] = []
|
||||
|
||||
public init(
|
||||
managedObjectContext: NSManagedObjectContext,
|
||||
domain: String?,
|
||||
additionalPredicate: NSPredicate?
|
||||
) {
|
||||
self.domain = domain ?? ""
|
||||
self.fetchedResultsController = {
|
||||
let fetchRequest = MastodonUser.sortedFetchRequest
|
||||
fetchRequest.predicate = MastodonUser.predicate(domain: domain ?? "", ids: [])
|
||||
fetchRequest.returnsObjectsAsFaults = false
|
||||
fetchRequest.fetchBatchSize = 20
|
||||
let controller = NSFetchedResultsController(
|
||||
fetchRequest: fetchRequest,
|
||||
managedObjectContext: managedObjectContext,
|
||||
sectionNameKeyPath: nil,
|
||||
cacheName: nil
|
||||
)
|
||||
|
||||
return controller
|
||||
}()
|
||||
self.additionalPredicate = additionalPredicate
|
||||
super.init()
|
||||
|
||||
// debounce output to prevent UI update issues
|
||||
_objectIDs
|
||||
.throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true)
|
||||
.map { objectIDs in objectIDs.map { ManagedObjectRecord(objectID: $0) } }
|
||||
.assign(to: &$records)
|
||||
|
||||
fetchedResultsController.delegate = self
|
||||
|
||||
Publishers.CombineLatest3(
|
||||
self.$domain.removeDuplicates(),
|
||||
self.$userIDs.removeDuplicates(),
|
||||
self.$additionalPredicate.removeDuplicates()
|
||||
)
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] domain, ids, additionalPredicate in
|
||||
guard let self = self else { return }
|
||||
var predicates = [MastodonUser.predicate(domain: domain ?? "", ids: ids)]
|
||||
if let additionalPredicate = additionalPredicate {
|
||||
predicates.append(additionalPredicate)
|
||||
}
|
||||
self.fetchedResultsController.fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
|
||||
do {
|
||||
try self.fetchedResultsController.performFetch()
|
||||
} catch {
|
||||
assertionFailure(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
.store(in: &disposeBag)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension UserFetchedResultsController {
|
||||
|
||||
public func append(userIDs: [Mastodon.Entity.Account.ID]) {
|
||||
var result = self.userIDs
|
||||
for userID in userIDs where !result.contains(userID) {
|
||||
result.append(userID)
|
||||
}
|
||||
self.userIDs = result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - NSFetchedResultsControllerDelegate
|
||||
extension UserFetchedResultsController: NSFetchedResultsControllerDelegate {
|
||||
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
|
||||
|
||||
let indexes = userIDs
|
||||
let objects = fetchedResultsController.fetchedObjects ?? []
|
||||
|
||||
let items: [NSManagedObjectID] = objects
|
||||
.compactMap { object in
|
||||
indexes.firstIndex(of: object.id).map { index in (index, object) }
|
||||
}
|
||||
.sorted { $0.0 < $1.0 }
|
||||
.map { $0.1.objectID }
|
||||
self._objectIDs.value = items
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user