Better handle login states

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-08-21 13:16:59 +05:30
parent b77a87d55f
commit 6537156019
5 changed files with 41 additions and 17 deletions

View File

@@ -41,5 +41,6 @@ sealed class AuthState {
object SignedOut : AuthState() object SignedOut : AuthState()
object Valid : AuthState() object Valid : AuthState()
object Fetching: AuthState() object Fetching: AuthState()
data class Status(val status: String?) : AuthState() object Verifying: AuthState()
data class Failed(val status: String) : AuthState()
} }

View File

@@ -104,8 +104,13 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
updateActionLayout(true) updateActionLayout(true)
} }
is AuthState.Status -> { AuthState.Verifying -> {
updateStatus(getString(R.string.verifying_new_session))
}
is AuthState.Failed -> {
updateStatus(it.status) updateStatus(it.status)
updateActionLayout(true)
resetActions() resetActions()
} }
} }

View File

@@ -115,8 +115,13 @@ class SplashFragment : Fragment(R.layout.fragment_splash) {
updateActionLayout(true) updateActionLayout(true)
} }
is AuthState.Status -> { AuthState.Verifying -> {
updateStatus(getString(R.string.verifying_new_session))
}
is AuthState.Failed -> {
updateStatus(it.status) updateStatus(it.status)
updateActionLayout(true)
resetActions() resetActions()
} }
} }

View File

@@ -87,7 +87,11 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
val authData = AuthHelper.build(email, aasToken, properties) val authData = AuthHelper.build(email, aasToken, properties)
verifyAndSaveAuth(authData, AccountType.GOOGLE) verifyAndSaveAuth(authData, AccountType.GOOGLE)
} catch (exception: Exception) { } catch (exception: Exception) {
updateStatus("Failed to generate Session") liveData.postValue(
AuthState.Failed(
(getApplication() as Context).getString(R.string.failed_to_generate_session)
)
)
Log.e(TAG, "Failed to generate Session", exception) Log.e(TAG, "Failed to generate Session", exception)
} }
} }
@@ -136,7 +140,7 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
throwError(playResponse, getApplication()) throwError(playResponse, getApplication())
} }
} catch (exception: Exception) { } catch (exception: Exception) {
updateStatus(exception.message.toString()) liveData.postValue(AuthState.Failed(exception.message.toString()))
Log.e(TAG, "Failed to generate Session", exception) Log.e(TAG, "Failed to generate Session", exception)
} }
} }
@@ -180,7 +184,7 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
throwError(playResponse, getApplication()) throwError(playResponse, getApplication())
} }
} catch (exception: Exception) { } catch (exception: Exception) {
updateStatus(exception.message.toString()) liveData.postValue(AuthState.Failed(exception.message.toString()))
Log.e(TAG, "Failed to generate Session", exception) Log.e(TAG, "Failed to generate Session", exception)
} }
} }
@@ -242,11 +246,18 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
when (e) { val error = when (e) {
is UnknownHostException -> updateStatus("No network") is UnknownHostException -> {
is ConnectException -> updateStatus("Could not connect to server") (getApplication() as Context).getString(R.string.title_no_network)
else -> updateStatus("Unknown error") }
is ConnectException -> {
(getApplication() as Context).getString(R.string.server_unreachable)
}
else -> {
(getApplication() as Context).getString(R.string.bad_request)
}
} }
liveData.postValue(AuthState.Failed(error))
requestState = RequestState.Pending requestState = RequestState.Pending
} }
} }
@@ -272,7 +283,7 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
} }
private fun verifyAndSaveAuth(authData: AuthData, type: AccountType) { private fun verifyAndSaveAuth(authData: AuthData, type: AccountType) {
updateStatus("Verifying new session") liveData.postValue(AuthState.Verifying)
if (spoofProvider.isLocaleSpoofEnabled()) { if (spoofProvider.isLocaleSpoofEnabled()) {
authData.locale = spoofProvider.getSpoofLocale() authData.locale = spoofProvider.getSpoofLocale()
@@ -289,7 +300,11 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
liveData.postValue(AuthState.SignedOut) liveData.postValue(AuthState.SignedOut)
requestState = RequestState.Pending requestState = RequestState.Pending
updateStatus("Failed to verify session") liveData.postValue(
AuthState.Failed(
(getApplication() as Context).getString(R.string.failed_to_generate_session)
)
)
} }
} }
@@ -314,10 +329,6 @@ class AuthViewModel(application: Application) : BaseAndroidViewModel(application
Preferences.putBoolean(getApplication(), Constants.ACCOUNT_SIGNED_IN, signedIn) Preferences.putBoolean(getApplication(), Constants.ACCOUNT_SIGNED_IN, signedIn)
} }
private fun updateStatus(status: String) {
liveData.postValue(AuthState.Status(status))
}
@Throws(Exception::class) @Throws(Exception::class)
private fun throwError(playResponse: PlayResponse, context: Context) { private fun throwError(playResponse: PlayResponse, context: Context) {
when (playResponse.code) { when (playResponse.code) {

View File

@@ -364,6 +364,8 @@
<string name="bad_request">Internal error! Please retry after sometime</string> <string name="bad_request">Internal error! Please retry after sometime</string>
<string name="login_rate_limited">Oops, You are rate limited</string> <string name="login_rate_limited">Oops, You are rate limited</string>
<string name="server_unreachable">Server unreachable</string> <string name="server_unreachable">Server unreachable</string>
<string name="failed_to_generate_session">Failed to generate Session</string>
<string name="verifying_new_session">Verifying new session</string>
<string name="failed_generating_session">Failed to generate session, error code: <xliff:g id="status_code">%1$d</xliff:g></string> <string name="failed_generating_session">Failed to generate session, error code: <xliff:g id="status_code">%1$d</xliff:g></string>
<string name="check_connectivity">Check internet connectivity</string> <string name="check_connectivity">Check internet connectivity</string>
<string name="app_language">App Language</string> <string name="app_language">App Language</string>