Hide InvalidatingPagingSourceFactory behind interface

This commit is contained in:
Kirill Kamakin
2022-11-13 10:54:03 +01:00
parent 6a6faed15b
commit a952a5252e
5 changed files with 11 additions and 16 deletions

View File

@@ -5,4 +5,5 @@ import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
interface RecipePagingSourceFactory : () -> PagingSource<Int, RecipeSummaryEntity> {
fun setQuery(newQuery: String?)
fun invalidate()
}

View File

@@ -1,8 +1,7 @@
package gq.kirmanak.mealient.data.recipes.impl
import androidx.paging.PagingSource
import androidx.paging.InvalidatingPagingSourceFactory
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
import gq.kirmanak.mealient.logging.Logger
import java.util.concurrent.atomic.AtomicReference
import javax.inject.Inject
@@ -16,14 +15,19 @@ class RecipePagingSourceFactoryImpl @Inject constructor(
private val query = AtomicReference<String>(null)
override fun invoke(): PagingSource<Int, RecipeSummaryEntity> {
private val delegate = InvalidatingPagingSourceFactory {
val currentQuery = query.get()
logger.d { "Creating paging source, query is $currentQuery" }
return recipeStorage.queryRecipes(currentQuery)
recipeStorage.queryRecipes(currentQuery)
}
override fun invoke() = delegate.invoke()
override fun setQuery(newQuery: String?) {
logger.v { "setQuery() called with: newQuery = $newQuery" }
query.set(newQuery)
invalidate()
}
override fun invalidate() = delegate.invalidate()
}

View File

@@ -1,7 +1,6 @@
package gq.kirmanak.mealient.data.recipes.impl
import androidx.paging.ExperimentalPagingApi
import androidx.paging.InvalidatingPagingSourceFactory
import androidx.paging.Pager
import androidx.paging.PagingConfig
import gq.kirmanak.mealient.data.recipes.RecipeRepo
@@ -20,7 +19,6 @@ class RecipeRepoImpl @Inject constructor(
private val mediator: RecipesRemoteMediator,
private val storage: RecipeStorage,
private val pagingSourceFactory: RecipePagingSourceFactory,
private val invalidatingPagingSourceFactory: InvalidatingPagingSourceFactory<Int, RecipeSummaryEntity>,
private val dataSource: RecipeDataSource,
private val logger: Logger,
) : RecipeRepo {
@@ -31,7 +29,7 @@ class RecipeRepoImpl @Inject constructor(
return Pager(
config = pagingConfig,
remoteMediator = mediator,
pagingSourceFactory = invalidatingPagingSourceFactory,
pagingSourceFactory = pagingSourceFactory,
)
}
@@ -59,6 +57,5 @@ class RecipeRepoImpl @Inject constructor(
override fun updateNameQuery(name: String?) {
logger.v { "updateNameQuery() called with: name = $name" }
pagingSourceFactory.setQuery(name)
invalidatingPagingSourceFactory.invalidate()
}
}

View File

@@ -17,7 +17,7 @@ import javax.inject.Singleton
class RecipesRemoteMediator @Inject constructor(
private val storage: RecipeStorage,
private val network: RecipeDataSource,
private val pagingSourceFactory: InvalidatingPagingSourceFactory<Int, RecipeSummaryEntity>,
private val pagingSourceFactory: RecipePagingSourceFactory,
private val logger: Logger,
) : RemoteMediator<Int, RecipeSummaryEntity>() {

View File

@@ -1,6 +1,5 @@
package gq.kirmanak.mealient.di
import androidx.paging.InvalidatingPagingSourceFactory
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.request.RequestOptions
import dagger.Binds
@@ -50,12 +49,6 @@ interface RecipeModule {
companion object {
@Provides
@Singleton
fun provideRecipePagingSourceFactory(
factory: RecipePagingSourceFactory,
) = InvalidatingPagingSourceFactory(factory)
@Provides
@Singleton
fun provideGlideRequestOptions(): RequestOptions = RequestOptions.centerCropTransform()