 import RxCocoa
 
 class PaginationViewModel<Element: Decodable, Filter: FilterType where Element.DecodedType == Element> {
-    
+
     var paginationRequest: PaginationRequest<Element, Filter>
     typealias LoadingType = (Bool, String)
-    
+
     let refreshTrigger = PublishSubject<Bool>()
     let loadNextPageTrigger = PublishSubject<Void>()
     let queryTrigger = PublishSubject<String>()
     let filterTrigger = PublishSubject<Filter>()
-
+    let networkErrorTrigger = PublishSubject<NetworkError>()
     let hasNextPage = Variable<Bool>(false)
     let fullloading = Variable<LoadingType>((false, "1"))
     let elements = Variable<[Element]>([])
-    
+
     private var disposeBag = DisposeBag()
     private let queryDisposeBag = DisposeBag()
-    
+
     init(route: RequestType, page: String = "1", query: String = "", filter: Filter? = nil) {
         paginationRequest = PaginationRequest(route: route, page: page, query: query, filter: filter)
         bindPaginationRequest(paginationRequest, nextPage: nil)
         setUpForceRefresh()
     }
-    
+
     private func setUpForceRefresh() {
-        
+
         queryTrigger.skip(1)
             .throttle(0.25, scheduler: MainScheduler.instance)
             .distinctUntilChanged()

}

class SimplePaginationViewModel<Element: Decodable where Element.DecodedType == Element> : PaginationViewModel<Element, EmptyFilter> {
    
    init(route: RequestType, page: String = "1", query: String = "") {
        super.init(route: route, page: page, query: query, filter: nil)
    }
}

extension PaginationViewModel {
    
    var loading: Driver<Bool> {
        return fullloading.asDriver().skip(1).map { $0.0 }.distinctUntilChanged()
    }
    
    var firstPageLoading: Driver<Bool> {
        return fullloading.asDriver().filter { $0.1 == "1" }.map { $0.0 }
    }
    
    var emptyState: Driver<Bool> {
        return Driver.combineLatest(loading, elements.asDriver().skip(1)) { (isLoading, elements) -> Bool in
            return !isLoading && elements.isEmpty
        }
        .distinctUntilChanged()
    }
    
}

            .merge()
            .bindTo(fullloading)
            .addDisposableTo(disposeBag)

        Observable
            .combineLatest(elements.asObservable(), response) { elements, response in
                return response.hasPreviousPage ? elements + response.elements : response.elements
