Skip to content

Feat: adds clear function to HitsSource #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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 @@
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 @@

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

Check warning on line 154 in Sources/InstantSearchCore/Hits/HitsInteractor.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/InstantSearchCore/Hits/HitsInteractor.swift#L154

Lines should not have trailing whitespace
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 {

Check warning on line 74 in Sources/InstantSearchSwiftUI/View/HitsList.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/InstantSearchSwiftUI/View/HitsList.swift#L74

Lines should not have trailing whitespace
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 = """

Check warning on line 89 in Sources/InstantSearchSwiftUI/View/HitsList.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/InstantSearchSwiftUI/View/HitsList.swift#L89

Prefer using UTF-8 encoded strings when converting between `String` and `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)

Check failure on line 126 in Sources/InstantSearchSwiftUI/View/HitsList.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/InstantSearchSwiftUI/View/HitsList.swift#L126

Force tries should be avoided

interactor.onResultsUpdated.subscribe(with: hitsController) { (reb, hit) in

Check warning on line 128 in Sources/InstantSearchSwiftUI/View/HitsList.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/InstantSearchSwiftUI/View/HitsList.swift#L128

Unused parameter in a closure should be replaced with _
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)
}

}