2021-07-15 14:28:36 +02:00
|
|
|
//
|
|
|
|
// SearchHistoryViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-15.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
|
|
|
import CommonOSLog
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-07-15 14:28:36 +02:00
|
|
|
|
2023-05-08 15:24:01 +02:00
|
|
|
final class SearchHistoryViewModel: FollowedBlockedUserIdProviding {
|
2021-07-15 14:28:36 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2021-07-15 14:28:36 +02:00
|
|
|
let searchHistoryFetchedResultController: SearchHistoryFetchedResultController
|
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UICollectionViewDiffableDataSource<SearchHistorySection, SearchHistoryItem>?
|
2023-05-08 15:24:01 +02:00
|
|
|
var followedUserIds = CurrentValueSubject<[String], Never>([])
|
|
|
|
var blockedUserIds = CurrentValueSubject<[String], Never>([])
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext) {
|
2021-07-15 14:28:36 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2021-07-15 14:28:36 +02:00
|
|
|
self.searchHistoryFetchedResultController = SearchHistoryFetchedResultController(managedObjectContext: context.managedObjectContext)
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
searchHistoryFetchedResultController.domain.value = authContext.mastodonAuthenticationBox.domain
|
|
|
|
searchHistoryFetchedResultController.userID.value = authContext.mastodonAuthenticationBox.userID
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
//extension SearchHistoryViewModel {
|
|
|
|
// func persistSearchHistory(for item: SearchHistoryItem) {
|
|
|
|
// guard let box = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
// let property = SearchHistory.Property(domain: box.domain, userID: box.userID)
|
|
|
|
//
|
|
|
|
// switch item {
|
|
|
|
// case .account(let objectID):
|
|
|
|
// let managedObjectContext = context.backgroundManagedObjectContext
|
|
|
|
// managedObjectContext.performChanges {
|
|
|
|
// guard let user = try? managedObjectContext.existingObject(with: objectID) as? MastodonUser else { return }
|
|
|
|
// 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 objectID):
|
|
|
|
// let managedObjectContext = context.backgroundManagedObjectContext
|
|
|
|
// managedObjectContext.performChanges {
|
|
|
|
// guard let hashtag = try? managedObjectContext.existingObject(with: objectID) as? Tag else { return }
|
|
|
|
// 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:
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .store(in: &context.disposeBag)
|
|
|
|
//
|
|
|
|
// case .status:
|
|
|
|
// // FIXME:
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func clearSearchHistory() {
|
|
|
|
// let managedObjectContext = context.backgroundManagedObjectContext
|
|
|
|
// managedObjectContext.performChanges {
|
|
|
|
// let request = SearchHistory.sortedFetchRequest
|
|
|
|
// let searchHistories = managedObjectContext.safeFetch(request)
|
|
|
|
// for searchHistory in searchHistories {
|
|
|
|
// managedObjectContext.delete(searchHistory)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .sink { result in
|
|
|
|
// // do nothing
|
|
|
|
// }
|
|
|
|
// .store(in: &context.disposeBag)
|
|
|
|
// }
|
|
|
|
//}
|