Replace Timber with Logger

This commit is contained in:
Kirill Kamakin
2022-08-05 20:16:29 +02:00
parent ba5f7322ab
commit 107bb64256
49 changed files with 458 additions and 260 deletions

View File

@@ -7,10 +7,10 @@ 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.logAndMapErrors
import gq.kirmanak.mealient.logging.Logger
import kotlinx.serialization.json.Json
import retrofit2.HttpException
import retrofit2.Response
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@@ -18,14 +18,15 @@ import javax.inject.Singleton
class AuthDataSourceImpl @Inject constructor(
private val authServiceFactory: ServiceFactory<AuthService>,
private val json: Json,
private val logger: Logger,
) : AuthDataSource {
override suspend fun authenticate(username: String, password: String): String {
Timber.v("authenticate() called with: username = $username, password = $password")
logger.v { "authenticate() called with: username = $username, password = $password" }
val authService = authServiceFactory.provideService()
val response = sendRequest(authService, username, password)
val accessToken = parseToken(response)
Timber.v("authenticate() returned: $accessToken")
logger.v { "authenticate() returned: $accessToken" }
return accessToken
}
@@ -34,6 +35,7 @@ class AuthDataSourceImpl @Inject constructor(
username: String,
password: String
): Response<GetTokenResponse> = logAndMapErrors(
logger,
block = { authService.getToken(username = username, password = password) },
logProvider = { "sendRequest: can't get token" },
)
@@ -44,7 +46,7 @@ class AuthDataSourceImpl @Inject constructor(
response.body()?.accessToken ?: throw NotMealie(NullPointerException("Body is null"))
} else {
val cause = HttpException(response)
val errorDetail: ErrorDetail? = response.decodeErrorBodyOrNull(json)
val errorDetail: ErrorDetail? = response.decodeErrorBodyOrNull(json, logger)
throw when (errorDetail?.detail) {
"Unauthorized" -> Unauthorized(cause)
else -> NotMealie(cause)

View File

@@ -4,9 +4,9 @@ import gq.kirmanak.mealient.data.auth.AuthDataSource
import gq.kirmanak.mealient.data.auth.AuthRepo
import gq.kirmanak.mealient.data.auth.AuthStorage
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
import gq.kirmanak.mealient.logging.Logger
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@@ -14,13 +14,14 @@ import javax.inject.Singleton
class AuthRepoImpl @Inject constructor(
private val authStorage: AuthStorage,
private val authDataSource: AuthDataSource,
private val logger: Logger,
) : AuthRepo {
override val isAuthorizedFlow: Flow<Boolean>
get() = authStorage.authHeaderFlow.map { it != null }
override suspend fun authenticate(email: String, password: String) {
Timber.v("authenticate() called with: email = $email, password = $password")
logger.v { "authenticate() called with: email = $email, password = $password" }
authDataSource.authenticate(email, password)
.let { AUTH_HEADER_FORMAT.format(it) }
.let { authStorage.setAuthHeader(it) }
@@ -35,14 +36,14 @@ class AuthRepoImpl @Inject constructor(
}
override suspend fun logout() {
Timber.v("logout() called")
logger.v { "logout() called" }
authStorage.setEmail(null)
authStorage.setPassword(null)
authStorage.setAuthHeader(null)
}
override suspend fun invalidateAuthHeader() {
Timber.v("invalidateAuthHeader() called")
logger.v { "invalidateAuthHeader() called" }
val email = authStorage.getEmail() ?: return
val password = authStorage.getPassword() ?: return
runCatchingExceptCancel { authenticate(email, password) }

View File

@@ -6,11 +6,11 @@ import androidx.core.content.edit
import gq.kirmanak.mealient.data.auth.AuthStorage
import gq.kirmanak.mealient.datastore.DataStoreModule.Companion.ENCRYPTED
import gq.kirmanak.mealient.extensions.prefsChangeFlow
import gq.kirmanak.mealient.logging.Logger
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.concurrent.Executors
import javax.inject.Inject
import javax.inject.Named
@@ -19,11 +19,12 @@ import javax.inject.Singleton
@Singleton
class AuthStorageImpl @Inject constructor(
@Named(ENCRYPTED) private val sharedPreferences: SharedPreferences,
private val logger: Logger,
) : AuthStorage {
override val authHeaderFlow: Flow<String?>
get() = sharedPreferences
.prefsChangeFlow { getString(AUTH_HEADER_KEY, null) }
.prefsChangeFlow(logger) { getString(AUTH_HEADER_KEY, null) }
.distinctUntilChanged()
private val singleThreadDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
@@ -43,13 +44,13 @@ class AuthStorageImpl @Inject constructor(
key: String,
value: String?
) = withContext(singleThreadDispatcher) {
Timber.v("putString() called with: key = $key, value = $value")
logger.v { "putString() called with: key = $key, value = $value" }
sharedPreferences.edit(commit = true) { putString(key, value) }
}
private suspend fun getString(key: String) = withContext(singleThreadDispatcher) {
val result = sharedPreferences.getString(key, null)
Timber.v("getString() called with: key = $key, returned: $result")
logger.v { "getString() called with: key = $key, returned: $result" }
result
}