From 45b0df31db45bdfa088961c83e2a97e85ff26062 Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Thu, 18 Jul 2024 11:12:15 +0200 Subject: [PATCH] Use the new networkstatus flow to know if we have internet or not --- Core | 2 +- .../com/infomaniak/mail/ui/MainActivity.kt | 23 ++----------------- .../com/infomaniak/mail/ui/MainViewModel.kt | 9 ++++++++ .../DownloadAttachmentProgressDialog.kt | 20 ++++++++++------ 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Core b/Core index b17b7106eb..e603e4881c 160000 --- a/Core +++ b/Core @@ -1 +1 @@ -Subproject commit b17b7106eb973b25484fcce181d2ad28e8f1f6be +Subproject commit e603e4881c80006b98ef24179eb21e2d1620db37 diff --git a/app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt b/app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt index 83ee34d535..676ba51ac9 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt @@ -31,19 +31,15 @@ import androidx.annotation.FloatRange import androidx.core.view.isVisible import androidx.drawerlayout.widget.DrawerLayout import androidx.lifecycle.Lifecycle.State -import androidx.lifecycle.distinctUntilChanged import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.NavDestination import androidx.navigation.fragment.NavHostFragment import androidx.work.Data import com.infomaniak.lib.core.MatomoCore.TrackerAction -import com.infomaniak.lib.core.networking.LiveDataNetworkStatus -import com.infomaniak.lib.core.utils.SentryLog +import com.infomaniak.lib.core.utils.* import com.infomaniak.lib.core.utils.Utils import com.infomaniak.lib.core.utils.Utils.toEnumOrThrow -import com.infomaniak.lib.core.utils.hasPermissions -import com.infomaniak.lib.core.utils.year import com.infomaniak.lib.stores.StoreUtils import com.infomaniak.lib.stores.StoreUtils.checkUpdateIsRequired import com.infomaniak.lib.stores.reviewmanagers.InAppReviewManager @@ -197,7 +193,6 @@ class MainActivity : BaseActivity() { localSettings.accentColor.theme, ) - observeNetworkStatus() observeDeletedMessages() observeDeleteThreadTrigger() observeDraftWorkerResults() @@ -208,6 +203,7 @@ class MainActivity : BaseActivity() { setupNavController() setupMenuDrawerCallbacks() + mainViewModel.observeNetworkStatus() mainViewModel.updateUserInfo() loadCurrentMailbox() @@ -228,21 +224,6 @@ class MainActivity : BaseActivity() { ) } - private fun observeNetworkStatus() { - LiveDataNetworkStatus(context = this).distinctUntilChanged().observe(this) { isAvailable -> - - SentryLog.d("Internet availability", if (isAvailable) "Available" else "Unavailable") - - Breadcrumb().apply { - category = "Network" - message = "Internet access is available : $isAvailable" - level = if (isAvailable) SentryLevel.INFO else SentryLevel.WARNING - }.also(Sentry::addBreadcrumb) - - mainViewModel.isInternetAvailable.value = isAvailable - } - } - private fun observeDeletedMessages() = with(mainViewModel) { deletedMessages.observe(owner = this@MainActivity) { if (it.isNotEmpty()) { diff --git a/app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt b/app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt index 47b312767a..1f4b503d27 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt @@ -24,6 +24,7 @@ import com.infomaniak.lib.core.utils.ApiErrorCode.Companion.translateError import com.infomaniak.lib.core.utils.DownloadManagerUtils import com.infomaniak.lib.core.utils.SentryLog import com.infomaniak.lib.core.utils.SingleLiveEvent +import com.infomaniak.lib.core.utils.networkStatusFlow import com.infomaniak.mail.MatomoMail.trackMultiSelectionEvent import com.infomaniak.mail.R import com.infomaniak.mail.data.api.ApiRepository @@ -185,6 +186,14 @@ class MainViewModel @Inject constructor( } } + fun observeNetworkStatus() = viewModelScope.launch(Dispatchers.IO) { + appContext.networkStatusFlow().collect { isAvailable -> + SentryLog.d("Internet availability", if (isAvailable) "Available" else "Unavailable") + + isInternetAvailable.postValue(isAvailable) + } + } + private fun observeFolderAndFilter() = MediatorLiveData>().apply { postValue(currentFolder.value to currentFilter.value!!) addSource(currentFolder) { postValue(it to value!!.second) } diff --git a/app/src/main/java/com/infomaniak/mail/ui/main/thread/actions/DownloadAttachmentProgressDialog.kt b/app/src/main/java/com/infomaniak/mail/ui/main/thread/actions/DownloadAttachmentProgressDialog.kt index 7a9055d1b7..f837c34790 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/main/thread/actions/DownloadAttachmentProgressDialog.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/main/thread/actions/DownloadAttachmentProgressDialog.kt @@ -22,26 +22,28 @@ import android.os.Bundle import android.view.KeyEvent import androidx.appcompat.content.res.AppCompatResources import androidx.fragment.app.DialogFragment -import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.infomaniak.lib.core.R import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar +import com.infomaniak.lib.core.utils.networkStatusFlow import com.infomaniak.lib.core.utils.setBackNavigationResult import com.infomaniak.mail.databinding.DialogDownloadProgressBinding -import com.infomaniak.mail.ui.MainViewModel import com.infomaniak.mail.utils.extensions.AttachmentExtensions import com.infomaniak.mail.utils.extensions.AttachmentExtensions.getIntentOrGoToPlayStore import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @AndroidEntryPoint class DownloadAttachmentProgressDialog : DialogFragment() { private val binding by lazy { DialogDownloadProgressBinding.inflate(layoutInflater) } private val navigationArgs: DownloadAttachmentProgressDialogArgs by navArgs() - private val mainViewModel: MainViewModel by activityViewModels() private val downloadAttachmentViewModel: DownloadAttachmentViewModel by viewModels() override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -75,9 +77,13 @@ class DownloadAttachmentProgressDialog : DialogFragment() { } } - private fun popBackStackWithError() { - val title = if (mainViewModel.isInternetAvailable.value == true) R.string.anErrorHasOccurred else R.string.noConnection - showSnackbar(title) - findNavController().popBackStack() + private fun popBackStackWithError() = lifecycleScope.launch(Dispatchers.IO) { + requireContext().networkStatusFlow().collect { isInternetAvailable -> + val title = if (isInternetAvailable) R.string.anErrorHasOccurred else R.string.noConnection + withContext(Dispatchers.Main) { + showSnackbar(title) + findNavController().popBackStack() + } + } } }