mastodon-ios/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel....

103 lines
3.8 KiB
Swift
Raw Normal View History

2021-04-21 08:46:31 +02:00
//
// SuggestionAccountViewModel.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/21.
//
import Combine
import CoreData
import CoreDataStack
import GameplayKit
import MastodonSDK
2022-10-08 07:43:06 +02:00
import MastodonCore
2021-04-21 08:46:31 +02:00
import os.log
import UIKit
protocol SuggestionAccountViewModelDelegate: AnyObject {
2021-04-22 04:11:19 +02:00
var homeTimelineNeedRefresh: PassthroughSubject<Void, Never> { get }
}
2021-04-22 04:29:53 +02:00
2021-04-21 08:46:31 +02:00
final class SuggestionAccountViewModel: NSObject {
var disposeBag = Set<AnyCancellable>()
weak var delegate: SuggestionAccountViewModelDelegate?
2021-04-21 08:46:31 +02:00
// input
let context: AppContext
let authContext: AuthContext
let userFetchedResultsController: UserFetchedResultsController
2022-02-16 10:25:55 +01:00
let selectedUserFetchedResultsController: UserFetchedResultsController
2021-04-21 11:58:56 +02:00
var viewWillAppear = PassthroughSubject<Void, Never>()
// output
var collectionViewDiffableDataSource: UICollectionViewDiffableDataSource<SelectedAccountSection, SelectedAccountItem>?
var tableViewDiffableDataSource: UITableViewDiffableDataSource<RecommendAccountSection, RecommendAccountItem>?
init(
context: AppContext,
authContext: AuthContext
) {
2021-04-21 08:46:31 +02:00
self.context = context
self.authContext = authContext
self.userFetchedResultsController = UserFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: nil,
2022-02-16 10:25:55 +01:00
additionalPredicate: nil
)
self.selectedUserFetchedResultsController = UserFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: nil,
additionalPredicate: nil
)
super.init()
2022-02-16 10:25:55 +01:00
userFetchedResultsController.domain = authContext.mastodonAuthenticationBox.domain
selectedUserFetchedResultsController.domain = authContext.mastodonAuthenticationBox.domain
2022-02-16 10:25:55 +01:00
selectedUserFetchedResultsController.additionalPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [
MastodonUser.predicate(followingBy: authContext.mastodonAuthenticationBox.userID),
MastodonUser.predicate(followRequestedBy: authContext.mastodonAuthenticationBox.userID)
2022-02-16 10:25:55 +01:00
])
2022-02-16 10:25:55 +01:00
// fetch recomment users
Task {
var userIDs: [MastodonUser.ID] = []
2021-04-21 11:58:56 +02:00
do {
let response = try await context.apiService.suggestionAccountV2(
query: nil,
authenticationBox: authContext.mastodonAuthenticationBox
)
userIDs = response.value.map { $0.account.id }
} catch let error as Mastodon.API.Error where error.httpResponseStatus == .notFound {
let response = try await context.apiService.suggestionAccount(
query: nil,
authenticationBox: authContext.mastodonAuthenticationBox
)
userIDs = response.value.map { $0.id }
2021-04-21 11:58:56 +02:00
} catch {
os_log("%{public}s[%{public}ld], %{public}s: fetch recommendAccountV2 failed. %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription)
2021-04-21 11:58:56 +02:00
}
guard !userIDs.isEmpty else { return }
userFetchedResultsController.userIDs = userIDs
2022-02-16 10:25:55 +01:00
selectedUserFetchedResultsController.userIDs = userIDs
2021-04-21 11:58:56 +02:00
}
2022-02-16 10:25:55 +01:00
// fetch relationship
userFetchedResultsController.$records
.removeDuplicates()
.sink { [weak self] records in
guard let _ = self else { return }
Task {
_ = try await context.apiService.relationship(
records: records,
authenticationBox: authContext.mastodonAuthenticationBox
2022-02-16 10:25:55 +01:00
)
}
}
.store(in: &disposeBag)
2021-04-21 11:58:56 +02:00
}
2021-04-21 08:46:31 +02:00
}