From d446e6d99807eb8ebdebeab3ab1597aeb659cead Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 14 Oct 2024 12:18:26 +0200 Subject: [PATCH] Updated behavior of alerts top bar --- .../ui/main/viewmodel/MainViewModel.kt | 54 +++++++++++-------- .../main_activity_notification_top_bar.xml | 6 +-- 2 files changed, 34 insertions(+), 26 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 44da148ef..06a0c83a5 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 @@ -113,6 +113,8 @@ class MainViewModel @UiThread constructor() : ViewModel() { private var monitorAccount = false + private var nonDefaultAccountNotificationsCount = 0 + private val coreListener = object : CoreListenerStub() { @WorkerThread override fun onGlobalStateChanged(core: Core, state: GlobalState?, message: String) { @@ -128,6 +130,7 @@ class MainViewModel @UiThread constructor() : ViewModel() { removeAlert(SINGLE_CALL) atLeastOneCall.postValue(false) + // TODO: do not do it if nothing has changed! computeNonDefaultAccountNotificationsCount() } @@ -135,7 +138,14 @@ class MainViewModel @UiThread constructor() : ViewModel() { override fun onFirstCallStarted(core: Core) { Log.i("$TAG First call started, adding in-call 'alert'") updateCallAlert() - atLeastOneCall.postValue(true) + coreContext.postOnCoreThreadDelayed({ + if (core.callsNb > 0) { + Log.i("$TAG At least a call is active, showing 'alert' top bar") + atLeastOneCall.postValue(true) + } else { + Log.i("$TAG No call found, do not show 'alert' top bar") + } + }, 1000L) } @WorkerThread @@ -145,7 +155,7 @@ class MainViewModel @UiThread constructor() : ViewModel() { state: Call.State?, message: String ) { - Log.i("$TAG A call's state changed, updating alerts if needed") + Log.i("$TAG A call's state changed, updating 'alerts' if needed") if ( core.callsNb > 1 && ( LinphoneUtils.isCallEnding(call.state) || @@ -285,7 +295,9 @@ class MainViewModel @UiThread constructor() : ViewModel() { init { defaultAccountRegistrationFailed = false showAlert.value = false + atLeastOneCall.value = false maxAlertLevel.value = NONE + nonDefaultAccountNotificationsCount = 0 enableAccountMonitoring(true) coreContext.postOnCoreThread { core -> @@ -301,8 +313,8 @@ class MainViewModel @UiThread constructor() : ViewModel() { if (core.callsNb > 0) { updateCallAlert() + atLeastOneCall.postValue(true) } - atLeastOneCall.postValue(core.callsNb > 0) if (core.defaultAccount?.state == RegistrationState.Ok && !firstAccountRegistered) { triggerNativeAddressBookImport() @@ -400,23 +412,25 @@ class MainViewModel @UiThread constructor() : ViewModel() { @WorkerThread private fun computeNonDefaultAccountNotificationsCount() { - removeAlert(NON_DEFAULT_ACCOUNT_NOTIFICATIONS) - var count = 0 for (account in coreContext.core.accountList) { if (account == coreContext.core.defaultAccount) continue count += account.unreadChatMessageCount + account.missedCallsCount } - if (count > 0) { - val label = AppUtils.getStringWithPlural( - R.plurals.pending_notification_for_other_accounts, - count, - count.toString() - ) - addAlert(NON_DEFAULT_ACCOUNT_NOTIFICATIONS, label, forceUpdate = true) - Log.i("$TAG Found [$count] pending notifications for other account(s)") - } else { - Log.i("$TAG No pending notification found for other account(s)") + if (count != nonDefaultAccountNotificationsCount) { + if (count > 0) { + val label = AppUtils.getStringWithPlural( + R.plurals.pending_notification_for_other_accounts, + count, + count.toString() + ) + addAlert(NON_DEFAULT_ACCOUNT_NOTIFICATIONS, label, forceUpdate = true) + Log.i("$TAG Found [$count] pending notifications for other account(s)") + } else { + removeAlert(NON_DEFAULT_ACCOUNT_NOTIFICATIONS) + Log.i("$TAG No pending notification found for other account(s)") + } + nonDefaultAccountNotificationsCount = count } } @@ -523,14 +537,8 @@ class MainViewModel @UiThread constructor() : ViewModel() { } alertLabel.postValue(label) - 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 { + if (type < SINGLE_CALL) { + // Call alert is displayed using atLeastOnCall mutable, not showAlert Log.i("$TAG Alert top-bar is currently invisible, display it now") showAlert.postValue(true) } diff --git a/app/src/main/res/layout/main_activity_notification_top_bar.xml b/app/src/main/res/layout/main_activity_notification_top_bar.xml index 4762630eb..a1b9e3ebd 100644 --- a/app/src/main/res/layout/main_activity_notification_top_bar.xml +++ b/app/src/main/res/layout/main_activity_notification_top_bar.xml @@ -23,7 +23,7 @@ android:layout_marginStart="16dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" - android:visibility="@{viewModel.showAlert ? View.VISIBLE : View.GONE, default=gone}" + android:visibility="@{viewModel.showAlert || viewModel.atLeastOneCall ? View.VISIBLE : View.GONE, default=gone}" android:src="@{viewModel.alertIcon, default=@drawable/bell_simple}" android:contentDescription="@null" app:layout_constraintStart_toStartOf="parent" @@ -46,7 +46,7 @@ android:textSize="16sp" android:maxLines="1" android:ellipsize="end" - android:visibility="@{viewModel.showAlert ? View.VISIBLE : View.GONE, default=gone}" + android:visibility="@{viewModel.showAlert || viewModel.atLeastOneCall ? View.VISIBLE : View.GONE, default=gone}" app:layout_constraintEnd_toStartOf="@id/end_barrier" app:layout_constraintStart_toEndOf="@id/icon" app:layout_constraintTop_toTopOf="parent" @@ -69,7 +69,7 @@ android:text="@{viewModel.callsStatus, default=`Paused`}" android:textColor="@color/white" android:textSize="14sp" - android:visibility="@{viewModel.showAlert && viewModel.atLeastOneCall ? View.VISIBLE : View.GONE, default=gone}" + android:visibility="@{viewModel.atLeastOneCall ? View.VISIBLE : View.GONE, default=gone}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/label" app:layout_constraintTop_toTopOf="parent"