Improve search

This commit is contained in:
Justin Mazzocchi 2021-03-04 22:16:38 -08:00
parent b148930801
commit 27213a78f4
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
4 changed files with 14 additions and 1 deletions

View file

@ -24,6 +24,7 @@ public class CollectionItemsViewModel: ObservableObject {
private var localLastReadId: CollectionItem.Id? private var localLastReadId: CollectionItem.Id?
private var markerLastReadId: CollectionItem.Id? private var markerLastReadId: CollectionItem.Id?
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var requestCancellables = Set<AnyCancellable>()
// swiftlint:disable:next function_body_length // swiftlint:disable:next function_body_length
public init(collectionService: CollectionService, identityContext: IdentityContext) { public init(collectionService: CollectionService, identityContext: IdentityContext) {
@ -247,13 +248,19 @@ extension CollectionItemsViewModel: CollectionViewModel {
receiveSubscription: { [weak self] _ in self?.loadingSubject.send(true) }, receiveSubscription: { [weak self] _ in self?.loadingSubject.send(true) },
receiveCompletion: { [weak self] _ in self?.loadingSubject.send(false) }) receiveCompletion: { [weak self] _ in self?.loadingSubject.send(false) })
.sink { _ in } .sink { _ in }
.store(in: &cancellables) .store(in: &requestCancellables)
collectionService.requestMarkerLastReadId() collectionService.requestMarkerLastReadId()
.sink { _ in } receiveValue: { [weak self] in self?.markerLastReadId = $0 } .sink { _ in } receiveValue: { [weak self] in self?.markerLastReadId = $0 }
.store(in: &cancellables) .store(in: &cancellables)
} }
public func cancelRequests() {
for cancellable in requestCancellables {
cancellable.cancel()
}
}
public func select(indexPath: IndexPath) { public func select(indexPath: IndexPath) {
let item = lastUpdate.sections[indexPath.section].items[indexPath.item] let item = lastUpdate.sections[indexPath.section].items[indexPath.item]

View file

@ -18,6 +18,7 @@ public protocol CollectionViewModel {
var announcesNewItems: Bool { get } var announcesNewItems: Bool { get }
func request(maxId: String?, minId: String?, search: Search?) func request(maxId: String?, minId: String?, search: Search?)
func requestNextPage(fromIndexPath indexPath: IndexPath) func requestNextPage(fromIndexPath indexPath: IndexPath)
func cancelRequests()
func viewedAtTop(indexPath: IndexPath) func viewedAtTop(indexPath: IndexPath)
func select(indexPath: IndexPath) func select(indexPath: IndexPath)
func canSelect(indexPath: IndexPath) -> Bool func canSelect(indexPath: IndexPath) -> Bool

View file

@ -158,6 +158,10 @@ extension ProfileViewModel: CollectionViewModel {
collectionViewModel.value.request(maxId: maxId, minId: minId, search: nil) collectionViewModel.value.request(maxId: maxId, minId: minId, search: nil)
} }
public func cancelRequests() {
collectionViewModel.value.cancelRequests()
}
public func requestNextPage(fromIndexPath indexPath: IndexPath) { public func requestNextPage(fromIndexPath indexPath: IndexPath) {
collectionViewModel.value.requestNextPage(fromIndexPath: indexPath) collectionViewModel.value.requestNextPage(fromIndexPath: indexPath)
} }

View file

@ -21,6 +21,7 @@ public final class SearchViewModel: CollectionItemsViewModel {
.removeDuplicates() .removeDuplicates()
.combineLatest($scope.removeDuplicates()) .combineLatest($scope.removeDuplicates())
.sink { [weak self] in .sink { [weak self] in
self?.cancelRequests()
self?.request( self?.request(
maxId: nil, maxId: nil,
minId: nil, minId: nil,