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
|
|
|
|
|
2021-04-21 12:52:09 +02:00
|
|
|
protocol SuggestionAccountViewModelDelegate: AnyObject {
|
2021-04-22 04:11:19 +02:00
|
|
|
var homeTimelineNeedRefresh: PassthroughSubject<Void, Never> { get }
|
2021-04-21 12:52:09 +02:00
|
|
|
}
|
2021-04-22 04:29:53 +02:00
|
|
|
|
2021-04-21 08:46:31 +02:00
|
|
|
final class SuggestionAccountViewModel: NSObject {
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
weak var delegate: SuggestionAccountViewModelDelegate?
|
|
|
|
|
2021-04-21 08:46:31 +02:00
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2022-02-10 12:30:41 +01:00
|
|
|
let userFetchedResultsController: UserFetchedResultsController
|
2022-02-16 10:25:55 +01:00
|
|
|
let selectedUserFetchedResultsController: UserFetchedResultsController
|
2021-04-21 11:58:56 +02:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
var viewWillAppear = PassthroughSubject<Void, Never>()
|
2021-04-23 04:25:08 +02:00
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
// output
|
|
|
|
var collectionViewDiffableDataSource: UICollectionViewDiffableDataSource<SelectedAccountSection, SelectedAccountItem>?
|
|
|
|
var tableViewDiffableDataSource: UITableViewDiffableDataSource<RecommendAccountSection, RecommendAccountItem>?
|
|
|
|
|
|
|
|
init(
|
2022-10-09 14:07:57 +02:00
|
|
|
context: AppContext,
|
|
|
|
authContext: AuthContext
|
2022-02-10 12:30:41 +01:00
|
|
|
) {
|
2021-04-21 08:46:31 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2022-02-10 12:30:41 +01:00
|
|
|
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
|
2021-07-07 11:09:12 +02:00
|
|
|
)
|
2022-02-10 12:30:41 +01:00
|
|
|
super.init()
|
2022-02-16 10:25:55 +01:00
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
userFetchedResultsController.domain = authContext.mastodonAuthenticationBox.domain
|
|
|
|
selectedUserFetchedResultsController.domain = authContext.mastodonAuthenticationBox.domain
|
2022-02-16 10:25:55 +01:00
|
|
|
selectedUserFetchedResultsController.additionalPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [
|
2022-10-09 14:07:57 +02:00
|
|
|
MastodonUser.predicate(followingBy: authContext.mastodonAuthenticationBox.userID),
|
|
|
|
MastodonUser.predicate(followRequestedBy: authContext.mastodonAuthenticationBox.userID)
|
2022-02-16 10:25:55 +01:00
|
|
|
])
|
2022-02-10 12:30:41 +01:00
|
|
|
|
2022-02-16 10:25:55 +01:00
|
|
|
// fetch recomment users
|
2022-02-10 12:30:41 +01:00
|
|
|
Task {
|
|
|
|
var userIDs: [MastodonUser.ID] = []
|
2021-04-21 11:58:56 +02:00
|
|
|
do {
|
2022-02-10 12:30:41 +01:00
|
|
|
let response = try await context.apiService.suggestionAccountV2(
|
|
|
|
query: nil,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-02-10 12:30:41 +01:00
|
|
|
)
|
|
|
|
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,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-02-10 12:30:41 +01:00
|
|
|
)
|
|
|
|
userIDs = response.value.map { $0.id }
|
2021-04-21 11:58:56 +02:00
|
|
|
} catch {
|
2022-02-10 12:30:41 +01:00
|
|
|
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
|
|
|
}
|
2022-02-10 12:30:41 +01: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-10 12:30:41 +01: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,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-02-16 10:25:55 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-21 11:58:56 +02:00
|
|
|
}
|
2022-02-10 12:30:41 +01:00
|
|
|
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|