package com.maniu.mn_vip_jetpack_other.paging;


import android.util.Log;

import androidx.annotation.NonNull;
import androidx.paging.PositionalDataSource;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据源
 */
public class ConcertDataSource extends PositionalDataSource<Concert> {

    /**
     * 初始化第一页的数据会调用的方法
     * @param params
     * @param callback
     */
    @Override
    public void loadInitial(@NonNull LoadInitialParams params, @NonNull
    final LoadInitialCallback<Concert> callback) {
        callback.onResult(fetchItems(0, 20), 0, 100);
    }

    /**
     * 加载更多也调用的方法
     * @param params
     * @param callback
     */
    @Override
    public void loadRange(@NonNull LoadRangeParams params, @NonNull
    final LoadRangeCallback<Concert> callback) {
        callback.onResult(fetchItems(params.startPosition, params.loadSize));
        Log.e("MN---------->",params.startPosition+""+params.loadSize);
    }

    private List<Concert> fetchItems(int startPosition, int pageSize) {
        List<Concert> list = new ArrayList<>();
        for (int i = startPosition; i < startPosition + pageSize; i++) {
            Concert concert = new Concert();
            concert.setAuthor("author = " + i);
            concert.setContent("content = " + i);
            concert.setTitle("title = " + i);
            list.add(concert);
        }
        return list;
    }
}
