1 /*
2  * Copyright 2019 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 package androidx.paging
18 
19 import androidx.arch.core.util.Function
20 
21 @Suppress("DEPRECATION")
22 internal class WrapperPositionalDataSource<A : Any, B : Any>(
23     private val source: PositionalDataSource<A>,
24     val listFunction: Function<List<A>, List<B>>
25 ) : PositionalDataSource<B>() {
26     override val isInvalid
27         get() = source.isInvalid
28 
addInvalidatedCallbacknull29     override fun addInvalidatedCallback(onInvalidatedCallback: InvalidatedCallback) {
30         source.addInvalidatedCallback(onInvalidatedCallback)
31     }
32 
removeInvalidatedCallbacknull33     override fun removeInvalidatedCallback(onInvalidatedCallback: InvalidatedCallback) {
34         source.removeInvalidatedCallback(onInvalidatedCallback)
35     }
36 
invalidatenull37     override fun invalidate() = source.invalidate()
38 
39     override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<B>) {
40         source.loadInitial(
41             params,
42             object : LoadInitialCallback<A>() {
43                 override fun onResult(data: List<A>, position: Int, totalCount: Int) =
44                     callback.onResult(convert(listFunction, data), position, totalCount)
45 
46                 override fun onResult(data: List<A>, position: Int) =
47                     callback.onResult(convert(listFunction, data), position)
48             }
49         )
50     }
51 
loadRangenull52     override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<B>) {
53         source.loadRange(
54             params,
55             object : LoadRangeCallback<A>() {
56                 override fun onResult(data: List<A>) =
57                     callback.onResult(convert(listFunction, data))
58             }
59         )
60     }
61 }
62