1/* 2 * Copyright 2018 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 17package androidx.paging; 18 19import androidx.annotation.NonNull; 20import androidx.annotation.Nullable; 21import androidx.arch.core.util.Function; 22 23import java.util.List; 24 25class WrapperPageKeyedDataSource<K, A, B> extends PageKeyedDataSource<K, B> { 26 private final PageKeyedDataSource<K, A> mSource; 27 private final Function<List<A>, List<B>> mListFunction; 28 29 WrapperPageKeyedDataSource(PageKeyedDataSource<K, A> source, 30 Function<List<A>, List<B>> listFunction) { 31 mSource = source; 32 mListFunction = listFunction; 33 } 34 35 @Override 36 public void addInvalidatedCallback(@NonNull InvalidatedCallback onInvalidatedCallback) { 37 mSource.addInvalidatedCallback(onInvalidatedCallback); 38 } 39 40 @Override 41 public void removeInvalidatedCallback(@NonNull InvalidatedCallback onInvalidatedCallback) { 42 mSource.removeInvalidatedCallback(onInvalidatedCallback); 43 } 44 45 @Override 46 public void invalidate() { 47 mSource.invalidate(); 48 } 49 50 @Override 51 public boolean isInvalid() { 52 return mSource.isInvalid(); 53 } 54 55 @Override 56 public void loadInitial(@NonNull LoadInitialParams<K> params, 57 final @NonNull LoadInitialCallback<K, B> callback) { 58 mSource.loadInitial(params, new LoadInitialCallback<K, A>() { 59 @Override 60 public void onResult(@NonNull List<A> data, int position, int totalCount, 61 @Nullable K previousPageKey, @Nullable K nextPageKey) { 62 callback.onResult(convert(mListFunction, data), position, totalCount, 63 previousPageKey, nextPageKey); 64 } 65 66 @Override 67 public void onResult(@NonNull List<A> data, @Nullable K previousPageKey, 68 @Nullable K nextPageKey) { 69 callback.onResult(convert(mListFunction, data), previousPageKey, nextPageKey); 70 } 71 }); 72 } 73 74 @Override 75 public void loadBefore(@NonNull LoadParams<K> params, 76 final @NonNull LoadCallback<K, B> callback) { 77 mSource.loadBefore(params, new LoadCallback<K, A>() { 78 @Override 79 public void onResult(@NonNull List<A> data, @Nullable K adjacentPageKey) { 80 callback.onResult(convert(mListFunction, data), adjacentPageKey); 81 } 82 }); 83 } 84 85 @Override 86 public void loadAfter(@NonNull LoadParams<K> params, 87 final @NonNull LoadCallback<K, B> callback) { 88 mSource.loadAfter(params, new LoadCallback<K, A>() { 89 @Override 90 public void onResult(@NonNull List<A> data, @Nullable K adjacentPageKey) { 91 callback.onResult(convert(mListFunction, data), adjacentPageKey); 92 } 93 }); 94 } 95} 96