package com.hd.live.wall.wallpapers.pages;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.paging.PagingSource;
import androidx.paging.PagingState;

import com.hd.live.wall.wallpapers.dbData.Collection;
import com.hd.live.wall.wallpapers.utils.helper;
import com.hd.live.wall.wallpapers.utils.dbManager;

import java.util.List;

import kotlin.coroutines.Continuation;

public class PageSourceLike extends PagingSource<Integer, Collection> {
     public static int pageSize = 10;

    public PageSourceLike() {

    }

    @Nullable
    @Override
    public Integer getRefreshKey(@NonNull PagingState<Integer, Collection> pagingState) {
        return null;
    }

    @Nullable
    @Override
    public Object load(@NonNull LoadParams<Integer> loadParams, @NonNull Continuation<? super LoadResult<Integer, Collection>> continuation) {

        try {
            int page = loadParams.getKey() != null ? loadParams.getKey() : 1;

            List<Collection> data = dbManager.queryLike(page);
            helper.logMsg("-----loadLIke---------page="+page+"--data="+data.size());
            return new LoadResult.Page<>(
                    data,
                    page > 1 ? page - 1 : null,
                    data.isEmpty() ? null : page + 1
            );
        } catch (Exception e) {
            return new LoadResult.Error<>(e);
        }
    }
}

