Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions Sources/InstantSearchCore/Hits/AnyHitsInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public protocol AnyHitsInteractor: AnyObject {

func notifyQueryChanged()
func process(_ error: Swift.Error, for query: Query)

func clear()
}
5 changes: 5 additions & 0 deletions Sources/InstantSearchCore/Hits/HitsInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class HitsInteractor<Record: Codable>: AnyHitsInteractor {
guard let hitsPageMap = paginator.pageMap, !paginator.isInvalidated else { return 0 }

if isLastQueryEmpty && !settings.showItemsOnEmptyQuery {
clear()
return 0
} else {
return hitsPageMap.count
Expand Down Expand Up @@ -150,6 +151,10 @@ public class HitsInteractor<Record: Codable>: AnyHitsInteractor {

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

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

public extension HitsInteractor {
Expand Down
4 changes: 4 additions & 0 deletions Sources/InstantSearchCore/Pagination/Paginator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ class Paginator<Item> {
public func invalidate() {
isInvalidated = true
}

public func clear() {
pageMap = nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import InstantSearchTelemetry
/// 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?]

/// List of hits items to present
@Published private(set) public var hits: [Hit?]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Published private(set) public var hits: [Hit?]
@Published public var hits: [Hit?]

This field is set from the InstantSearchCore module, so it cannot be declared as private(set) without compilation failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have any compilation failure targeting macOS and iOS. Am I missing something?


/// The state ID to assign to the scrollview presenting the hits
@Published public var scrollID: UUID
@Published private(set) public var scrollID: UUID

public var hitsSource: HitsInteractor<Hit>?

Expand Down
55 changes: 49 additions & 6 deletions Sources/InstantSearchSwiftUI/View/HitsList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,46 @@
#if os(iOS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing this conditional compiling clause, you make it fail while compiling for macOS. So, the test step fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad. Fixed

@available(iOS 13.0, tvOS 13.0, watchOS 7.0, *)
struct HitsView_Previews: PreviewProvider {

struct PreviewRecord<Value: Codable>: Codable {
let objectID: ObjectID
let value: Value

init(_ value: Value, objectID: ObjectID = ObjectID(rawValue: UUID().uuidString)) {
self.value = value
self.objectID = objectID
}

static func withValue(_ value: Value) -> Self {
.init(value)
}
}

static let rawHits: Data = """
{
"hits": [
{
"objectID": "1",
"value": "h1"
},
{
"objectID": "2",
"value": "h2"
}
]
}
""".data(using: .utf8)!

static let hitsController: HitsObservableController<PreviewRecord<String>> = .init()

static let interactor = HitsInteractor<PreviewRecord<String>>(infiniteScrolling: .off, showItemsOnEmptyQuery: true)

static var previews: some View {
let hitsController: HitsObservableController<String> = .init()
NavigationView {
HitsList(hitsController) { string, _ in
VStack {
HStack {
Text(string ?? "---")
Text(string?.value ?? "---")
.frame(maxWidth: .infinity, minHeight: 30, maxHeight: .infinity, alignment: .leading)
.padding(.horizontal, 16)
}
Expand All @@ -88,20 +121,30 @@
}
.padding(.top, 20)
.onAppear {
hitsController.hits = ["One", "Two", "Three"]
}.navigationBarTitle("Hits")
hitsController.hitsSource = interactor

let results = try! JSONDecoder().decode(SearchResponse.self, from: rawHits)

interactor.onResultsUpdated.subscribe(with: hitsController) { (reb, hit) in
reb.reload()
}
interactor.update(results)
}
.navigationBarTitle("Hits")
}

NavigationView {
HitsList(hitsController) { string, _ in
VStack {
HStack {
Text(string ?? "---")
Text(string?.value ?? "---")
}
Divider()
}
} noResults: {
Text("No results")
}.navigationBarTitle("Hits")
}
.navigationBarTitle("Hits")
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion Tests/InstantSearchCoreTests/Unit/Hits/HitsInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,35 @@ class HitsInteractorTests: XCTestCase {
XCTAssertEqual(hitsInteractor.hit(atIndex: 1), Person(firstName: "Helen", lastName: "Smith"))
exp.fulfill()
}

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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,9 @@ class MultiIndexHitsInteractorTests: XCTestCase {
func loadMoreResults() {
didCallLoadMoreResults()
}

func clear() {

}
}
}
11 changes: 11 additions & 0 deletions Tests/InstantSearchCoreTests/Unit/PaginatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ class PaginatorTests: XCTestCase {
XCTAssertNotNil(paginator.pageMap)
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)
}

}