Skip to content

Commit

Permalink
Refactor the code to eliminate this nested "when".
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Jun 4, 2024
1 parent de8f8e1 commit 1eafc8f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
10 changes: 7 additions & 3 deletions app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 25 additions & 18 deletions app/src/main/java/com/infomaniak/mail/utils/LoginUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1eafc8f

Please sign in to comment.