Merge pull request #69 from kirmanak/cleanup

Cleanup code after modularization
This commit is contained in:
Kirill Kamakin
2022-08-05 22:13:03 +02:00
committed by GitHub
8 changed files with 19 additions and 58 deletions

View File

@@ -105,7 +105,6 @@ dependencies {
implementation(libs.androidx.paging.runtimeKtx)
testImplementation(libs.androidx.paging.commonKtx)
implementation(libs.jetbrains.kotlinx.datetime)
implementation(libs.bumptech.glide.glide)

View File

@@ -17,8 +17,7 @@ class AddRecipeDataSourceImpl @Inject constructor(
override suspend fun addRecipe(recipe: AddRecipeRequest): String {
logger.v { "addRecipe() called with: recipe = $recipe" }
val service = addRecipeServiceFactory.provideService()
val response = logAndMapErrors(
logger,
val response = logger.logAndMapErrors(
block = { service.addRecipe(recipe) },
logProvider = { "addRecipe: can't add recipe" }
)

View File

@@ -2,11 +2,7 @@ package gq.kirmanak.mealient.data.add.impl
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
import gq.kirmanak.mealient.data.add.AddRecipeRepo
import gq.kirmanak.mealient.data.add.models.AddRecipeIngredient
import gq.kirmanak.mealient.data.add.models.AddRecipeInstruction
import gq.kirmanak.mealient.data.add.models.AddRecipeRequest
import gq.kirmanak.mealient.data.add.models.AddRecipeSettings
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
import gq.kirmanak.mealient.logging.Logger
import kotlinx.coroutines.flow.Flow
@@ -23,32 +19,11 @@ class AddRecipeRepoImpl @Inject constructor(
) : AddRecipeRepo {
override val addRecipeRequestFlow: Flow<AddRecipeRequest>
get() = addRecipeStorage.updates.map { it ->
AddRecipeRequest(
name = it.recipeName,
description = it.recipeDescription,
recipeYield = it.recipeYield,
recipeIngredient = it.recipeIngredients.map { AddRecipeIngredient(note = it) },
recipeInstructions = it.recipeInstructions.map { AddRecipeInstruction(text = it) },
settings = AddRecipeSettings(
public = it.isRecipePublic,
disableComments = it.areCommentsDisabled,
)
)
}
get() = addRecipeStorage.updates.map { AddRecipeRequest(it) }
override suspend fun preserve(recipe: AddRecipeRequest) {
logger.v { "preserveRecipe() called with: recipe = $recipe" }
val input = AddRecipeDraft(
recipeName = recipe.name,
recipeDescription = recipe.description,
recipeYield = recipe.recipeYield,
recipeInstructions = recipe.recipeInstructions.map { it.text },
recipeIngredients = recipe.recipeIngredient.map { it.note },
isRecipePublic = recipe.settings.public,
areCommentsDisabled = recipe.settings.disableComments,
)
addRecipeStorage.save(input)
addRecipeStorage.save(recipe.toDraft())
}
override suspend fun clear() {

View File

@@ -5,7 +5,7 @@ import gq.kirmanak.mealient.data.network.ErrorDetail
import gq.kirmanak.mealient.data.network.NetworkError.NotMealie
import gq.kirmanak.mealient.data.network.NetworkError.Unauthorized
import gq.kirmanak.mealient.data.network.ServiceFactory
import gq.kirmanak.mealient.extensions.decodeErrorBodyOrNull
import gq.kirmanak.mealient.extensions.decodeErrorBody
import gq.kirmanak.mealient.extensions.logAndMapErrors
import gq.kirmanak.mealient.logging.Logger
import kotlinx.serialization.json.Json
@@ -34,8 +34,7 @@ class AuthDataSourceImpl @Inject constructor(
authService: AuthService,
username: String,
password: String
): Response<GetTokenResponse> = logAndMapErrors(
logger,
): Response<GetTokenResponse> = logger.logAndMapErrors(
block = { authService.getToken(username = username, password = password) },
logProvider = { "sendRequest: can't get token" },
)
@@ -46,7 +45,9 @@ class AuthDataSourceImpl @Inject constructor(
response.body()?.accessToken ?: throw NotMealie(NullPointerException("Body is null"))
} else {
val cause = HttpException(response)
val errorDetail: ErrorDetail? = response.decodeErrorBodyOrNull(json, logger)
val errorDetail = json.runCatching<Json, ErrorDetail> { decodeErrorBody(response) }
.onFailure { logger.e(it) { "Can't decode error body" } }
.getOrNull()
throw when (errorDetail?.detail) {
"Unauthorized" -> Unauthorized(cause)
else -> NotMealie(cause)

View File

@@ -19,8 +19,7 @@ class VersionDataSourceImpl @Inject constructor(
logger.v { "getVersionInfo() called with: baseUrl = $baseUrl" }
val service = serviceFactory.provideService(baseUrl)
val response = logAndMapErrors(
logger,
val response = logger.logAndMapErrors(
block = { service.getVersion() },
logProvider = { "getVersionInfo: can't request version" }
)

View File

@@ -8,28 +8,23 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import retrofit2.HttpException
import retrofit2.Response
import java.io.InputStream
inline fun <T, reified R> Response<T>.decodeErrorBodyOrNull(json: Json, logger: Logger): R? =
errorBody()?.byteStream()?.let { json.decodeFromStreamOrNull<R>(it, logger) }
@OptIn(ExperimentalSerializationApi::class)
inline fun <reified T> Json.decodeFromStreamOrNull(stream: InputStream, logger: Logger): T? =
runCatching { decodeFromStream<T>(stream) }
.onFailure { logger.e(it) { "decodeFromStreamOrNull: can't decode" } }
.getOrNull()
inline fun <T, reified R> Json.decodeErrorBody(response: Response<T>): R =
checkNotNull(response.errorBody()) { "Can't decode absent error body" }
.byteStream()
.let(::decodeFromStream)
fun Throwable.mapToNetworkError(): NetworkError = when (this) {
is HttpException, is SerializationException -> NetworkError.NotMealie(this)
else -> NetworkError.NoServerConnection(this)
}
inline fun <T> logAndMapErrors(
logger: Logger,
inline fun <T> Logger.logAndMapErrors(
block: () -> T,
noinline logProvider: () -> String
): T =
runCatchingExceptCancel(block).getOrElse {
logger.e(it, messageSupplier = logProvider)
throw it.mapToNetworkError()
}
): T = runCatchingExceptCancel(block).getOrElse {
e(it, messageSupplier = logProvider)
throw it.mapToNetworkError()
}

View File

@@ -1,7 +0,0 @@
package gq.kirmanak.mealient
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class App : Application()