diff --git a/ServiceLayer/Sources/ServiceLayer/Services/SearchService.swift b/ServiceLayer/Sources/ServiceLayer/Services/SearchService.swift index edc4249..0eae36f 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/SearchService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/SearchService.swift @@ -26,7 +26,7 @@ public struct SearchService { return (search.offset == nil ? results : $0.0.appending(results), search.limit) } - .flatMap(contentDatabase.publisher(results:limit:)).eraseToAnyPublisher() + .map(contentDatabase.publisher(results:limit:)).switchToLatest().eraseToAnyPublisher() } } diff --git a/View Controllers/ExploreViewController.swift b/View Controllers/ExploreViewController.swift index 2a61322..784cb71 100644 --- a/View Controllers/ExploreViewController.swift +++ b/View Controllers/ExploreViewController.swift @@ -79,12 +79,9 @@ final class ExploreViewController: UICollectionViewController { } .store(in: &cancellables) - viewModel.searchViewModel.events.sink { [weak self] in - if case let .navigation(navigation) = $0, - case let .searchScope(scope) = navigation { - searchController.searchBar.selectedScopeButtonIndex = scope.rawValue - self?.updateSearchResults(for: searchController) - } + viewModel.searchViewModel.searchScopeChanges.sink { [weak self] in + searchController.searchBar.selectedScopeButtonIndex = $0.rawValue + self?.updateSearchResults(for: searchController) } .store(in: &cancellables) } diff --git a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift index c658daf..09f3aad 100644 --- a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift @@ -17,6 +17,7 @@ public class CollectionItemsViewModel: ObservableObject { private let eventsSubject = PassthroughSubject, Never>() private let loadingSubject = PassthroughSubject() private let expandAllSubject: CurrentValueSubject + private let searchScopeChangesSubject = PassthroughSubject() private var topVisibleIndexPath = IndexPath(item: 0, section: 0) private let lastReadId = CurrentValueSubject(nil) private var lastSelectedLoadMore: LoadMore? @@ -105,6 +106,8 @@ extension CollectionItemsViewModel: CollectionViewModel { .eraseToAnyPublisher() } + public var searchScopeChanges: AnyPublisher { searchScopeChangesSubject.eraseToAnyPublisher() } + public var canRefresh: Bool { collectionService.canRefresh } public func request(maxId: String? = nil, minId: String? = nil, search: Search?) { @@ -154,7 +157,7 @@ extension CollectionItemsViewModel: CollectionViewModel { .navigationService .timelineService(timeline: .tag(tag.name))))) case let .moreResults(moreResults): - send(event: .navigation(.searchScope(moreResults.scope))) + searchScopeChangesSubject.send(moreResults.scope) } } diff --git a/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift index 3931ab6..6528926 100644 --- a/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift @@ -12,6 +12,7 @@ public protocol CollectionViewModel { var alertItems: AnyPublisher { get } var loading: AnyPublisher { get } var events: AnyPublisher { get } + var searchScopeChanges: AnyPublisher { get } var nextPageMaxId: String? { get } var canRefresh: Bool { get } func request(maxId: String?, minId: String?, search: Search?) diff --git a/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift b/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift index 007c3fb..061e248 100644 --- a/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift @@ -129,6 +129,10 @@ extension ProfileViewModel: CollectionViewModel { .eraseToAnyPublisher() } + public var searchScopeChanges: AnyPublisher { + collectionViewModel.flatMap(\.searchScopeChanges).eraseToAnyPublisher() + } + public var nextPageMaxId: String? { collectionViewModel.value.nextPageMaxId } diff --git a/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift b/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift index df8efda..640a3c9 100644 --- a/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift @@ -16,8 +16,9 @@ public final class SearchViewModel: CollectionItemsViewModel { super.init(collectionService: searchService, identityContext: identityContext) - $query.removeDuplicates() + $query.dropFirst() .debounce(for: .seconds(Self.debounceInterval), scheduler: DispatchQueue.global()) + .removeDuplicates() .combineLatest($scope.removeDuplicates()) .sink { [weak self] in self?.request( @@ -39,7 +40,7 @@ public final class SearchViewModel: CollectionItemsViewModel { } private extension SearchViewModel { - static let debounceInterval: TimeInterval = 0.25 + static let debounceInterval: TimeInterval = 0.5 } private extension SearchScope {