package com.wall.photography.wallpaper.fragment

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.paging.MyLoveSource
//import com.wall.photography.wallpaper.paging.MyLoveSource
import com.wall.photography.wallpaper.room.BeanDao
import com.wall.photography.wallpaper.room.MyDataBase
import kotlinx.coroutines.flow.Flow


class LoveViewModel : ViewModel() {
    private lateinit var userDao: BeanDao
    private lateinit var allLove: LiveData<List<Bean>>


    private fun getPaging(): Flow<PagingData<Bean>> {
        return Pager(
            config = PagingConfig(pageSize = MyApp.pageSize, initialLoadSize = MyApp.pageSize),
            pagingSourceFactory = { MyLoveSource() }
        ).flow
    }

    fun getPagingData(): Flow<PagingData<Bean>> {

        return getPaging().cachedIn(viewModelScope)
    }

    fun setData() {
        userDao = MyDataBase.getInstance().getBeanDao()
        allLove = userDao.getLoveWallpaper()
    }

    fun getLoves(): LiveData<List<Bean>> {
        return allLove
    }
}