All Downloads are FREE. Search and download functionalities are using the official Maven repository.

android.arch.paging.TiledDataSource Maven / Gradle / Ivy

/*
 * Copyright 2016-2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.arch.paging;

import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;

import java.util.Collections;
import java.util.List;

// NOTE: Room 1.0 depends on this class, so it should not be removed until
// we can require a version of Room that uses PositionalDataSource directly
/**
 * @param  Type loaded by the TiledDataSource.
 *
 * @deprecated Use {@link PositionalDataSource}
 * @hide
 */
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public abstract class TiledDataSource extends PositionalDataSource {

    @WorkerThread
    public abstract int countItems();

    @Override
    boolean isContiguous() {
        return false;
    }

    @WorkerThread
    public abstract List loadRange(int startPosition, int count);

    @Override
    public void loadInitial(@NonNull LoadInitialParams params,
            @NonNull LoadInitialCallback callback) {
        int totalCount = countItems();
        if (totalCount == 0) {
            callback.onResult(Collections.emptyList(), 0, 0);
            return;
        }

        // bound the size requested, based on known count
        final int firstLoadPosition = computeInitialLoadPosition(params, totalCount);
        final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, totalCount);

        // convert from legacy behavior
        List list = loadRange(firstLoadPosition, firstLoadSize);
        if (list != null && list.size() == firstLoadSize) {
            callback.onResult(list, firstLoadPosition, totalCount);
        } else {
            // null list, or size doesn't match request
            // The size check is a WAR for Room 1.0, subsequent versions do the check in Room
            invalidate();
        }
    }

    @Override
    public void loadRange(@NonNull LoadRangeParams params,
            @NonNull LoadRangeCallback callback) {
        List list = loadRange(params.startPosition, params.loadSize);
        if (list != null) {
            callback.onResult(list);
        } else {
            invalidate();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy