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

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

Merge "Move NotificationsInteractor & related repos to shared." into main

parents 8ec9b4f1 c2d97a5f
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -56,8 +56,6 @@ import com.android.customization.picker.grid.data.repository.GridRepositoryImpl
import com.android.customization.picker.grid.domain.interactor.GridInteractor
import com.android.customization.picker.grid.domain.interactor.GridSnapshotRestorer
import com.android.customization.picker.grid.ui.viewmodel.GridScreenViewModel
import com.android.customization.picker.notifications.data.repository.NotificationsRepository
import com.android.customization.picker.notifications.domain.interactor.NotificationsInteractor
import com.android.customization.picker.notifications.domain.interactor.NotificationsSnapshotRestorer
import com.android.customization.picker.notifications.ui.viewmodel.NotificationSectionViewModel
import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
@@ -67,6 +65,8 @@ import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQui
import com.android.systemui.shared.clocks.ClockRegistry
import com.android.systemui.shared.customization.data.content.CustomizationProviderClient
import com.android.systemui.shared.customization.data.content.CustomizationProviderClientImpl
import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository
import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor
import com.android.wallpaper.config.BaseFlags
import com.android.wallpaper.module.CustomizationSections
import com.android.wallpaper.module.FragmentFactory
@@ -91,6 +91,7 @@ open class ThemePickerInjector
internal constructor(
    @MainDispatcher private val mainScope: CoroutineScope,
    @MainDispatcher private val mainDispatcher: CoroutineDispatcher,
    @BackgroundDispatcher private val bgScope: CoroutineScope,
    @BackgroundDispatcher private val bgDispatcher: CoroutineDispatcher,
    private val userEventLogger: ThemesUserEventLogger,
) : WallpaperPicker2Injector(mainScope, bgDispatcher, userEventLogger), CustomizationInjector {
@@ -110,7 +111,7 @@ internal constructor(
    private var clockCarouselViewModelFactory: ClockCarouselViewModel.Factory? = null
    private var clockViewFactory: ClockViewFactory? = null
    private var clockPickerSnapshotRestorer: ClockPickerSnapshotRestorer? = null
    private var notificationsInteractor: NotificationsInteractor? = null
    private var notificationSettingsInteractor: NotificationSettingsInteractor? = null
    private var notificationSectionViewModelFactory: NotificationSectionViewModel.Factory? = null
    private var colorPickerInteractor: ColorPickerInteractor? = null
    private var colorPickerViewModelFactory: ColorPickerViewModel.Factory? = null
@@ -297,19 +298,17 @@ internal constructor(

    private fun getNotificationsInteractor(
        context: Context,
    ): NotificationsInteractor {
        val appContext = context.applicationContext
        return notificationsInteractor
            ?: NotificationsInteractor(
    ): NotificationSettingsInteractor {
        return notificationSettingsInteractor
            ?: NotificationSettingsInteractor(
                    repository =
                        NotificationsRepository(
                        NotificationSettingsRepository(
                            scope = getApplicationCoroutineScope(),
                            backgroundDispatcher = bgDispatcher,
                            secureSettingsRepository = getSecureSettingsRepository(context),
                        ),
                    snapshotRestorer = { getNotificationsSnapshotRestorer(appContext) },
                )
                .also { notificationsInteractor = it }
                .also { notificationSettingsInteractor = it }
    }

    private fun getNotificationsSnapshotRestorer(context: Context): NotificationsSnapshotRestorer {
@@ -319,6 +318,7 @@ internal constructor(
                        getNotificationsInteractor(
                            context = context,
                        ),
                    backgroundScope = bgScope,
                )
                .also { notificationsSnapshotRestorer = it }
    }
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import com.android.customization.picker.clock.shared.ClockSize
import com.android.customization.picker.clock.shared.model.ClockMetadataModel
import com.android.systemui.plugins.clocks.ClockMetadata
import com.android.systemui.shared.clocks.ClockRegistry
import com.android.wallpaper.settings.data.repository.SecureSettingsRepository
import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
+0 −74
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.customization.picker.notifications.data.repository

import android.provider.Settings
import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel
import com.android.wallpaper.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.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.withContext

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

    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) {
        withContext(backgroundDispatcher) {
            secureSettingsRepository.set(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                value = if (model.isShowNotificationsOnLockScreenEnabled) 1 else 0,
            )
        }
    }
}
+0 −52
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.customization.picker.notifications.domain.interactor

import com.android.customization.picker.notifications.data.repository.NotificationsRepository
import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel
import javax.inject.Provider
import kotlinx.coroutines.flow.Flow

/** Encapsulates business logic for interacting with notifications. */
class NotificationsInteractor(
    private val repository: NotificationsRepository,
    private val snapshotRestorer: Provider<NotificationsSnapshotRestorer>,
) {
    /** The current state of the notification setting. */
    val settings: Flow<NotificationSettingsModel> = repository.settings

    /** 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)
        snapshotRestorer.get().storeSnapshot(model)
    }

    suspend fun getSettings(): NotificationSettingsModel {
        return repository.getSettings()
    }
}
+9 −4
Original line number Diff line number Diff line
@@ -12,24 +12,28 @@
 * 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.customization.picker.notifications.domain.interactor

import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel
import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor
import com.android.systemui.shared.notifications.shared.model.NotificationSettingsModel
import com.android.wallpaper.picker.di.modules.BackgroundDispatcher
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore
import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/** Handles state restoration for notification settings. */
class NotificationsSnapshotRestorer(
    private val interactor: NotificationsInteractor,
    private val interactor: NotificationSettingsInteractor,
    @BackgroundDispatcher private val backgroundScope: CoroutineScope,
) : SnapshotRestorer {

    private var snapshotStore: SnapshotStore = SnapshotStore.NOOP

    fun storeSnapshot(model: NotificationSettingsModel) {
    private fun storeSnapshot(model: NotificationSettingsModel) {
        snapshotStore.store(snapshot(model))
    }

@@ -37,6 +41,7 @@ class NotificationsSnapshotRestorer(
        store: SnapshotStore,
    ): RestorableSnapshot {
        snapshotStore = store
        backgroundScope.launch { interactor.settings.collect { model -> storeSnapshot(model) } }
        return snapshot(interactor.getSettings())
    }

Loading