From 1eafc8f06ed4814e5ad7f24d8616491a45fb34df Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Tue, 4 Jun 2024 07:45:24 +0200 Subject: [PATCH] Refactor the code to eliminate this nested "when". --- .../infomaniak/mail/utils/ConfettiUtils.kt | 10 ++- .../com/infomaniak/mail/utils/LoginUtils.kt | 43 +++++++------ .../mail/workers/DraftsActionsWorker.kt | 64 ++++++++++--------- 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt b/app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt index 2729ad3fd2e..e171928bab6 100644 --- a/app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt +++ b/app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt @@ -145,14 +145,18 @@ object ConfettiUtils { displayConfetti(config, sourceRight, container) } - when (type) { - ConfettiType.COLORED_SNOW -> displaySnow(colored = true) - ConfettiType.INFOMANIAK -> when ((0..2).random()) { + fun displayInfomaniak() { + when ((0..2).random()) { 0 -> displayTada() 1 -> displaySnow() else -> if (isInPortrait()) displaySingleGeneva() else displayDoubleGeneva() } } + + when (type) { + ConfettiType.COLORED_SNOW -> displaySnow(colored = true) + ConfettiType.INFOMANIAK -> displayInfomaniak() + } } private fun displayConfetti(config: ConfettiConfig, source: ConfettiSource, container: ViewGroup) = with(config) { diff --git a/app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt b/app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt index d24f1d680fc..9d953117baa 100644 --- a/app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt +++ b/app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt @@ -92,27 +92,34 @@ class LoginUtils @Inject constructor( val context = requireContext() - when (val returnValue = LoginActivity.authenticateUser(context, apiToken, mailboxController)) { - is User -> { - context.trackAccountEvent("loggedIn") - mailboxController.getFirstValidMailbox(returnValue.id)?.mailboxId?.let { AccountUtils.currentMailboxId = it } - AccountUtils.reloadApp?.invoke() - return@launch - } - is MailboxErrorCode -> withContext(mainDispatcher) { - when (returnValue) { - MailboxErrorCode.NO_MAILBOX -> context.launchNoMailboxActivity() - MailboxErrorCode.NO_VALID_MAILBOX -> context.launchNoValidMailboxesActivity() - } - } - is ApiResponse<*> -> withContext(mainDispatcher) { - showError(context.getString(returnValue.translatedError)) - } - else -> withContext(mainDispatcher) { - showError(context.getString(R.string.anErrorHasOccurred)) + suspend fun loginSuccess(user: User) { + context.trackAccountEvent("loggedIn") + mailboxController.getFirstValidMailbox(user.id)?.mailboxId?.let { AccountUtils.currentMailboxId = it } + AccountUtils.reloadApp?.invoke() + } + + suspend fun mailboxError(errorCode: MailboxErrorCode) = withContext(mainDispatcher) { + when (errorCode) { + MailboxErrorCode.NO_MAILBOX -> context.launchNoMailboxActivity() + MailboxErrorCode.NO_VALID_MAILBOX -> context.launchNoValidMailboxesActivity() } } + suspend fun apiError(apiResponse: ApiResponse<*>) = withContext(mainDispatcher) { + showError(context.getString(apiResponse.translatedError)) + } + + suspend fun otherError() = withContext(mainDispatcher) { + showError(context.getString(R.string.anErrorHasOccurred)) + } + + when (val returnValue = LoginActivity.authenticateUser(context, apiToken, mailboxController)) { + is User -> return@launch loginSuccess(returnValue) + is MailboxErrorCode -> mailboxError(returnValue) + is ApiResponse<*> -> apiError(returnValue) + else -> otherError() + } + infomaniakLogin.deleteToken( okHttpClient = HttpClient.okHttpClientNoTokenInterceptor, token = apiToken, diff --git a/app/src/main/java/com/infomaniak/mail/workers/DraftsActionsWorker.kt b/app/src/main/java/com/infomaniak/mail/workers/DraftsActionsWorker.kt index ddac8a0e848..df0dc35f01f 100644 --- a/app/src/main/java/com/infomaniak/mail/workers/DraftsActionsWorker.kt +++ b/app/src/main/java/com/infomaniak/mail/workers/DraftsActionsWorker.kt @@ -313,42 +313,46 @@ class DraftsActionsWorker @AssistedInject constructor( } } - when (updatedDraft.action) { - DraftAction.SAVE -> with(ApiRepository.saveDraft(mailboxUuid, updatedDraft, okHttpClient)) { - data?.let { data -> - realmActionOnDraft = { realm -> - realm.findLatest(updatedDraft)?.apply { - remoteUuid = data.draftRemoteUuid - messageUid = data.messageUid - action = null - } + fun executeSaveAction() = with(ApiRepository.saveDraft(mailboxUuid, updatedDraft, okHttpClient)) { + data?.let { data -> + realmActionOnDraft = { realm -> + realm.findLatest(updatedDraft)?.apply { + remoteUuid = data.draftRemoteUuid + messageUid = data.messageUid + action = null } - scheduledDate = dateFormatWithTimezone.format(Date()) - savedDraftUuid = data.draftRemoteUuid - } ?: run { - retryWithNewIdentityOrThrow(draft, mailboxUuid, isFirstTime) } + scheduledDate = dateFormatWithTimezone.format(Date()) + savedDraftUuid = data.draftRemoteUuid + } ?: run { + retryWithNewIdentityOrThrow(draft, mailboxUuid, isFirstTime) } - DraftAction.SEND -> with(ApiRepository.sendDraft(mailboxUuid, updatedDraft, okHttpClient)) { - when { - isSuccess() -> { - scheduledDate = data?.scheduledDate - realmActionOnDraft = deleteDraftCallback(updatedDraft) - } - error?.exception is SerializationException -> { - realmActionOnDraft = deleteDraftCallback(updatedDraft) - Sentry.withScope { scope -> - scope.setExtra("Is data null ?", "${data == null}") - scope.setExtra("Error code", error?.code.toString()) - scope.setExtra("Error description", error?.description.toString()) - Sentry.captureMessage("Return JSON for SendDraft API call was modified", SentryLevel.ERROR) - } - } - else -> { - retryWithNewIdentityOrThrow(draft, mailboxUuid, isFirstTime) + } + + fun executeSendAction() = with(ApiRepository.sendDraft(mailboxUuid, updatedDraft, okHttpClient)) { + when { + isSuccess() -> { + scheduledDate = data?.scheduledDate + realmActionOnDraft = deleteDraftCallback(updatedDraft) + } + error?.exception is SerializationException -> { + realmActionOnDraft = deleteDraftCallback(updatedDraft) + Sentry.withScope { scope -> + scope.setExtra("Is data null ?", "${data == null}") + scope.setExtra("Error code", error?.code.toString()) + scope.setExtra("Error description", error?.description.toString()) + Sentry.captureMessage("Return JSON for SendDraft API call was modified", SentryLevel.ERROR) } } + else -> { + retryWithNewIdentityOrThrow(draft, mailboxUuid, isFirstTime) + } } + } + + when (updatedDraft.action) { + DraftAction.SAVE -> executeSaveAction() + DraftAction.SEND -> executeSendAction() else -> Unit }