Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b24d38e authored by Ioana Alexandru's avatar Ioana Alexandru Committed by Android (Google) Code Review
Browse files

Merge "Make NotificationSettingsRepository hold individual settings." into main

parents cd5f79d2 bdd5448d
Loading
Loading
Loading
Loading
+9 −26
Original line number Diff line number Diff line
@@ -17,56 +17,39 @@
package com.android.systemui.shared.notifications.data.repository

import android.provider.Settings
import com.android.systemui.shared.notifications.shared.model.NotificationSettingsModel
import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext

/** Provides access to state related to notifications. */
/** Provides access to state related to notification settings. */
class NotificationSettingsRepository(
    scope: CoroutineScope,
    private val backgroundDispatcher: CoroutineDispatcher,
    private val secureSettingsRepository: SecureSettingsRepository,
) {
    /** The current state of the notification setting. */
    val settings: SharedFlow<NotificationSettingsModel> =
    val isShowNotificationsOnLockScreenEnabled: StateFlow<Boolean> =
        secureSettingsRepository
            .intSetting(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
            )
            .map { lockScreenShowNotificationsInt ->
                NotificationSettingsModel(
                    isShowNotificationsOnLockScreenEnabled = lockScreenShowNotificationsInt == 1,
                )
            }
            .shareIn(
            .map { it == 1 }
            .stateIn(
                scope = scope,
                started = SharingStarted.WhileSubscribed(),
                replay = 1,
                initialValue = false,
            )

    suspend fun getSettings(): NotificationSettingsModel {
        return withContext(backgroundDispatcher) {
            NotificationSettingsModel(
                isShowNotificationsOnLockScreenEnabled =
                    secureSettingsRepository.get(
                        name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                        defaultValue = 0,
                    ) == 1
            )
        }
    }

    suspend fun setSettings(model: NotificationSettingsModel) {
    suspend fun setShowNotificationsOnLockscreenEnabled(enabled: Boolean) {
        withContext(backgroundDispatcher) {
            secureSettingsRepository.set(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                value = if (model.isShowNotificationsOnLockScreenEnabled) 1 else 0,
                value = if (enabled) 1 else 0,
            )
        }
    }
+10 −19
Original line number Diff line number Diff line
@@ -17,32 +17,23 @@
package com.android.systemui.shared.notifications.domain.interactor

import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository
import com.android.systemui.shared.notifications.shared.model.NotificationSettingsModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

/** Encapsulates business logic for interacting with notification settings. */
class NotificationSettingsInteractor(
    private val repository: NotificationSettingsRepository,
) {
    /** The current state of the notification setting. */
    val settings: Flow<NotificationSettingsModel> = repository.settings
    /** Should notifications be visible on the lockscreen? */
    val isShowNotificationsOnLockScreenEnabled: StateFlow<Boolean> =
        repository.isShowNotificationsOnLockScreenEnabled

    /** Toggles the setting to show or hide notifications on the lock screen. */
    suspend fun toggleShowNotificationsOnLockScreenEnabled() {
        val currentModel = repository.getSettings()
        setSettings(
            currentModel.copy(
                isShowNotificationsOnLockScreenEnabled =
                    !currentModel.isShowNotificationsOnLockScreenEnabled,
            )
        )
    }

    suspend fun setSettings(model: NotificationSettingsModel) {
        repository.setSettings(model)
    suspend fun setShowNotificationsOnLockscreenEnabled(enabled: Boolean) {
        repository.setShowNotificationsOnLockscreenEnabled(enabled)
    }

    suspend fun getSettings(): NotificationSettingsModel {
        return repository.getSettings()
    /** Toggles the setting to show or hide notifications on the lock screen. */
    suspend fun toggleShowNotificationsOnLockscreenEnabled() {
        val current = repository.isShowNotificationsOnLockScreenEnabled.value
        repository.setShowNotificationsOnLockscreenEnabled(!current)
    }
}
+0 −24
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.systemui.shared.notifications.shared.model

/** Models notification settings. */
data class NotificationSettingsModel(
    /** Whether notifications are shown on the lock screen. */
    val isShowNotificationsOnLockScreenEnabled: Boolean = false,
)
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.shared.notifications.data.repository

import android.provider.Settings
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.shared.settings.data.repository.FakeSecureSettingsRepository
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@SmallTest
@RunWith(JUnit4::class)
class NotificationSettingsRepositoryTest : SysuiTestCase() {

    private lateinit var underTest: NotificationSettingsRepository

    private lateinit var testScope: TestScope
    private lateinit var secureSettingsRepository: FakeSecureSettingsRepository

    @Before
    fun setUp() {
        val testDispatcher = StandardTestDispatcher()
        testScope = TestScope(testDispatcher)
        secureSettingsRepository = FakeSecureSettingsRepository()

        underTest =
            NotificationSettingsRepository(
                scope = testScope.backgroundScope,
                backgroundDispatcher = testDispatcher,
                secureSettingsRepository = secureSettingsRepository,
            )
    }

    @Test
    fun testGetIsShowNotificationsOnLockscreenEnabled() =
        testScope.runTest {
            val showNotifs by collectLastValue(underTest.isShowNotificationsOnLockScreenEnabled)

            secureSettingsRepository.set(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                value = 1,
            )
            assertThat(showNotifs).isEqualTo(true)

            secureSettingsRepository.set(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                value = 0,
            )
            assertThat(showNotifs).isEqualTo(false)
        }

    @Test
    fun testSetIsShowNotificationsOnLockscreenEnabled() =
        testScope.runTest {
            val showNotifs by collectLastValue(underTest.isShowNotificationsOnLockScreenEnabled)

            underTest.setShowNotificationsOnLockscreenEnabled(true)
            assertThat(showNotifs).isEqualTo(true)

            underTest.setShowNotificationsOnLockscreenEnabled(false)
            assertThat(showNotifs).isEqualTo(false)
        }
}