From 39cdc013176db4b8e19197e1128ccb4c869f3a2d Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 22 Jul 2021 14:13:49 +0800 Subject: [PATCH] fix: search history not isolate issue --- CoreDataStack/Entity/SearchHistory.swift | 17 +++++++++++++++++ ...SearchHistoryFetchedResultController.swift | 19 +++++++++++++++++++ .../SearchHistoryViewModel.swift | 9 +++++++++ 3 files changed, 45 insertions(+) diff --git a/CoreDataStack/Entity/SearchHistory.swift b/CoreDataStack/Entity/SearchHistory.swift index 37191bbe..3894d1c1 100644 --- a/CoreDataStack/Entity/SearchHistory.swift +++ b/CoreDataStack/Entity/SearchHistory.swift @@ -99,3 +99,20 @@ extension SearchHistory: Managed { return [NSSortDescriptor(keyPath: \SearchHistory.updatedAt, ascending: false)] } } + +extension SearchHistory { + static func predicate(domain: String) -> NSPredicate { + return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.domain), domain) + } + + static func predicate(userID: String) -> NSPredicate { + return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.userID), userID) + } + + public static func predicate(domain: String, userID: String) -> NSPredicate { + return NSCompoundPredicate(andPredicateWithSubpredicates: [ + predicate(domain: domain), + predicate(userID: userID) + ]) + } +} diff --git a/Mastodon/Diffiable/FetchedResultsController/SearchHistoryFetchedResultController.swift b/Mastodon/Diffiable/FetchedResultsController/SearchHistoryFetchedResultController.swift index b83bfe66..6d4461ea 100644 --- a/Mastodon/Diffiable/FetchedResultsController/SearchHistoryFetchedResultController.swift +++ b/Mastodon/Diffiable/FetchedResultsController/SearchHistoryFetchedResultController.swift @@ -17,6 +17,8 @@ final class SearchHistoryFetchedResultController: NSObject { var disposeBag = Set() let fetchedResultsController: NSFetchedResultsController + let domain = CurrentValueSubject(nil) + let userID = CurrentValueSubject(nil) // output let objectIDs = CurrentValueSubject<[NSManagedObjectID], Never>([]) @@ -38,6 +40,23 @@ final class SearchHistoryFetchedResultController: NSObject { super.init() fetchedResultsController.delegate = self + + Publishers.CombineLatest( + self.domain.removeDuplicates(), + self.userID.removeDuplicates() + ) + .receive(on: DispatchQueue.main) + .sink { [weak self] domain, userID in + guard let self = self else { return } + let predicates = [SearchHistory.predicate(domain: domain ?? "", userID: userID ?? "")] + self.fetchedResultsController.fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) + do { + try self.fetchedResultsController.performFetch() + } catch { + assertionFailure(error.localizedDescription) + } + } + .store(in: &disposeBag) } } diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift index d3d41e90..934c55b1 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift @@ -25,6 +25,15 @@ final class SearchHistoryViewModel { self.context = context self.searchHistoryFetchedResultController = SearchHistoryFetchedResultController(managedObjectContext: context.managedObjectContext) + context.authenticationService.activeMastodonAuthenticationBox + .receive(on: DispatchQueue.main) + .sink { [weak self] box in + guard let self = self else { return } + self.searchHistoryFetchedResultController.domain.value = box?.domain + self.searchHistoryFetchedResultController.userID.value = box?.userID + } + .store(in: &disposeBag) + // may block main queue by large dataset searchHistoryFetchedResultController.objectIDs .removeDuplicates()