Skip to content

Commit

Permalink
Merge pull request #12 from Lickability/kpa/fix-timing-bug
Browse files Browse the repository at this point in the history
Fixing search text timing bug
  • Loading branch information
Pearapps authored Mar 7, 2023
2 parents 8fe840b + d203239 commit 0d06df8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Example/Photos/PhotoListViewStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class PhotoListViewStore: ViewStore {
let status: Status
let showsPhotoCount: Bool
let navigationTitle: LocalizedStringKey
fileprivate let searchText: String
let searchText: String

init(status: PhotoListViewStore.ViewState.Status = .loading, showsPhotoCount: Bool = false, navigationTitle: LocalizedStringKey = ViewState.defaultNavigationTitle, searchText: String = "") {
self.status = status
Expand Down Expand Up @@ -62,17 +62,19 @@ final class PhotoListViewStore: ViewStore {
self.provider = provider
let showsPhotosCountPublisher = self.showsPhotosCountPublisher.prepend(ViewState.initial.showsPhotoCount)
let photoPublisher = provider.providePhotos().prepend([])
let searchTextPublisher = self.searchTextPublisher.debounce(for: .seconds(1), scheduler: scheduler).prepend(ViewState.initial.searchText)
let searchTextUIPublisher = self.searchTextPublisher.prepend(ViewState.initial.searchText)
let searchTextPublisher = searchTextUIPublisher.throttle(for: 1, scheduler: scheduler, latest: true)

photoPublisher
.combineLatest(showsPhotosCountPublisher, searchTextPublisher)
.map { (result: Result<[Photo], ProviderError>, showsPhotosCount: Bool, searchText: String) in
.combineLatest(showsPhotosCountPublisher, searchTextPublisher, searchTextUIPublisher)
.map { (result: Result<[Photo], ProviderError>, showsPhotosCount: Bool, searchText: String, searchTextUI: String) in
switch result {
case let .success(photos):
let filteredPhotos = photos.filter(searchText: searchText)
let navigationTitle = showsPhotosCount ? LocalizedStringKey("Photos \(filteredPhotos.count)") : ViewState.defaultNavigationTitle
return ViewState(status: .content(filteredPhotos), showsPhotoCount: showsPhotosCount, navigationTitle: navigationTitle, searchText: searchText)
return ViewState(status: .content(filteredPhotos), showsPhotoCount: showsPhotosCount, navigationTitle: navigationTitle, searchText: searchTextUI)
case let .failure(error):
return ViewState(status: .error(error), showsPhotoCount: false, navigationTitle: ViewState.defaultNavigationTitle, searchText: searchText)
return ViewState(status: .error(error), showsPhotoCount: false, navigationTitle: ViewState.defaultNavigationTitle, searchText: searchTextUI)
}
}
.receive(on: scheduler)
Expand Down
9 changes: 9 additions & 0 deletions ExampleTests/ViewStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ final class ViewStoreTests: XCTestCase {
XCTAssertTrue(photos[0].title.contains("2"))
}
}

func testSearchProperlyFiltersAndSearchTextIsCorrect() {
let mock = MockItemProvider(photosCount: 3)

let vs = PhotoListViewStore(provider: mock, scheduler: MainQueueScheduler(type: .synchronous))
vs.send(.search("2"))

XCTAssertEqual(vs.viewState.searchText, "2")
}
}

0 comments on commit 0d06df8

Please sign in to comment.