Use more concise runCatching instead of Result

This commit is contained in:
Kirill Kamakin
2021-11-13 10:14:15 +03:00
parent 10b3dadc3d
commit cb495aaba3
6 changed files with 19 additions and 25 deletions

View File

@@ -4,5 +4,5 @@ interface AuthDataSource {
/** /**
* Tries to acquire authentication token using the provided credentials on specified server. * Tries to acquire authentication token using the provided credentials on specified server.
*/ */
suspend fun authenticate(username: String, password: String, baseUrl: String): Result<String> suspend fun authenticate(username: String, password: String, baseUrl: String): String
} }

View File

@@ -3,7 +3,7 @@ package gq.kirmanak.mealie.data.auth
interface AuthRepo { interface AuthRepo {
suspend fun isAuthenticated(): Boolean suspend fun isAuthenticated(): Boolean
suspend fun authenticate(username: String, password: String, baseUrl: String): Throwable? suspend fun authenticate(username: String, password: String, baseUrl: String)
suspend fun getBaseUrl(): String? suspend fun getBaseUrl(): String?

View File

@@ -1,8 +1,8 @@
package gq.kirmanak.mealie.data.auth.impl package gq.kirmanak.mealie.data.auth.impl
import gq.kirmanak.mealie.data.impl.RetrofitBuilder
import gq.kirmanak.mealie.data.auth.AuthDataSource import gq.kirmanak.mealie.data.auth.AuthDataSource
import gq.kirmanak.mealie.data.auth.AuthService import gq.kirmanak.mealie.data.auth.AuthService
import gq.kirmanak.mealie.data.impl.RetrofitBuilder
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import retrofit2.create import retrofit2.create
import timber.log.Timber import timber.log.Timber
@@ -16,16 +16,11 @@ class AuthDataSourceImpl @Inject constructor(
username: String, username: String,
password: String, password: String,
baseUrl: String baseUrl: String
): Result<String> { ): String {
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl") Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
val authService = retrofitBuilder.buildRetrofit(baseUrl).create<AuthService>() val authService = retrofitBuilder.buildRetrofit(baseUrl).create<AuthService>()
val response = try { val response = authService.getToken(username, password)
authService.getToken(username, password)
} catch (e: Exception) {
Timber.e(e, "Authenticate() exception")
return Result.failure(e)
}
Timber.d("authenticate() response is $response") Timber.d("authenticate() response is $response")
return Result.success(response.accessToken) return response.accessToken
} }
} }

View File

@@ -21,15 +21,12 @@ class AuthRepoImpl @Inject constructor(
username: String, username: String,
password: String, password: String,
baseUrl: String baseUrl: String
): Throwable? { ) {
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl") Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
val url = if (baseUrl.startsWith("http")) baseUrl else "https://$baseUrl" val url = if (baseUrl.startsWith("http")) baseUrl else "https://$baseUrl"
val authResult = dataSource.authenticate(username, password, url) val accessToken = dataSource.authenticate(username, password, url)
Timber.d("authenticate result is $authResult") Timber.d("authenticate result is $accessToken")
if (authResult.isFailure) return authResult.exceptionOrNull() storage.storeAuthData(accessToken, url)
val token = checkNotNull(authResult.getOrNull())
storage.storeAuthData(token, url)
return null
} }
override suspend fun getBaseUrl(): String? { override suspend fun getBaseUrl(): String? {

View File

@@ -71,8 +71,13 @@ class AuthenticationFragment : Fragment() {
} ?: return } ?: return
} }
lifecycleScope.launchWhenResumed { lifecycleScope.launchWhenResumed {
val exception = viewModel.authenticate(email, pass, url) runCatching {
if (exception == null) navigateToRecipes() viewModel.authenticate(email, pass, url)
}.onSuccess {
navigateToRecipes()
}.onFailure {
Timber.e(it, "Can't authenticate")
}
} }
} }

View File

@@ -21,11 +21,8 @@ class AuthenticationViewModel @Inject constructor(
return result return result
} }
suspend fun authenticate(username: String, password: String, baseUrl: String): Throwable? { suspend fun authenticate(username: String, password: String, baseUrl: String) {
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl") Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
val result = authRepo.authenticate(username, password, baseUrl) authRepo.authenticate(username, password, baseUrl)
if (result == null) Timber.d("authenticate() returns null")
else Timber.e(result, "authenticate() returns error")
return result
} }
} }