1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.wallpaper.picker.customization.data.repository 17 18 import android.app.WallpaperColors 19 import com.android.wallpaper.config.BaseFlags 20 import com.android.wallpaper.model.Screen 21 import com.android.wallpaper.picker.customization.data.content.WallpaperClient 22 import com.android.wallpaper.picker.customization.shared.model.WallpaperColorsModel 23 import javax.inject.Inject 24 import javax.inject.Singleton 25 import kotlinx.coroutines.flow.MutableStateFlow 26 import kotlinx.coroutines.flow.StateFlow 27 import kotlinx.coroutines.flow.asStateFlow 28 29 @Singleton 30 /** Repository class to keep track of WallpaperColors for the current wallpaper */ 31 class WallpaperColorsRepository 32 @Inject 33 constructor( 34 client: WallpaperClient, 35 ) { 36 37 private val isNewPickerUi = BaseFlags.get().isNewPickerUi() 38 39 private val _homeWallpaperColors = 40 if (isNewPickerUi) { 41 MutableStateFlow<WallpaperColorsModel>( 42 WallpaperColorsModel.Loaded(client.getWallpaperColors(Screen.HOME_SCREEN)) 43 ) 44 } else { 45 MutableStateFlow<WallpaperColorsModel>(WallpaperColorsModel.Loading) 46 } 47 48 /** WallpaperColors for the currently set home wallpaper */ 49 val homeWallpaperColors: StateFlow<WallpaperColorsModel> = _homeWallpaperColors.asStateFlow() 50 51 private val _lockWallpaperColors = 52 if (isNewPickerUi) { 53 MutableStateFlow<WallpaperColorsModel>( 54 WallpaperColorsModel.Loaded(client.getWallpaperColors(Screen.LOCK_SCREEN)) 55 ) 56 } else { 57 MutableStateFlow<WallpaperColorsModel>(WallpaperColorsModel.Loading) 58 } 59 /** WallpaperColors for the currently set lock wallpaper */ 60 val lockWallpaperColors: StateFlow<WallpaperColorsModel> = _lockWallpaperColors.asStateFlow() 61 setHomeWallpaperColorsnull62 fun setHomeWallpaperColors(colors: WallpaperColors?) { 63 _homeWallpaperColors.value = WallpaperColorsModel.Loaded(colors) 64 } 65 setLockWallpaperColorsnull66 fun setLockWallpaperColors(colors: WallpaperColors?) { 67 _lockWallpaperColors.value = WallpaperColorsModel.Loaded(colors) 68 } 69 } 70