1 /* <lambda>null2 * Copyright (C) 2024 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 */ 17 18 package com.android.customization.picker.grid.data.repository 19 20 import android.graphics.drawable.Drawable 21 import com.android.customization.model.grid.GridOptionModel 22 import com.android.customization.model.grid.ShapeGridManager 23 import com.android.customization.model.grid.ShapeOptionModel 24 import com.android.wallpaper.picker.di.modules.BackgroundDispatcher 25 import javax.inject.Inject 26 import javax.inject.Singleton 27 import kotlinx.coroutines.CoroutineDispatcher 28 import kotlinx.coroutines.CoroutineScope 29 import kotlinx.coroutines.flow.Flow 30 import kotlinx.coroutines.flow.MutableStateFlow 31 import kotlinx.coroutines.flow.StateFlow 32 import kotlinx.coroutines.flow.asStateFlow 33 import kotlinx.coroutines.flow.map 34 import kotlinx.coroutines.launch 35 import kotlinx.coroutines.withContext 36 37 @Singleton 38 class ShapeGridRepository 39 @Inject 40 constructor( 41 private val manager: ShapeGridManager, 42 @BackgroundDispatcher private val bgScope: CoroutineScope, 43 @BackgroundDispatcher private val bgDispatcher: CoroutineDispatcher, 44 ) { 45 46 private val _shapeOptions = MutableStateFlow<List<ShapeOptionModel>?>(null) 47 private val _gridOptions = MutableStateFlow<List<GridOptionModel>?>(null) 48 49 init { 50 bgScope.launch { 51 _gridOptions.value = manager.getGridOptions() 52 _shapeOptions.value = manager.getShapeOptions() 53 } 54 } 55 56 val shapeOptions: StateFlow<List<ShapeOptionModel>?> = _shapeOptions.asStateFlow() 57 58 val selectedShapeOption: Flow<ShapeOptionModel?> = 59 shapeOptions.map { shapeOptions -> shapeOptions?.firstOrNull { it.isCurrent } } 60 61 val gridOptions: StateFlow<List<GridOptionModel>?> = _gridOptions.asStateFlow() 62 63 val selectedGridOption: Flow<GridOptionModel?> = 64 gridOptions.map { gridOptions -> gridOptions?.firstOrNull { it.isCurrent } } 65 66 suspend fun applySelectedOption(shapeKey: String, gridKey: String) = 67 withContext(bgDispatcher) { 68 manager.applyShapeGridOption(shapeKey, gridKey) 69 // After applying, we should query and update shape and grid options again. 70 _gridOptions.value = manager.getGridOptions() 71 _shapeOptions.value = manager.getShapeOptions() 72 } 73 74 fun getGridOptionDrawable(iconId: Int): Drawable? { 75 return manager.getGridOptionDrawable(iconId) 76 } 77 } 78