I'm working on a iOS project require pagination (API call will have a page parameter to pull results). The ideal is when user scroll near the bottom of collectionView, a call to API will happen to fetch more results (increase page number) then add new result to existing array of model.
Here is my implementation that work when user search:
This is PhotoListViewReactor
import RxSwift
import RxCocoa
import ReactorKit
class PhotoListViewReactor : Reactor {
enum Action {
case searchFlickr(_ keyword: String, _ page: Int)
}
enum Mutation {
case flickrList([Photos])
}
struct State{
var keyword : String?
var photos : [Photos] = []
var page: Int = 1
}
var initialState: State = State()
init() { }
func mutate(action: Action) -> Observable<Mutation> {
switch action {
case let .searchFlickr(keyword,page):
return AppService.request(keyword: keyword,page: page)
.catchErrorJustReturn([])
.map{[Photos(photos: $0)]}
.map {Mutation.flickrList($0)}
}
}
func reduce(state: State, mutation: Mutation) -> State {
var newState = state
switch mutation {
case let .flickrList(photos):
newState.photos += photos
newState.page += 1
}
return newState
}
}
The function for get data from API is
static func request(keyword: String, page: Int) -> Observable<[Photo]>
I have added a page: Int to State struct but don't know how to implement page (increase page and call API when user scroll near the bottom)