Skip to content
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

Refresh And Paginated Search Examples #5

Open
kemalturk opened this issue Feb 13, 2021 · 0 comments
Open

Refresh And Paginated Search Examples #5

kemalturk opened this issue Feb 13, 2021 · 0 comments

Comments

@kemalturk
Copy link

We need refresh and paginated search examples. I made a solution like this. But if there is more compact and convenient solution please share with us.

`
typealias Page = PaginatedResponseData

let steps = PublishRelay<Step>()

private let disposeBag = DisposeBag()
private var disposable: Disposable?

private let service = UpSocialExplorePeopleSearchService()

let data = BehaviorRelay<[UpSocialExplorePeopleSearchItem]>(value: [])
var items: [UpSocialExplorePeopleSearchItem] {
    data.value
}

private(set) var pager: Pager<Page>?

private func createPager(_ searchText: String) -> Pager<Page> {
    let pager = Pager<Page>(make: {[weak self] previousPage in
        guard let self = self else {
            return Observable.just(Page(currentPage: 0, lastPage: 1, nextPageUrl: nil, data: []))
        }

        return self.service.browse(searchText: searchText, page: (previousPage?.currentPage ?? 0) + 1).asObservable()
        .catch { error in
            print(error)
            return Observable.just(Page(currentPage: 0, lastPage: 1, nextPageUrl: nil, data: []))
        }
    }, while: { page in
        page.currentPage < page.lastPage
    })
    return pager
}

func browse(searchText: String) {

    data.accept([])

    disposable?.dispose()

    pager = createPager(searchText)

    disposable = pager?.page
    .withUnretained(self)
    .debug("Page")
    .bind { vm, page in
        vm.data.accept(vm.data.value + page.data)
    }

    disposable?.disposed(by: disposeBag)

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant