From 27213a78f4165efc324b1e86f33635a5460dffa6 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Thu, 4 Mar 2021 22:16:38 -0800 Subject: [PATCH] Improve search --- .../View Models/CollectionItemsViewModel.swift | 9 ++++++++- .../ViewModels/View Models/CollectionViewModel.swift | 1 + .../ViewModels/View Models/ProfileViewModel.swift | 4 ++++ .../Sources/ViewModels/View Models/SearchViewModel.swift | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift index 38682a6..ea1c2cf 100644 --- a/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CollectionItemsViewModel.swift @@ -24,6 +24,7 @@ public class CollectionItemsViewModel: ObservableObject { private var localLastReadId: CollectionItem.Id? private var markerLastReadId: CollectionItem.Id? private var cancellables = Set() + private var requestCancellables = Set() // swiftlint:disable:next function_body_length public init(collectionService: CollectionService, identityContext: IdentityContext) { @@ -247,13 +248,19 @@ extension CollectionItemsViewModel: CollectionViewModel { receiveSubscription: { [weak self] _ in self?.loadingSubject.send(true) }, receiveCompletion: { [weak self] _ in self?.loadingSubject.send(false) }) .sink { _ in } - .store(in: &cancellables) + .store(in: &requestCancellables) collectionService.requestMarkerLastReadId() .sink { _ in } receiveValue: { [weak self] in self?.markerLastReadId = $0 } .store(in: &cancellables) } + public func cancelRequests() { + for cancellable in requestCancellables { + cancellable.cancel() + } + } + public func select(indexPath: IndexPath) { let item = lastUpdate.sections[indexPath.section].items[indexPath.item] diff --git a/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift index 6b3f7a6..9ca62bd 100644 --- a/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CollectionViewModel.swift @@ -18,6 +18,7 @@ public protocol CollectionViewModel { var announcesNewItems: Bool { get } func request(maxId: String?, minId: String?, search: Search?) func requestNextPage(fromIndexPath indexPath: IndexPath) + func cancelRequests() func viewedAtTop(indexPath: IndexPath) func select(indexPath: IndexPath) func canSelect(indexPath: IndexPath) -> Bool diff --git a/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift b/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift index c9a4fbd..8b116c2 100644 --- a/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/ProfileViewModel.swift @@ -158,6 +158,10 @@ extension ProfileViewModel: CollectionViewModel { collectionViewModel.value.request(maxId: maxId, minId: minId, search: nil) } + public func cancelRequests() { + collectionViewModel.value.cancelRequests() + } + public func requestNextPage(fromIndexPath indexPath: IndexPath) { collectionViewModel.value.requestNextPage(fromIndexPath: indexPath) } diff --git a/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift b/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift index 2c7c161..cbd5e9d 100644 --- a/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/SearchViewModel.swift @@ -21,6 +21,7 @@ public final class SearchViewModel: CollectionItemsViewModel { .removeDuplicates() .combineLatest($scope.removeDuplicates()) .sink { [weak self] in + self?.cancelRequests() self?.request( maxId: nil, minId: nil,