Skip to content

Commit d41fc93

Browse files
committed
Updated CreatePasskeyScreen/ViewModel logic, modified handling of http code errors, and removed redundant HttpException handling
1 parent e247be0 commit d41fc93

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

Shrine/app/src/main/java/com/authentication/shrine/repository/AuthRepository.kt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
5151
import kotlinx.coroutines.flow.first
5252
import kotlinx.coroutines.flow.map
5353
import org.json.JSONObject
54-
import retrofit2.HttpException
5554
import java.io.IOException
5655
import javax.inject.Inject
5756
import javax.inject.Singleton
@@ -111,12 +110,10 @@ class AuthRepository @Inject constructor(
111110
if (response.code() == 401) {
112111
signOut()
113112
}
114-
AuthResult.Failure(AuthError.UserAlreadyExists)
113+
AuthResult.Failure(AuthError.ServerError(response.message()))
115114
}
116115
} catch (e: IOException) {
117116
AuthResult.Failure(AuthError.NetworkError)
118-
} catch (e: HttpException) {
119-
AuthResult.Failure(AuthError.ServerError(e.message()))
120117
} catch (e: Exception) {
121118
AuthResult.Failure(AuthError.Unknown(e.message))
122119
}
@@ -145,12 +142,10 @@ class AuthRepository @Inject constructor(
145142
if (response.code() == 401) {
146143
signOut()
147144
}
148-
AuthResult.Failure(AuthError.ServerError(null))
145+
AuthResult.Failure(AuthError.ServerError(response.message()))
149146
}
150147
} catch (e: IOException) {
151148
AuthResult.Failure(AuthError.NetworkError)
152-
} catch (e: HttpException) {
153-
AuthResult.Failure(AuthError.ServerError(e.message()))
154149
} catch (e: Exception) {
155150
AuthResult.Failure(AuthError.Unknown(e.message))
156151
}
@@ -239,8 +234,6 @@ class AuthRepository @Inject constructor(
239234
}
240235
} catch (e: IOException) {
241236
AuthResult.Failure(AuthError.NetworkError)
242-
} catch (e: HttpException) {
243-
AuthResult.Failure(AuthError.ServerError(e.message()))
244237
} catch (e: Exception) {
245238
AuthResult.Failure(AuthError.Unknown(e.message))
246239
}
@@ -313,8 +306,6 @@ class AuthRepository @Inject constructor(
313306
}
314307
} catch (e: IOException) {
315308
AuthResult.Failure(AuthError.NetworkError)
316-
} catch (e: HttpException) {
317-
AuthResult.Failure(AuthError.ServerError(e.message()))
318309
} catch (e: Exception) {
319310
AuthResult.Failure(AuthError.Unknown(e.message))
320311
}
@@ -344,8 +335,6 @@ class AuthRepository @Inject constructor(
344335
}
345336
} catch (e: IOException) {
346337
AuthResult.Failure(AuthError.NetworkError)
347-
} catch (e: HttpException) {
348-
AuthResult.Failure(AuthError.ServerError(e.message()))
349338
} catch (e: Exception) {
350339
AuthResult.Failure(AuthError.Unknown(e.message))
351340
}
@@ -402,7 +391,7 @@ class AuthRepository @Inject constructor(
402391
if (apiResult.code() == 401) {
403392
signOut()
404393
}
405-
AuthResult.Failure(AuthError.InvalidCredentials)
394+
AuthResult.Failure(AuthError.ServerError(apiResult.message()))
406395
}
407396
}
408397
}
@@ -418,8 +407,6 @@ class AuthRepository @Inject constructor(
418407
AuthResult.Failure(AuthError.Unknown(null))
419408
} catch (e: IOException) {
420409
AuthResult.Failure(AuthError.NetworkError)
421-
} catch (e: HttpException) {
422-
AuthResult.Failure(AuthError.ServerError(e.message()))
423410
} catch (e: Exception) {
424411
AuthResult.Failure(AuthError.Unknown(e.message))
425412
}
@@ -454,8 +441,6 @@ class AuthRepository @Inject constructor(
454441
}
455442
} catch (e: IOException) {
456443
AuthResult.Failure(AuthError.NetworkError)
457-
} catch (e: HttpException) {
458-
AuthResult.Failure(AuthError.ServerError(e.message()))
459444
} catch (e: Exception) {
460445
AuthResult.Failure(AuthError.Unknown(e.message))
461446
}
@@ -477,7 +462,7 @@ class AuthRepository @Inject constructor(
477462
/**
478463
* Checks if the user is signed in through passkeys.
479464
*
480-
* @return True if the user is signed in through passkeys, false otherwise.
465+
* @return True if the user is signed in through passkeys, false otherwise.
481466
*/
482467
suspend fun isSignedInThroughPasskeys(): Boolean {
483468
val isSignedInThroughPasskeys = dataStore.read(IS_SIGNED_IN_THROUGH_PASSKEYS)
@@ -509,8 +494,6 @@ class AuthRepository @Inject constructor(
509494
}
510495
} catch (e: IOException) {
511496
AuthResult.Failure(AuthError.NetworkError)
512-
} catch (e: HttpException) {
513-
AuthResult.Failure(AuthError.ServerError(e.message()))
514497
} catch (e: Exception) {
515498
AuthResult.Failure(AuthError.Unknown(e.message))
516499
}
@@ -598,8 +581,6 @@ class AuthRepository @Inject constructor(
598581
}
599582
} catch (e: IOException) {
600583
AuthResult.Failure(AuthError.NetworkError)
601-
} catch (e: HttpException) {
602-
AuthResult.Failure(AuthError.ServerError(e.message()))
603584
} catch (e: Exception) {
604585
AuthResult.Failure(AuthError.Unknown(e.message))
605586
}

Shrine/app/src/main/java/com/authentication/shrine/ui/CreatePasskeyScreen.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,18 @@ fun CreatePasskeyScreen(
145145
ShrineLoader()
146146
}
147147

148-
val snackbarMessage = when {
149-
!uiState.errorMessage.isNullOrBlank() -> uiState.errorMessage
150-
else -> null
151-
}
148+
if (uiState.messageResourceId != R.string.empty_string) {
149+
val baseMessage = stringResource(uiState.messageResourceId)
150+
val finalMessage = if (uiState.errorMessage != null) {
151+
"$baseMessage ${uiState.errorMessage}"
152+
} else {
153+
baseMessage
154+
}
152155

153-
if (snackbarMessage != null) {
154-
LaunchedEffect(snackbarMessage) {
155-
snackbarHostState.showSnackbar(snackbarMessage)
156+
LaunchedEffect(finalMessage) {
157+
snackbarHostState.showSnackbar(
158+
message = finalMessage,
159+
)
156160
}
157161
}
158162
}

Shrine/app/src/main/java/com/authentication/shrine/ui/viewmodel/CreatePasskeyViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,6 @@ class CreatePasskeyViewModel @Inject constructor(
135135
data class CreatePasskeyUiState(
136136
val isLoading: Boolean = false,
137137
val navigateToMainMenu: Boolean = false,
138-
@StringRes val messageResourceId: Int? = null,
138+
@StringRes val messageResourceId: Int= R.string.empty_string,
139139
val errorMessage: String? = null,
140140
)

0 commit comments

Comments
 (0)