1 /*
<lambda>null2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 @file:Suppress("unused")
18 
19 package androidx.paging.samples
20 
21 import androidx.annotation.Sampled
22 import androidx.paging.PagingState
23 import androidx.paging.rxjava2.RxPagingSource
24 import io.reactivex.Single
25 import io.reactivex.schedulers.Schedulers
26 import java.io.IOException
27 import retrofit2.HttpException
28 
29 private class RxBackendService {
30     data class RemoteResult(val items: List<Item>, val prev: String, val next: String)
31 
32     @Suppress("UNUSED_PARAMETER")
33     fun searchUsers(searchTerm: String, pageKey: String?): Single<RemoteResult> {
34         throw NotImplementedError()
35     }
36 }
37 
38 @Sampled
rxPagingSourceSamplenull39 fun rxPagingSourceSample() {
40     /**
41      * Sample RxPagingSource which loads `Item`s from network requests via Retrofit to a backend
42      * service, which uses String tokens to load pages (each response has a next/previous token).
43      */
44     class MyPagingSource(val myBackend: RxBackendService, val searchTerm: String) :
45         RxPagingSource<String, Item>() {
46         override fun loadSingle(params: LoadParams<String>): Single<LoadResult<String, Item>> {
47             return myBackend
48                 // Single-based network load
49                 .searchUsers(searchTerm, params.key)
50                 .subscribeOn(Schedulers.io())
51                 .map<LoadResult<String, Item>> { result ->
52                     LoadResult.Page(
53                         data = result.items,
54                         prevKey = result.prev,
55                         nextKey = result.next
56                     )
57                 }
58                 .onErrorReturn { e ->
59                     when (e) {
60                         // Retrofit calls that return the body type throw either IOException for
61                         // network failures, or HttpException for any non-2xx HTTP status codes.
62                         // This code reports all errors to the UI, but you can inspect/wrap the
63                         // exceptions to provide more context.
64                         is IOException -> LoadResult.Error(e)
65                         is HttpException -> LoadResult.Error(e)
66                         else -> throw e
67                     }
68                 }
69         }
70 
71         override fun getRefreshKey(state: PagingState<String, Item>): String? {
72             return state.anchorPosition?.let { state.closestItemToPosition(it)?.id }
73         }
74     }
75 }
76