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 WrapperPageKeyedDataSource<K : Any, A : Any, B : Any>( 23 private val source: PageKeyedDataSource<K, A>, 24 private val listFunction: Function<List<A>, List<B>> 25 ) : PageKeyedDataSource<K, 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<K>, callback: LoadInitialCallback<K, B>) { 40 source.loadInitial( 41 params, 42 object : LoadInitialCallback<K, A>() { 43 override fun onResult( 44 data: List<A>, 45 position: Int, 46 totalCount: Int, 47 previousPageKey: K?, 48 nextPageKey: K? 49 ) { 50 val convertedData = convert(listFunction, data) 51 callback.onResult( 52 convertedData, 53 position, 54 totalCount, 55 previousPageKey, 56 nextPageKey 57 ) 58 } 59 60 override fun onResult(data: List<A>, previousPageKey: K?, nextPageKey: K?) { 61 val convertedData = convert(listFunction, data) 62 callback.onResult(convertedData, previousPageKey, nextPageKey) 63 } 64 } 65 ) 66 } 67 loadBeforenull68 override fun loadBefore(params: LoadParams<K>, callback: LoadCallback<K, B>) { 69 source.loadBefore( 70 params, 71 object : LoadCallback<K, A>() { 72 override fun onResult(data: List<A>, adjacentPageKey: K?) = 73 callback.onResult(convert(listFunction, data), adjacentPageKey) 74 } 75 ) 76 } 77 loadAfternull78 override fun loadAfter(params: LoadParams<K>, callback: LoadCallback<K, B>) { 79 source.loadAfter( 80 params, 81 object : LoadCallback<K, A>() { 82 override fun onResult(data: List<A>, adjacentPageKey: K?) = 83 callback.onResult(convert(listFunction, data), adjacentPageKey) 84 } 85 ) 86 } 87 } 88