Skip to content

Commit

Permalink
Use the new networkstatus flow to know if we have internet or not
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Aug 6, 2024
1 parent 79818ce commit aaa44e6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Core
22 changes: 1 addition & 21 deletions app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,7 +193,6 @@ class MainActivity : BaseActivity() {
localSettings.accentColor.theme,
)

observeNetworkStatus()
observeDeletedMessages()
observeDeleteThreadTrigger()
observeDraftWorkerResults()
Expand Down Expand Up @@ -228,21 +223,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
16 changes: 15 additions & 1 deletion 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 @@ -100,7 +101,6 @@ class MainViewModel @Inject constructor(

// First boolean is the download status, second boolean is if the LoadMore button should be displayed
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 @@ -172,6 +172,19 @@ 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 = true
)

val isNetworkAvailableLiveData = isNetworkAvailable.asLiveData(Dispatchers.IO)

private var currentThreadsLiveJob: Job? = null

fun reassignCurrentThreadsLive() {
Expand Down Expand Up @@ -1080,5 +1093,6 @@ class MainViewModel @Inject constructor(
private val DEFAULT_SELECTED_FOLDER = FolderRole.INBOX
private const val REFRESH_DELAY = 2_000L // We add this delay because `etop` isn't always big enough.
private const val MAX_REFRESH_DELAY = 6_000L
private const val TIMEOUT_MS_NETWORK_AVAILABILITY_MS = 500L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
SwipeAction.NONE -> throw IllegalStateException("Cannot swipe on an action which is not set")
}

val shouldKeepItemBecauseOfNoConnection = isInternetAvailable.value == false
val shouldKeepItemBecauseOfNoConnection = mainViewModel.isNetworkAvailableLiveData.value == false

return shouldKeepItemBecauseOfAction || shouldKeepItemBecauseOfNoConnection
}
Expand Down Expand Up @@ -529,7 +529,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
}

private fun observeNetworkStatus() {
mainViewModel.isInternetAvailable.observe(viewLifecycleOwner) { isAvailable ->
mainViewModel.isNetworkAvailableLiveData.observe(viewLifecycleOwner) { isAvailable ->
TransitionManager.beginDelayedTransition(binding.root)
binding.noNetwork.isGone = isAvailable
if (!isAvailable) updateThreadsVisibility()
Expand Down Expand Up @@ -722,7 +722,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.isNetworkAvailableLiveData.value == true
val isBooting = currentThreadsCount == null && !isCursorNull && isNetworkConnected
val shouldDisplayThreadsView = isBooting || areThereThreads || isFilterEnabled || (isCursorNull && isNetworkConnected)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class DownloadAttachmentProgressDialog : DialogFragment() {
}

private fun popBackStackWithError() {
val title = if (mainViewModel.isInternetAvailable.value == true) R.string.anErrorHasOccurred else R.string.noConnection
// We have to use the Flow instead of the LiveData here since the mainViewModel is not the same instance as the one in the
// MainActivity. So, instead of observing the LiveData in order to make it "active", we use the Flow.
val title = if (mainViewModel.isNetworkAvailable.value) R.string.anErrorHasOccurred else R.string.noConnection
showSnackbar(title)
findNavController().popBackStack()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.infomaniak.mail.data.models.draft.Draft.DraftAction
import com.infomaniak.mail.databinding.ActivityNewMessageBinding
import com.infomaniak.mail.ui.BaseActivity
import com.infomaniak.mail.ui.LaunchActivity
import com.infomaniak.mail.ui.MainViewModel
import com.infomaniak.mail.ui.main.SnackbarManager
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.SentryDebug
Expand Down

0 comments on commit aaa44e6

Please sign in to comment.