Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/InstantSearchCore/Hits/HitsInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ public class HitsInteractor<Record: Codable>: AnyHitsInteractor {

infiniteScrollingController.calculatePagesAndLoad(currentRow: rowIndex, offset: pageLoadOffset, pageMap: hitsPageMap)
}


public func clear() {
paginator.clear()
}
}

extension HitsInteractor {
Expand Down
2 changes: 1 addition & 1 deletion Sources/InstantSearchCore/Hits/HitsSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol HitsSource: AnyObject {

func numberOfHits() -> Int
func hit(atIndex index: Int) -> Record?

func clear()
}

extension HitsInteractor: HitsSource {}
3 changes: 3 additions & 0 deletions Sources/InstantSearchCore/Pagination/Paginator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ class Paginator<Item> {
isInvalidated = true
}

public func clear() {
pageMap = nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import SwiftUI
/// HitsController implementation adapted for usage with SwiftUI views
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public class HitsObservableController<Hit: Codable>: ObservableObject, HitsController {

/// List of hits itemsto present
@Published public var hits: [Hit?]
@Published public var hits: [Hit?] {
didSet {
if hits.isEmpty {
hitsSource?.clear()
}
}
}

/// The state ID to assign to the scrollview presenting the hits
@Published public var scrollID: UUID
Expand Down
26 changes: 26 additions & 0 deletions Tests/InstantSearchCoreTests/Unit/Hits/HitsInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,31 @@ class HitsInteractorTests: XCTestCase {
waitForExpectations(timeout: 5)

}

func testClearTriggering() throws {
let paginationController = Paginator<TestRecord<Int>>()
let infiniteScrollingController = TestInfiniteScrollingController()

let hits = (0..<20).map(TestRecord.withValue)
let results = SearchResponse(hits: hits)

let vm = HitsInteractor(
settings: .init(showItemsOnEmptyQuery: true),
paginationController: paginationController,
infiniteScrollingController: infiniteScrollingController
)

let exp = expectation(description: "on results updated")

vm.onResultsUpdated.subscribe(with: self) { (_, _) in
XCTAssertEqual(vm.numberOfHits(), hits.count)
exp.fulfill()
}
vm.update(results)
waitForExpectations(timeout: 3, handler: .none)

vm.clear()
XCTAssertEqual(vm.numberOfHits(), 0)
}

}
10 changes: 10 additions & 0 deletions Tests/InstantSearchCoreTests/Unit/PaginatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,15 @@ class PaginatorTests: XCTestCase {
XCTAssertEqual(paginator.pageMap?.count, 3)

}

func testClear() {
let paginator = Paginator<String>()
let page = TestPageable(index: 0, items: ["i1", "i2", "i3"])
paginator.process(page)
XCTAssertEqual(paginator.pageMap?.count, 3)

paginator.clear()
XCTAssertNil(paginator.pageMap)
}

}
5 changes: 4 additions & 1 deletion Tests/InstantSearchTests/TestHitsSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestHitsSource: HitsSource {

typealias Hit = String

let hits: [String]
var hits: [String]

init(hits: [String]) {
self.hits = hits
Expand All @@ -28,4 +28,7 @@ class TestHitsSource: HitsSource {
return hits[index]
}

func clear() {
hits = []
}
}