Skip to content

Commit

Permalink
Merge pull request #1552 from Infomaniak/fix-currentFolder-in-MenuDrawer
Browse files Browse the repository at this point in the history
Use `waitInitMediator()` in MenuDrawer to fix race condition with `currentFolder`
  • Loading branch information
JorisBodin authored Nov 14, 2023
2 parents 69d3b60 + 49e55fa commit 26fd99b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,22 @@ class MenuDrawerFragment : MenuFoldersFragment(), MailboxListFragment {

private fun observeFoldersLive() = with(mainViewModel) {

currentDefaultFoldersLive.observe(viewLifecycleOwner) { defaultFolders ->
val newCurrentFolderId = currentFolderId ?: return@observe
Utils.waitInitMediator(
currentFolder,
currentDefaultFoldersLive,
).observe(viewLifecycleOwner) { (currentFolder, defaultFolders) ->
val newCurrentFolderId = currentFolder?.id ?: return@observe
binding.defaultFoldersList.post {
defaultFoldersAdapter.setFolders(defaultFolders, newCurrentFolderId)
}
}

currentCustomFoldersLive.observe(viewLifecycleOwner) { customFolders ->
Utils.waitInitMediator(
currentFolder,
currentCustomFoldersLive,
).observe(viewLifecycleOwner) { (currentFolder, customFolders) ->
binding.noFolderText.isVisible = customFolders.isEmpty()
val newCurrentFolderId = currentFolderId ?: return@observe
val newCurrentFolderId = currentFolder?.id ?: return@observe
binding.customFoldersList.post {
customFoldersAdapter.setFolders(customFolders, newCurrentFolderId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import com.infomaniak.mail.ui.alertDialogs.DescriptionAlertDialog
import com.infomaniak.mail.ui.alertDialogs.InformationAlertDialog
import com.infomaniak.mail.ui.main.thread.AttachmentAdapter
import com.infomaniak.mail.ui.newMessage.NewMessageFragment.FieldType.*
import com.infomaniak.mail.ui.newMessage.NewMessageUtils.waitInitMediator
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.ImportationResult
import com.infomaniak.mail.utils.*
import com.infomaniak.mail.utils.ExternalUtils.findExternalRecipientForNewMessage
Expand Down Expand Up @@ -187,15 +186,12 @@ class NewMessageFragment : Fragment() {
observeExternals()
}

private fun observeExternals() {
waitInitMediator(
newMessageViewModel.initResult,
newMessageViewModel.mergedContacts,
).observe(viewLifecycleOwner) { (_, mergedContacts) ->
val externalMailFlagEnabled = newMessageViewModel.currentMailbox.externalMailFlagEnabled
private fun observeExternals() = with(newMessageViewModel) {
Utils.waitInitMediator(initResult, mergedContacts).observe(viewLifecycleOwner) { (_, mergedContacts) ->
val externalMailFlagEnabled = currentMailbox.externalMailFlagEnabled
val shouldWarnForExternal = externalMailFlagEnabled && !newMessageActivityArgs.arrivedFromExistingDraft
val emailDictionary = mergedContacts.second
val aliases = newMessageViewModel.currentMailbox.aliases
val aliases = currentMailbox.aliases

updateFields(shouldWarnForExternal, emailDictionary, aliases)
updateBanner(shouldWarnForExternal, emailDictionary, aliases)
Expand Down

This file was deleted.

17 changes: 17 additions & 0 deletions app/src/main/java/com/infomaniak/mail/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.infomaniak.mail.utils

import android.net.Uri
import androidx.core.net.toUri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import com.infomaniak.mail.data.models.Folder.FolderRole
import io.sentry.Sentry
import kotlinx.coroutines.*
Expand Down Expand Up @@ -81,4 +83,19 @@ object Utils {
NO_MAILBOX,
NO_VALID_MAILBOX,
}

fun <T1, T2> waitInitMediator(liveData1: LiveData<T1>, liveData2: LiveData<T2>): MediatorLiveData<Pair<T1, T2>> {

fun areLiveDataInitialized() = liveData1.isInitialized && liveData2.isInitialized

fun MediatorLiveData<Pair<T1, T2>>.postIfInit() {
@Suppress("UNCHECKED_CAST")
if (areLiveDataInitialized()) postValue((liveData1.value as T1) to (liveData2.value as T2))
}

return MediatorLiveData<Pair<T1, T2>>().apply {
addSource(liveData1) { postIfInit() }
addSource(liveData2) { postIfInit() }
}
}
}

0 comments on commit 26fd99b

Please sign in to comment.