Hide MealieDataSourceWrapper behind interfaces
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
package gq.kirmanak.mealient.data.add.impl
|
|
||||||
|
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
|
||||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
|
||||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
|
||||||
import gq.kirmanak.mealient.extensions.logAndMapErrors
|
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
class AddRecipeDataSourceImpl @Inject constructor(
|
|
||||||
private val logger: Logger,
|
|
||||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
|
||||||
) : AddRecipeDataSource {
|
|
||||||
|
|
||||||
override suspend fun addRecipe(recipe: AddRecipeRequest): String {
|
|
||||||
logger.v { "addRecipe() called with: recipe = $recipe" }
|
|
||||||
val response = logger.logAndMapErrors(
|
|
||||||
block = { mealieDataSourceWrapper.addRecipe(recipe) },
|
|
||||||
logProvider = { "addRecipe: can't add recipe" }
|
|
||||||
)
|
|
||||||
logger.v { "addRecipe() response = $response" }
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package gq.kirmanak.mealient.data.baseurl.impl
|
|
||||||
|
|
||||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
|
||||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
|
||||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
|
||||||
import gq.kirmanak.mealient.extensions.logAndMapErrors
|
|
||||||
import gq.kirmanak.mealient.extensions.versionInfo
|
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
class VersionDataSourceImpl @Inject constructor(
|
|
||||||
private val logger: Logger,
|
|
||||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
|
||||||
) : VersionDataSource {
|
|
||||||
|
|
||||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo {
|
|
||||||
logger.v { "getVersionInfo() called with: baseUrl = $baseUrl" }
|
|
||||||
|
|
||||||
val response = logger.logAndMapErrors(
|
|
||||||
block = { mealieDataSourceWrapper.getVersionInfo(baseUrl) },
|
|
||||||
logProvider = { "getVersionInfo: can't request version" }
|
|
||||||
)
|
|
||||||
|
|
||||||
return response.versionInfo()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
package gq.kirmanak.mealient.data.network
|
package gq.kirmanak.mealient.data.network
|
||||||
|
|
||||||
|
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||||
|
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||||
|
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||||
|
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||||
import gq.kirmanak.mealient.datasource.models.*
|
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||||
|
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||||
|
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
||||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||||
|
import gq.kirmanak.mealient.extensions.toVersionInfo
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -13,34 +20,27 @@ class MealieDataSourceWrapper @Inject constructor(
|
|||||||
private val baseURLStorage: BaseURLStorage,
|
private val baseURLStorage: BaseURLStorage,
|
||||||
private val authRepo: AuthRepo,
|
private val authRepo: AuthRepo,
|
||||||
private val mealieDataSource: MealieDataSource,
|
private val mealieDataSource: MealieDataSource,
|
||||||
) {
|
) : AddRecipeDataSource, RecipeDataSource, VersionDataSource {
|
||||||
|
|
||||||
suspend fun addRecipe(recipe: AddRecipeRequest): String {
|
override suspend fun addRecipe(recipe: AddRecipeRequest): String =
|
||||||
val baseUrl = baseURLStorage.requireBaseURL()
|
withAuthHeader { token -> addRecipe(getUrl(), token, recipe) }
|
||||||
return withAuthHeader { token -> addRecipe(baseUrl, token, recipe) }
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getVersionInfo(baseUrl: String): VersionResponse {
|
override suspend fun getVersionInfo(baseUrl: String): VersionInfo =
|
||||||
return mealieDataSource.getVersionInfo(baseUrl)
|
mealieDataSource.getVersionInfo(baseUrl).toVersionInfo()
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun requestRecipes(
|
override suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponse> =
|
||||||
start: Int = 0,
|
withAuthHeader { token -> requestRecipes(getUrl(), token, start, limit) }
|
||||||
limit: Int = 9999,
|
|
||||||
): List<GetRecipeSummaryResponse> {
|
|
||||||
val baseUrl = baseURLStorage.requireBaseURL()
|
|
||||||
return withAuthHeader { token -> requestRecipes(baseUrl, token, start, limit) }
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun requestRecipeInfo(slug: String): GetRecipeResponse {
|
override suspend fun requestRecipeInfo(slug: String): GetRecipeResponse =
|
||||||
val baseUrl = baseURLStorage.requireBaseURL()
|
withAuthHeader { token -> requestRecipeInfo(getUrl(), token, slug) }
|
||||||
return withAuthHeader { token -> requestRecipeInfo(baseUrl, token, slug) }
|
|
||||||
}
|
private suspend fun getUrl() = baseURLStorage.requireBaseURL()
|
||||||
|
|
||||||
private suspend inline fun <T> withAuthHeader(block: MealieDataSource.(String?) -> T): T =
|
private suspend inline fun <T> withAuthHeader(block: MealieDataSource.(String?) -> T): T =
|
||||||
mealieDataSource.runCatching { block(authRepo.getAuthHeader()) }.getOrElse {
|
mealieDataSource.runCatching { block(authRepo.getAuthHeader()) }.getOrElse {
|
||||||
if (it is NetworkError.Unauthorized) {
|
if (it is NetworkError.Unauthorized) {
|
||||||
authRepo.invalidateAuthHeader()
|
authRepo.invalidateAuthHeader()
|
||||||
|
// Trying again with new authentication header
|
||||||
mealieDataSource.block(authRepo.getAuthHeader())
|
mealieDataSource.block(authRepo.getAuthHeader())
|
||||||
} else {
|
} else {
|
||||||
throw it
|
throw it
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
package gq.kirmanak.mealient.data.recipes.network
|
|
||||||
|
|
||||||
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
|
||||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
|
||||||
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
class RecipeDataSourceImpl @Inject constructor(
|
|
||||||
private val logger: Logger,
|
|
||||||
private val mealieDataSourceWrapper: MealieDataSourceWrapper,
|
|
||||||
) : RecipeDataSource {
|
|
||||||
|
|
||||||
override suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponse> {
|
|
||||||
logger.v { "requestRecipes() called with: start = $start, limit = $limit" }
|
|
||||||
val recipeSummary = mealieDataSourceWrapper.requestRecipes(start, limit)
|
|
||||||
logger.v { "requestRecipes() returned: $recipeSummary" }
|
|
||||||
return recipeSummary
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun requestRecipeInfo(slug: String): GetRecipeResponse {
|
|
||||||
logger.v { "requestRecipeInfo() called with: slug = $slug" }
|
|
||||||
val recipeInfo = mealieDataSourceWrapper.requestRecipeInfo(slug)
|
|
||||||
logger.v { "requestRecipeInfo() returned: $recipeInfo" }
|
|
||||||
return recipeInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -6,8 +6,8 @@ import dagger.hilt.InstallIn
|
|||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||||
import gq.kirmanak.mealient.data.add.impl.AddRecipeDataSourceImpl
|
|
||||||
import gq.kirmanak.mealient.data.add.impl.AddRecipeRepoImpl
|
import gq.kirmanak.mealient.data.add.impl.AddRecipeRepoImpl
|
||||||
|
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
||||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorageImpl
|
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorageImpl
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -23,7 +23,7 @@ interface AddRecipeModule {
|
|||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
fun bindAddRecipeDataSource(addRecipeDataSourceImpl: AddRecipeDataSourceImpl): AddRecipeDataSource
|
fun bindAddRecipeDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): AddRecipeDataSource
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import dagger.hilt.components.SingletonComponent
|
|||||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||||
import gq.kirmanak.mealient.data.baseurl.impl.BaseURLStorageImpl
|
import gq.kirmanak.mealient.data.baseurl.impl.BaseURLStorageImpl
|
||||||
import gq.kirmanak.mealient.data.baseurl.impl.VersionDataSourceImpl
|
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@@ -16,7 +16,7 @@ interface BaseURLModule {
|
|||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
fun bindVersionDataSource(versionDataSourceImpl: VersionDataSourceImpl): VersionDataSource
|
fun bindVersionDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): VersionDataSource
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import dagger.Provides
|
|||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import gq.kirmanak.mealient.R
|
import gq.kirmanak.mealient.R
|
||||||
|
import gq.kirmanak.mealient.data.network.MealieDataSourceWrapper
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||||
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
|
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
|
||||||
import gq.kirmanak.mealient.data.recipes.db.RecipeStorageImpl
|
import gq.kirmanak.mealient.data.recipes.db.RecipeStorageImpl
|
||||||
@@ -16,7 +17,6 @@ import gq.kirmanak.mealient.data.recipes.impl.RecipeImageUrlProvider
|
|||||||
import gq.kirmanak.mealient.data.recipes.impl.RecipeImageUrlProviderImpl
|
import gq.kirmanak.mealient.data.recipes.impl.RecipeImageUrlProviderImpl
|
||||||
import gq.kirmanak.mealient.data.recipes.impl.RecipeRepoImpl
|
import gq.kirmanak.mealient.data.recipes.impl.RecipeRepoImpl
|
||||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSourceImpl
|
|
||||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||||
import gq.kirmanak.mealient.ui.recipes.images.RecipeModelLoaderFactory
|
import gq.kirmanak.mealient.ui.recipes.images.RecipeModelLoaderFactory
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
@@ -28,7 +28,7 @@ interface RecipeModule {
|
|||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideRecipeDataSource(recipeDataSourceImpl: RecipeDataSourceImpl): RecipeDataSource
|
fun provideRecipeDataSource(mealieDataSourceWrapper: MealieDataSourceWrapper): RecipeDataSource
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ fun GetRecipeSummaryResponse.recipeEntity() = RecipeSummaryEntity(
|
|||||||
dateUpdated = dateUpdated,
|
dateUpdated = dateUpdated,
|
||||||
)
|
)
|
||||||
|
|
||||||
fun VersionResponse.versionInfo() = VersionInfo(production, version, demoStatus)
|
fun VersionResponse.toVersionInfo() = VersionInfo(production, version, demoStatus)
|
||||||
|
|
||||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest(
|
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest(
|
||||||
name = recipeName,
|
name = recipeName,
|
||||||
|
|||||||
Reference in New Issue
Block a user