2021-07-14 14:28:41 +02:00
|
|
|
//
|
|
|
|
// SearchResultViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-14.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
2021-07-15 14:28:36 +02:00
|
|
|
import CommonOSLog
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonSDK
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
final class SearchResultViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
|
|
|
let searchScope: SearchDetailViewModel.SearchScope
|
|
|
|
let searchText = CurrentValueSubject<String, Never>("")
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published var hashtags: [Mastodon.Entity.Tag] = []
|
|
|
|
let userFetchedResultsController: UserFetchedResultsController
|
2021-07-14 14:28:41 +02:00
|
|
|
let statusFetchedResultsController: StatusFetchedResultsController
|
2022-01-27 14:23:39 +01:00
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
let viewDidAppear = CurrentValueSubject<Bool, Never>(false)
|
2021-07-15 09:49:30 +02:00
|
|
|
var cellFrameCache = NSCache<NSNumber, NSValue>()
|
|
|
|
var navigationBarFrame = CurrentValueSubject<CGRect, Never>(.zero)
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<SearchResultSection, SearchResultItem>!
|
|
|
|
@Published var items: [SearchResultItem] = []
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Loading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
|
|
|
State.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(State.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
2021-07-14 14:40:27 +02:00
|
|
|
let didDataSourceUpdate = PassthroughSubject<Void, Never>()
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
init(context: AppContext, searchScope: SearchDetailViewModel.SearchScope) {
|
|
|
|
self.context = context
|
|
|
|
self.searchScope = searchScope
|
2022-01-27 14:23:39 +01:00
|
|
|
self.userFetchedResultsController = UserFetchedResultsController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
domain: nil,
|
2022-02-16 10:25:55 +01:00
|
|
|
additionalPredicate: nil
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
2021-07-14 14:28:41 +02:00
|
|
|
self.statusFetchedResultsController = StatusFetchedResultsController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
domain: nil,
|
|
|
|
additionalTweetPredicate: nil
|
|
|
|
)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
context.authenticationService.activeMastodonAuthenticationBox
|
|
|
|
.map { $0?.domain }
|
2022-02-10 12:30:41 +01:00
|
|
|
.assign(to: \.domain, on: userFetchedResultsController)
|
2022-01-27 14:23:39 +01:00
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
context.authenticationService.activeMastodonAuthenticationBox
|
|
|
|
.map { $0?.domain }
|
|
|
|
.assign(to: \.value, on: statusFetchedResultsController.domain)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// Publishers.CombineLatest(
|
|
|
|
// items,
|
|
|
|
// statusFetchedResultsController.objectIDs.removeDuplicates()
|
|
|
|
// )
|
|
|
|
// .receive(on: DispatchQueue.main)
|
|
|
|
// .sink { [weak self] items, statusObjectIDs in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// guard let diffableDataSource = self.diffableDataSource else { return }
|
|
|
|
//
|
|
|
|
// var snapshot = NSDiffableDataSourceSnapshot<SearchResultSection, SearchResultItem>()
|
|
|
|
// snapshot.appendSections([.main])
|
|
|
|
//
|
|
|
|
// // append account & hashtag items
|
|
|
|
//
|
|
|
|
// var items = items
|
|
|
|
// if self.searchScope == .all {
|
|
|
|
// // all search scope not paging. it's safe sort on whole dataset
|
|
|
|
// items.sort(by: { ($0.sortKey ?? "") < ($1.sortKey ?? "")})
|
|
|
|
// }
|
|
|
|
// snapshot.appendItems(items, toSection: .main)
|
|
|
|
//
|
|
|
|
// var oldSnapshotAttributeDict: [NSManagedObjectID : Item.StatusAttribute] = [:]
|
|
|
|
// let oldSnapshot = diffableDataSource.snapshot()
|
|
|
|
// for item in oldSnapshot.itemIdentifiers {
|
|
|
|
// guard case let .status(objectID, attribute) = item else { continue }
|
|
|
|
// oldSnapshotAttributeDict[objectID] = attribute
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // append statuses
|
|
|
|
// var statusItems: [SearchResultItem] = []
|
|
|
|
// for objectID in statusObjectIDs {
|
|
|
|
// let attribute = oldSnapshotAttributeDict[objectID] ?? Item.StatusAttribute()
|
|
|
|
// statusItems.append(.status(statusObjectID: objectID, attribute: attribute))
|
|
|
|
// }
|
|
|
|
// snapshot.appendItems(statusItems, toSection: .main)
|
|
|
|
//
|
|
|
|
// if let currentState = self.stateMachine.currentState {
|
|
|
|
// switch currentState {
|
|
|
|
// case is State.Loading, is State.Fail, is State.Idle:
|
|
|
|
// let attribute = SearchResultItem.BottomLoaderAttribute(isEmptyResult: false)
|
|
|
|
// snapshot.appendItems([.bottomLoader(attribute: attribute)], toSection: .main)
|
|
|
|
// case is State.Fail:
|
|
|
|
// break
|
|
|
|
// case is State.NoMore:
|
|
|
|
// if snapshot.itemIdentifiers.isEmpty {
|
|
|
|
// let attribute = SearchResultItem.BottomLoaderAttribute(isEmptyResult: true)
|
|
|
|
// snapshot.appendItems([.bottomLoader(attribute: attribute)], toSection: .main)
|
|
|
|
// }
|
|
|
|
// default:
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// diffableDataSource.defaultRowAnimation = .fade
|
|
|
|
// diffableDataSource.apply(snapshot, animatingDifferences: true) { [weak self] in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// self.didDataSourceUpdate.send()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// .store(in: &disposeBag)
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
extension SearchResultViewModel {
|
|
|
|
func persistSearchHistory(for item: SearchResultItem) {
|
2022-01-27 14:23:39 +01:00
|
|
|
fatalError()
|
|
|
|
// guard let box = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
// let property = SearchHistory.Property(domain: box.domain, userID: box.userID)
|
|
|
|
// let domain = box.domain
|
|
|
|
//
|
|
|
|
// switch item {
|
|
|
|
// case .account(let entity):
|
|
|
|
// let managedObjectContext = context.backgroundManagedObjectContext
|
|
|
|
// managedObjectContext.performChanges {
|
|
|
|
// let (user, _) = APIService.CoreData.createOrMergeMastodonUser(
|
|
|
|
// into: managedObjectContext,
|
|
|
|
// for: nil,
|
|
|
|
// in: domain,
|
|
|
|
// entity: entity,
|
|
|
|
// userCache: nil,
|
|
|
|
// networkDate: Date(),
|
|
|
|
// log: OSLog.api
|
|
|
|
// )
|
|
|
|
// if let searchHistory = user.findSearchHistory(domain: box.domain, userID: box.userID) {
|
|
|
|
// searchHistory.update(updatedAt: Date())
|
|
|
|
// } else {
|
|
|
|
// SearchHistory.insert(into: managedObjectContext, property: property, account: user)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .sink { result in
|
|
|
|
// switch result {
|
|
|
|
// case .failure(let error):
|
|
|
|
// assertionFailure(error.localizedDescription)
|
|
|
|
// case .success:
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .store(in: &context.disposeBag)
|
|
|
|
//
|
|
|
|
// case .hashtag(let entity):
|
|
|
|
// let managedObjectContext = context.backgroundManagedObjectContext
|
|
|
|
// var tag: Tag?
|
|
|
|
// managedObjectContext.performChanges {
|
|
|
|
// let (hashtag, _) = APIService.CoreData.createOrMergeTag(
|
|
|
|
// into: managedObjectContext,
|
|
|
|
// entity: entity
|
|
|
|
// )
|
|
|
|
// tag = hashtag
|
|
|
|
// if let searchHistory = hashtag.findSearchHistory(domain: box.domain, userID: box.userID) {
|
|
|
|
// searchHistory.update(updatedAt: Date())
|
|
|
|
// } else {
|
|
|
|
// _ = SearchHistory.insert(into: managedObjectContext, property: property, hashtag: hashtag)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .sink { result in
|
|
|
|
// switch result {
|
|
|
|
// case .failure(let error):
|
|
|
|
// assertionFailure(error.localizedDescription)
|
|
|
|
// case .success:
|
|
|
|
// print(tag?.searchHistories)
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .store(in: &context.disposeBag)
|
|
|
|
//
|
|
|
|
// case .status:
|
|
|
|
// // FIXME:
|
|
|
|
// break
|
|
|
|
// case .bottomLoader:
|
|
|
|
// break
|
|
|
|
// }
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
|
|
|
}
|