1 /*
2 * Copyright (C) 2022 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.settingslib.spa.widget.ui
18
19 import androidx.compose.foundation.interaction.MutableInteractionSource
20 import androidx.compose.foundation.layout.size
21 import androidx.compose.material.icons.Icons
22 import androidx.compose.material.icons.filled.Check
23 import androidx.compose.material.icons.filled.Close
24 import androidx.compose.material3.Icon
25 import androidx.compose.material3.Switch
26 import androidx.compose.material3.SwitchDefaults
27 import androidx.compose.runtime.Composable
28 import androidx.compose.runtime.remember
29 import androidx.compose.ui.Modifier
30 import com.android.settingslib.spa.framework.compose.contentDescription
31 import com.android.settingslib.spa.framework.theme.isSpaExpressiveEnabled
32 import com.android.settingslib.spa.framework.util.wrapOnSwitchWithLog
33
34 @Composable
SettingsSwitchnull35 internal fun SettingsSwitch(
36 checked: Boolean?,
37 changeable: () -> Boolean,
38 contentDescription: String? = null,
39 onCheckedChange: ((newChecked: Boolean) -> Unit)? = null,
40 interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
41 ) {
42 if (checked != null) {
43 Switch(
44 checked = checked,
45 onCheckedChange = wrapOnSwitchWithLog(onCheckedChange),
46 modifier = Modifier.contentDescription(contentDescription),
47 enabled = changeable(),
48 interactionSource = interactionSource,
49 thumbContent =
50 if (isSpaExpressiveEnabled) {
51 if (checked) {
<lambda>null52 {
53 Icon(
54 imageVector = Icons.Filled.Check,
55 contentDescription = null,
56 modifier = Modifier.size(SwitchDefaults.IconSize),
57 )
58 }
59 } else {
<lambda>null60 {
61 Icon(
62 imageVector = Icons.Filled.Close,
63 contentDescription = null,
64 modifier = Modifier.size(SwitchDefaults.IconSize),
65 )
66 }
67 }
68 } else null)
69 } else {
70 Switch(
71 checked = false,
72 onCheckedChange = null,
73 enabled = false,
74 interactionSource = interactionSource,
75 thumbContent =
76 if (isSpaExpressiveEnabled) {
<lambda>null77 {
78 Icon(
79 imageVector = Icons.Filled.Close,
80 contentDescription = null,
81 modifier = Modifier.size(SwitchDefaults.IconSize),
82 )
83 }
84 } else null
85 )
86 }
87 }
88