Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new networkstatus flow to know if we have internet or not #1972

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ 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.Utils
import com.infomaniak.lib.core.utils.Utils.toEnumOrThrow
Expand Down Expand Up @@ -76,7 +74,6 @@ import com.infomaniak.mail.utils.Utils.openShortcutHelp
import com.infomaniak.mail.utils.extensions.isUserAlreadySynchronized
import com.infomaniak.mail.workers.DraftsActionsWorker
import dagger.hilt.android.AndroidEntryPoint
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -193,7 +190,6 @@ class MainActivity : BaseActivity() {
localSettings.accentColor.theme,
)

observeNetworkStatus()
observeDeletedMessages()
observeDeleteThreadTrigger()
observeDraftWorkerResults()
Expand Down Expand Up @@ -224,21 +220,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()) {
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.infomaniak.mail.ui
import android.app.Application
import androidx.lifecycle.*
import com.infomaniak.lib.core.models.ApiResponse
import com.infomaniak.lib.core.networking.NetworkAvailability
import com.infomaniak.lib.core.utils.ApiErrorCode.Companion.translateError
import com.infomaniak.lib.core.utils.DownloadManagerUtils
import com.infomaniak.lib.core.utils.SentryLog
Expand Down Expand Up @@ -99,7 +100,6 @@ class MainViewModel @Inject constructor(
private var refreshEverythingJob: Job? = null

val isDownloadingChanges: MutableLiveData<Boolean> = MutableLiveData(false)
val isInternetAvailable = MutableLiveData<Boolean>()
val isMovedToNewFolder = SingleLiveEvent<Boolean>()
val toggleLightThemeForMessage = SingleLiveEvent<Message>()
val deletedMessages = SingleLiveEvent<Set<String>>()
Expand Down Expand Up @@ -137,11 +137,13 @@ class MainViewModel @Inject constructor(
}.asLiveData(ioCoroutineContext)

val defaultFoldersLive = _currentMailboxObjectId.filterNotNull().flatMapLatest {
folderController.getMenuDrawerDefaultFoldersAsync().map { it.list.flattenFolderChildrenAndRemoveMessages(dismissHiddenChildren = true) }
folderController.getMenuDrawerDefaultFoldersAsync()
.map { it.list.flattenFolderChildrenAndRemoveMessages(dismissHiddenChildren = true) }
}.asLiveData(ioCoroutineContext)

val customFoldersLive = _currentMailboxObjectId.filterNotNull().flatMapLatest {
folderController.getMenuDrawerCustomFoldersAsync().map { it.list.flattenFolderChildrenAndRemoveMessages(dismissHiddenChildren = true) }
folderController.getMenuDrawerCustomFoldersAsync()
.map { it.list.flattenFolderChildrenAndRemoveMessages(dismissHiddenChildren = true) }
}.asLiveData(ioCoroutineContext)

val currentQuotasLive = _currentMailboxObjectId.flatMapLatest {
Expand Down Expand Up @@ -169,6 +171,17 @@ class MainViewModel @Inject constructor(

val currentThreadsLive = MutableLiveData<ResultsChange<Thread>>()

val isNetworkAvailable = NetworkAvailability(appContext).isNetworkAvailable
.mapLatest {
SentryLog.d("Internet availability", if (it) "Available" else "Unavailable")
it
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = null,
)

private var currentThreadsLiveJob: Job? = null

fun reassignCurrentThreadsLive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.core.view.isVisible
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.viewModels
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -81,6 +82,7 @@ import com.infomaniak.mail.utils.extensions.*
import dagger.hilt.android.AndroidEntryPoint
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.launch
import java.util.Date
import javax.inject.Inject
import com.infomaniak.lib.core.R as RCore
Expand Down Expand Up @@ -495,8 +497,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
SwipeAction.NONE -> error("Cannot swipe on an action which is not set")
}

val shouldKeepItemBecauseOfNoConnection = isInternetAvailable.value == false

val shouldKeepItemBecauseOfNoConnection = isNetworkAvailable.value == false
return shouldKeepItemBecauseOfAction || shouldKeepItemBecauseOfNoConnection
}

Expand Down Expand Up @@ -529,10 +530,14 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
}

private fun observeNetworkStatus() {
tevincent marked this conversation as resolved.
Show resolved Hide resolved
mainViewModel.isInternetAvailable.observe(viewLifecycleOwner) { isAvailable ->
TransitionManager.beginDelayedTransition(binding.root)
binding.noNetwork.isGone = isAvailable
if (!isAvailable) updateThreadsVisibility()
viewLifecycleOwner.lifecycleScope.launch {
mainViewModel.isNetworkAvailable.collect { isNetworkAvailable ->
if (isNetworkAvailable != null) {
TransitionManager.beginDelayedTransition(binding.root)
binding.noNetwork.isGone = isNetworkAvailable
if (!isNetworkAvailable) updateThreadsVisibility()
}
}
}
}

Expand Down Expand Up @@ -731,7 +736,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
val areThereThreads = (currentThreadsCount ?: 0) > 0
val isFilterEnabled = mainViewModel.currentFilter.value != ThreadFilter.ALL
val isCursorNull = currentFolderCursor == null
val isNetworkConnected = mainViewModel.isInternetAvailable.value == true
val isNetworkConnected = mainViewModel.isNetworkAvailable.value == true
val isBooting = currentThreadsCount == null && !isCursorNull && isNetworkConnected
val isWaitingFirstThreads = isCursorNull && isNetworkConnected
val shouldDisplayThreadsView = isBooting || isWaitingFirstThreads || areThereThreads || isFilterEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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
Expand All @@ -35,6 +36,8 @@ 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.flow.first
import kotlinx.coroutines.launch

@AndroidEntryPoint
class DownloadAttachmentProgressDialog : DialogFragment() {
Expand Down Expand Up @@ -76,8 +79,11 @@ class DownloadAttachmentProgressDialog : DialogFragment() {
}

private fun popBackStackWithError() {
tevincent marked this conversation as resolved.
Show resolved Hide resolved
val title = if (mainViewModel.isInternetAvailable.value == true) R.string.anErrorHasOccurred else R.string.noConnection
showSnackbar(title)
findNavController().popBackStack()
viewLifecycleOwner.lifecycleScope.launch {
mainViewModel.isNetworkAvailable.first { it != null }?.let { isNetworkAvailable ->
showSnackbar(title = if (isNetworkAvailable) R.string.anErrorHasOccurred else R.string.noConnection)
findNavController().popBackStack()
}
}
}
}
Loading