package library.ext

import android.os.Looper
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.DataSource
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList

/**
 * 线程分配设置数据到LiveData
 */
fun <T> MutableLiveData<T>.setData(data: T?) =
        if (Looper.getMainLooper().thread == Thread.currentThread()) {
            setValue(data)
        } else {
            postValue(data)
        }

fun <T, K> DataSource.Factory<T, K>.toLiveData(pageSize: Int = 20,
                                               callback: PagedList.BoundaryCallback<K>? = null): LiveData<PagedList<K>> =
        LivePagedListBuilder(this, pageSize).setBoundaryCallback(callback).build()
