Created
May 21, 2022 04:01
-
-
Save MamboBryan/11fa0b29844fec80621065ebccd727df to your computer and use it in GitHub Desktop.
The repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mambobryan.samba.data.repositories | |
import androidx.paging.ExperimentalPagingApi | |
import androidx.paging.Pager | |
import androidx.paging.PagingConfig | |
import androidx.paging.PagingData | |
import com.mambobryan.samba.data.local.AppDatabase | |
import com.mambobryan.samba.data.model.Character | |
import com.mambobryan.samba.data.remote.ApiService | |
import com.mambobryan.samba.data.remote.CharactersPagingSource | |
import com.mambobryan.samba.data.remote.CharactersRemoteMediator | |
import kotlinx.coroutines.flow.Flow | |
import javax.inject.Inject | |
class CharactersRepository @Inject constructor( | |
private val apiService: ApiService, | |
private val database: AppDatabase | |
) { | |
companion object { | |
const val PAGE_SIZE = 20 | |
} | |
// REMOTE ONLY | |
fun getCharacters(): Flow<PagingData<Character>> { | |
return Pager( | |
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), | |
pagingSourceFactory = { CharactersPagingSource(apiService) } | |
).flow | |
} | |
// REMOTE WITH CACHE | |
@OptIn(ExperimentalPagingApi::class) | |
fun getCharactersWithCache(): Flow<PagingData<Character>> { | |
return Pager( | |
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), | |
remoteMediator = CharactersRemoteMediator(service = apiService, database = database), | |
pagingSourceFactory = { database.charactersDao().getAllCharacters() } | |
).flow | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment