Use more concise runCatching instead of Result
This commit is contained in:
@@ -4,5 +4,5 @@ interface AuthDataSource {
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package gq.kirmanak.mealie.data.auth
|
||||
interface AuthRepo {
|
||||
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?
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
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.AuthService
|
||||
import gq.kirmanak.mealie.data.impl.RetrofitBuilder
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import retrofit2.create
|
||||
import timber.log.Timber
|
||||
@@ -16,16 +16,11 @@ class AuthDataSourceImpl @Inject constructor(
|
||||
username: String,
|
||||
password: String,
|
||||
baseUrl: String
|
||||
): Result<String> {
|
||||
): String {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
|
||||
val authService = retrofitBuilder.buildRetrofit(baseUrl).create<AuthService>()
|
||||
val response = try {
|
||||
authService.getToken(username, password)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Authenticate() exception")
|
||||
return Result.failure(e)
|
||||
}
|
||||
val response = authService.getToken(username, password)
|
||||
Timber.d("authenticate() response is $response")
|
||||
return Result.success(response.accessToken)
|
||||
return response.accessToken
|
||||
}
|
||||
}
|
||||
@@ -21,15 +21,12 @@ class AuthRepoImpl @Inject constructor(
|
||||
username: String,
|
||||
password: String,
|
||||
baseUrl: String
|
||||
): Throwable? {
|
||||
) {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
|
||||
val url = if (baseUrl.startsWith("http")) baseUrl else "https://$baseUrl"
|
||||
val authResult = dataSource.authenticate(username, password, url)
|
||||
Timber.d("authenticate result is $authResult")
|
||||
if (authResult.isFailure) return authResult.exceptionOrNull()
|
||||
val token = checkNotNull(authResult.getOrNull())
|
||||
storage.storeAuthData(token, url)
|
||||
return null
|
||||
val accessToken = dataSource.authenticate(username, password, url)
|
||||
Timber.d("authenticate result is $accessToken")
|
||||
storage.storeAuthData(accessToken, url)
|
||||
}
|
||||
|
||||
override suspend fun getBaseUrl(): String? {
|
||||
|
||||
@@ -71,8 +71,13 @@ class AuthenticationFragment : Fragment() {
|
||||
} ?: return
|
||||
}
|
||||
lifecycleScope.launchWhenResumed {
|
||||
val exception = viewModel.authenticate(email, pass, url)
|
||||
if (exception == null) navigateToRecipes()
|
||||
runCatching {
|
||||
viewModel.authenticate(email, pass, url)
|
||||
}.onSuccess {
|
||||
navigateToRecipes()
|
||||
}.onFailure {
|
||||
Timber.e(it, "Can't authenticate")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,11 +21,8 @@ class AuthenticationViewModel @Inject constructor(
|
||||
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")
|
||||
val result = authRepo.authenticate(username, password, baseUrl)
|
||||
if (result == null) Timber.d("authenticate() returns null")
|
||||
else Timber.e(result, "authenticate() returns error")
|
||||
return result
|
||||
authRepo.authenticate(username, password, baseUrl)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user