package com.example.pmulabs.room.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.pmulabs.room.dao.ArticleDao
import com.example.pmulabs.room.models.Article
import com.example.pmulabs.room.pagingSource.ArticlePagingSource
import kotlinx.coroutines.flow.Flow

class OfflineArticleRepository(private val articleDao: ArticleDao) : ArticleRepository {
    override suspend fun insertArticle(article: Article) = articleDao.insert(article)
    override suspend fun updateArticle(article: Article) = articleDao.update(article)
    override suspend fun deleteArticle(article: Article) = articleDao.delete(article)
    override fun getAllArticles(): Flow<List<Article>> = articleDao.getAll()
    override fun getArticleById(idArticle: Int): Flow<Article> = articleDao.getArticleById(idArticle)
    override fun getArticles(): Flow<PagingData<Article>> = Pager(config = PagingConfig(pageSize = 4, jumpThreshold = 4, initialLoadSize = 4) ){ ArticlePagingSource(articleDao) }.flow

}