Fix issue where notification settings were slow to open.

This commit is contained in:
Greyson Parrelli 2023-09-06 16:23:14 -04:00
parent b3350b22b6
commit 61cdb48273
3 changed files with 24 additions and 7 deletions

View file

@ -16,19 +16,21 @@ import org.thoughtcrime.securesms.util.livedata.Store
class NotificationsSettingsViewModel(private val sharedPreferences: SharedPreferences) : ViewModel() {
private val store = Store(getState())
val state: LiveData<NotificationsSettingsState> = store.stateLiveData
init {
if (NotificationChannels.supported()) {
SignalStore.settings().messageNotificationSound = NotificationChannels.getInstance().messageRingtone
SignalStore.settings().isMessageVibrateEnabled = NotificationChannels.getInstance().messageVibrate
}
store.update { getState(calculateSlowNotifications = true) }
}
private val store = Store(getState())
val state: LiveData<NotificationsSettingsState> = store.stateLiveData
fun refresh() {
store.update { getState() }
store.update { getState(currentState = it) }
}
fun setMessageNotificationsEnabled(enabled: Boolean) {
@ -100,7 +102,12 @@ class NotificationsSettingsViewModel(private val sharedPreferences: SharedPrefer
refresh()
}
private fun getState(): NotificationsSettingsState = NotificationsSettingsState(
/**
* @param currentState If provided and [calculateSlowNotifications] = false, then we will copy the slow notification state from it
* @param calculateSlowNotifications If true, calculate the true slow notification state (this is not main-thread safe). Otherwise, it will copy from
* [currentState] or default to false.
*/
private fun getState(currentState: NotificationsSettingsState? = null, calculateSlowNotifications: Boolean = false): NotificationsSettingsState = NotificationsSettingsState(
messageNotificationsState = MessageNotificationsState(
notificationsEnabled = SignalStore.settings().isMessageNotificationsEnabled && canEnableNotifications(),
canEnableNotifications = canEnableNotifications(),
@ -112,7 +119,13 @@ class NotificationsSettingsViewModel(private val sharedPreferences: SharedPrefer
repeatAlerts = SignalStore.settings().messageNotificationsRepeatAlerts,
messagePrivacy = SignalStore.settings().messageNotificationsPrivacy.toString(),
priority = TextSecurePreferences.getNotificationPriority(ApplicationDependencies.getApplication()),
troubleshootNotifications = SlowNotificationHeuristics.isPotentiallyCausedByBatteryOptimizations() && SlowNotificationHeuristics.isHavingDelayedNotifications()
troubleshootNotifications = if (calculateSlowNotifications) {
SlowNotificationHeuristics.isPotentiallyCausedByBatteryOptimizations() && SlowNotificationHeuristics.isHavingDelayedNotifications()
} else if (currentState != null) {
currentState.messageNotificationsState.troubleshootNotifications
} else {
false
}
),
callNotificationsState = CallNotificationsState(
notificationsEnabled = SignalStore.settings().isCallNotificationsEnabled && canEnableNotifications(),

View file

@ -7,6 +7,7 @@ package org.thoughtcrime.securesms.notifications
import android.os.Build
import android.text.TextUtils
import androidx.annotation.WorkerThread
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.database.LocalMetricsDatabase
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
@ -85,6 +86,7 @@ object SlowNotificationHeuristics {
return true
}
@WorkerThread
@JvmStatic
fun isHavingDelayedNotifications(): Boolean {
if (!SignalStore.settings().isMessageNotificationsEnabled ||

View file

@ -6,6 +6,7 @@
package org.thoughtcrime.securesms.notifications
import android.os.Build
import androidx.annotation.WorkerThread
import androidx.lifecycle.ViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
@ -43,6 +44,7 @@ class SlowNotificationsViewModel : ViewModel() {
checkSubject.onNext(Unit)
}
@WorkerThread
private fun checkHeuristics(): Single<State> {
return Single.fromCallable {
var state = State.NONE