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 Jul 19, 2024
1 parent 4a6d439 commit 78f6c2d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
23 changes: 2 additions & 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 All @@ -208,6 +203,7 @@ class MainActivity : BaseActivity() {
setupNavController()
setupMenuDrawerCallbacks()

mainViewModel.observeNetworkStatus()
mainViewModel.updateUserInfo()

loadCurrentMailbox()
Expand All @@ -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()) {
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,9 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.notifications.ResultsChange
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.util.Date
Expand Down Expand Up @@ -185,6 +189,20 @@ class MainViewModel @Inject constructor(
}
}

fun observeNetworkStatus() = viewModelScope.launch(Dispatchers.IO) {
appContext.networkStatusFlow().collect { 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)

isInternetAvailable.postValue(isAvailable)
}
}

private fun observeFolderAndFilter() = MediatorLiveData<Pair<Folder?, ThreadFilter>>().apply {
postValue(currentFolder.value to currentFilter.value!!)
addSource(currentFolder) { postValue(it to value!!.second) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,8 +78,14 @@ class DownloadAttachmentProgressDialog : DialogFragment() {
}

private fun popBackStackWithError() {
val title = if (mainViewModel.isInternetAvailable.value == true) R.string.anErrorHasOccurred else R.string.noConnection
showSnackbar(title)
findNavController().popBackStack()
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()
}
}
}
}
}

0 comments on commit 78f6c2d

Please sign in to comment.