package com.cartoon.wallpapers.live.page;


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

import com.cartoon.wallpapers.live.mydata.FavoriteData;
import com.cartoon.wallpapers.live.utils.Com;
import com.cartoon.wallpapers.live.utils.ObjectBoxManager;

import java.util.List;

import kotlin.coroutines.Continuation;

public class LikePageSource extends PagingSource<Integer, FavoriteData> {
     public static int pageSize = 10;


    public LikePageSource() {

    }

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

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

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

            List<FavoriteData> data = ObjectBoxManager.queryLike(page);
            Com.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);
        }
    }
}

