Skip to content

Commit

Permalink
chore: Avoid Sonar lint complexity code
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Jan 31, 2025
1 parent 847b537 commit 5f6e02d
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.infomaniak.mail.utils

import android.content.Context
import androidx.activity.result.ActivityResult
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -100,37 +101,37 @@ class LoginUtils @Inject constructor(

val context = requireContext()

suspend fun loginSuccess(user: User) {
context.trackAccountEvent("loggedIn")
ioDispatcher {
mailboxController.getFirstValidMailbox(user.id)?.mailboxId?.let { AccountUtils.currentMailboxId = it }
}
AccountUtils.reloadApp?.invoke()
when (val returnValue = LoginActivity.authenticateUser(context, apiToken, mailboxController)) {
is User -> return@launch context.loginSuccess(returnValue)
is MailboxErrorCode -> context.mailboxError(returnValue)
is ApiResponse<*> -> context.apiError(returnValue)
else -> context.otherError()
}

suspend fun mailboxError(errorCode: MailboxErrorCode) = withContext(mainDispatcher) {
when (errorCode) {
MailboxErrorCode.NO_MAILBOX -> context.launchNoMailboxActivity()
MailboxErrorCode.NO_VALID_MAILBOX -> context.launchNoValidMailboxesActivity()
}
}
logout(infomaniakLogin, apiToken)
}

suspend fun apiError(apiResponse: ApiResponse<*>) = withContext(mainDispatcher) {
showError(context.getString(apiResponse.translatedError))
private suspend fun Context.loginSuccess(user: User) {
trackAccountEvent("loggedIn")
ioDispatcher {
mailboxController.getFirstValidMailbox(user.id)?.mailboxId?.let { AccountUtils.currentMailboxId = it }
}
AccountUtils.reloadApp?.invoke()
}

suspend fun otherError() = withContext(mainDispatcher) {
showError(context.getString(R.string.anErrorHasOccurred))
private suspend fun Context.mailboxError(errorCode: MailboxErrorCode) {
when (errorCode) {
MailboxErrorCode.NO_MAILBOX -> launchNoMailboxActivity()
MailboxErrorCode.NO_VALID_MAILBOX -> launchNoValidMailboxesActivity()
}
}

when (val returnValue = LoginActivity.authenticateUser(context, apiToken, mailboxController)) {
is User -> return@launch loginSuccess(returnValue)
is MailboxErrorCode -> mailboxError(returnValue)
is ApiResponse<*> -> apiError(returnValue)
else -> otherError()
}
private suspend fun Context.apiError(apiResponse: ApiResponse<*>) = withContext(mainDispatcher) {
showError(getString(apiResponse.translatedError))
}

logout(infomaniakLogin, apiToken)
private suspend fun Context.otherError() = withContext(mainDispatcher) {
showError(getString(R.string.anErrorHasOccurred))
}

private suspend fun logout(infomaniakLogin: InfomaniakLogin, apiToken: ApiToken) {
Expand Down

0 comments on commit 5f6e02d

Please sign in to comment.