Should prevent 'no network' alert over orange background (instead of red) in main activity due to race condition

This commit is contained in:
Sylvain Berfini 2024-09-02 14:35:03 +02:00
parent 49c3e68f84
commit b4a754db53

View file

@ -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<Pair<Int, String>>()
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)
}
}
}