Skip to content

Commit

Permalink
Fix an issue with load more not working in search
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jun 2, 2024
1 parent 2e5e5f8 commit fa8f286
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ struct ConsoleSearchResultsListContentView: View {
ForEach(viewModel.results) { result in
let isLast = result.id == viewModel.results.last?.id
ConsoleSearchResultView(viewModel: result, isSeparatorNeeded: !viewModel.parameters.terms.isEmpty && !isLast)
.onAppear {
viewModel.didScroll(to: result)
}
}
if !viewModel.isSearching && viewModel.hasMore {
#if os(iOS) || os(visionOS)
PlainListGroupSeparator()
#endif
Button(action: viewModel.buttonShowMoreResultsTapped) {
Text("Show More Results")
}
if !viewModel.isSearching && !viewModel.hasMore && !viewModel.results.isEmpty {
Text("No more results")
.frame(maxWidth: .infinity, minHeight: 24, alignment: .center)
.font(.footnote)
.foregroundStyle(.secondary)
.listRowSeparator(.hidden, edges: .bottom)
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions Sources/PulseUI/Features/Search/ConsoleSearchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class ConsoleSearchViewModel: ObservableObject, ConsoleSearchOperationDele
if parameters.isEmpty {
return "Search"
} else {
return "\(results.count) results"
return "\(results.count)\(hasMore ? "+" : "") results"
}
}

Expand Down Expand Up @@ -253,7 +253,6 @@ final class ConsoleSearchViewModel: ObservableObject, ConsoleSearchOperationDele

func searchOperationDidFinish(_ operation: ConsoleSearchOperation, hasMore: Bool) {
if operation === self.operation {
self.operation = nil
isSearching = false
if dirtyDate != nil {
self.dirtyDate = nil
Expand Down Expand Up @@ -313,11 +312,6 @@ final class ConsoleSearchViewModel: ObservableObject, ConsoleSearchOperationDele
}
}

func buttonShowMoreResultsTapped() {
isSearching = true
operation?.resume()
}

func buttonShowNewlyAddedSearchResultsTapped() {
refreshNow()
}
Expand All @@ -327,6 +321,17 @@ final class ConsoleSearchViewModel: ObservableObject, ConsoleSearchOperationDele
updateSearchTokens()
}

func didScroll(to result: ConsoleSearchResultViewModel) {
guard results.count > 3 && results[results.endIndex - 2].entity.objectID == result.entity.objectID else {
return
}
guard !isSearching && hasMore else {
return
}
isSearching = true
operation?.resume() // Load more
}

// MARK: Suggested Tokens

private func updateSearchTokens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ConsoleSearchOperation {
private var entities: [NSManagedObject]
private var objectIDs: [NSManagedObjectID]
private var index = 0
private var cutoff = 10
private var cutoff = 12
private let service: ConsoleSearchService
private let context: NSManagedObjectContext
private let lock: os_unfair_lock_t
Expand Down

0 comments on commit fa8f286

Please sign in to comment.