From b4a754db5394361f1403a192daeb330a519e009a Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 2 Sep 2024 14:35:03 +0200 Subject: [PATCH] Should prevent 'no network' alert over orange background (instead of red) in main activity due to race condition --- .../ui/main/viewmodel/MainViewModel.kt | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt b/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt index 82d064e89..839922093 100644 --- a/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt +++ b/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt @@ -29,12 +29,7 @@ import androidx.core.content.ContextCompat import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.cancelAndJoin -import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R @@ -114,8 +109,6 @@ class MainViewModel @UiThread constructor() : ViewModel() { private val alertsList = arrayListOf>() - private var alertJob: Job? = null - private var firstAccountRegistered: Boolean = false private var monitorAccount = false @@ -369,7 +362,7 @@ class MainViewModel @UiThread constructor() : ViewModel() { coreContext.postOnCoreThread { Log.i("$TAG User closed alerts top bar, clearing alerts") - cancelAlertJob() + showAlert.postValue(false) alertsList.clear() updateDisplayedAlert() } @@ -463,7 +456,7 @@ class MainViewModel @UiThread constructor() : ViewModel() { it.first == type } if (found == null || forceUpdate) { - cancelAlertJob() + showAlert.postValue(false) if (found != null) { alertsList.remove(found) } @@ -483,7 +476,7 @@ class MainViewModel @UiThread constructor() : ViewModel() { it.first == type } if (found != null) { - cancelAlertJob() + showAlert.postValue(false) Log.i("$TAG Removing alert with type [$type]") alertsList.remove(found) updateDisplayedAlert() @@ -492,16 +485,6 @@ class MainViewModel @UiThread constructor() : ViewModel() { } } - @WorkerThread - private fun cancelAlertJob() { - viewModelScope.launch { - withContext(Dispatchers.Main) { - alertJob?.cancelAndJoin() - alertJob = null - } - } - } - @WorkerThread private fun updateDisplayedAlert() { // Sort alerts by priority @@ -538,17 +521,16 @@ class MainViewModel @UiThread constructor() : ViewModel() { if (showAlert.value == true) { Log.i("$TAG Alert top-bar is already visible") } else { - Log.i("$TAG Alert top-bar is currently invisible, starting job to display it") - coreContext.postOnMainThread { - val delayMs = if (type == SINGLE_CALL) 1000L else 0L - alertJob = viewModelScope.launch { - withContext(Dispatchers.IO) { - delay(delayMs) - withContext(Dispatchers.Main) { - showAlert.value = true - } + if (type == SINGLE_CALL) { + Log.i("$TAG Alert top-bar is currently invisible, displaying it in a second") + coreContext.postOnCoreThreadDelayed({ + if (maxAlertLevel.value != NONE) { + showAlert.postValue(true) } - } + }, 1000L) + } else { + Log.i("$TAG Alert top-bar is currently invisible, display it now") + showAlert.postValue(true) } } }