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 package com.android.customization.picker.settings.ui.viewmodel 18 19 import androidx.lifecycle.ViewModel 20 import androidx.lifecycle.ViewModelProvider 21 import com.android.customization.picker.settings.domain.interactor.ColorContrastSectionInteractor 22 import com.android.themepicker.R 23 import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon 24 import com.android.wallpaper.picker.common.text.ui.viewmodel.Text 25 import javax.inject.Inject 26 import javax.inject.Singleton 27 import kotlinx.coroutines.flow.Flow 28 import kotlinx.coroutines.flow.map 29 30 class ColorContrastSectionViewModel 31 private constructor( 32 colorContrastSectionInteractor: ColorContrastSectionInteractor, 33 ) : ViewModel() { 34 35 val summary: Flow<ColorContrastSectionDataViewModel> = 36 colorContrastSectionInteractor.contrast.map { contrastValue -> 37 when (contrastValue) { 38 ContrastValue.STANDARD.value -> 39 ColorContrastSectionDataViewModel( 40 Text.Resource(R.string.color_contrast_default_title), 41 Icon.Resource( 42 res = R.drawable.ic_contrast_standard, 43 contentDescription = null, 44 ) 45 ) 46 ContrastValue.MEDIUM.value -> 47 ColorContrastSectionDataViewModel( 48 Text.Resource(R.string.color_contrast_medium_title), 49 Icon.Resource( 50 res = R.drawable.ic_contrast_medium, 51 contentDescription = null, 52 ) 53 ) 54 ContrastValue.HIGH.value -> 55 ColorContrastSectionDataViewModel( 56 Text.Resource(R.string.color_contrast_high_title), 57 Icon.Resource( 58 res = R.drawable.ic_contrast_high, 59 contentDescription = null, 60 ) 61 ) 62 else -> { 63 println("Invalid contrast value: $contrastValue") 64 throw IllegalArgumentException("Invalid contrast value") 65 } 66 } 67 } 68 69 enum class ContrastValue(val value: Float) { 70 STANDARD(0f), 71 MEDIUM(0.5f), 72 HIGH(1f) 73 } 74 75 @Singleton 76 class Factory 77 @Inject 78 constructor( 79 private val colorContrastSectionInteractor: ColorContrastSectionInteractor, 80 ) : ViewModelProvider.Factory { 81 override fun <T : ViewModel> create(modelClass: Class<T>): T { 82 @Suppress("UNCHECKED_CAST") 83 return ColorContrastSectionViewModel( 84 colorContrastSectionInteractor = colorContrastSectionInteractor, 85 ) 86 as T 87 } 88 } 89 } 90