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.systemui.brightness.ui.compose
18
19 import androidx.compose.animation.core.animateIntAsState
20 import androidx.compose.foundation.clickable
21 import androidx.compose.foundation.layout.fillMaxWidth
22 import androidx.compose.foundation.layout.size
23 import androidx.compose.material3.MaterialTheme
24 import androidx.compose.material3.Text
25 import androidx.compose.runtime.Composable
26 import androidx.compose.runtime.getValue
27 import androidx.compose.runtime.mutableIntStateOf
28 import androidx.compose.runtime.remember
29 import androidx.compose.runtime.rememberCoroutineScope
30 import androidx.compose.runtime.setValue
31 import androidx.compose.ui.Modifier
32 import androidx.compose.ui.res.stringResource
33 import androidx.compose.ui.unit.dp
34 import androidx.lifecycle.compose.collectAsStateWithLifecycle
35 import com.android.compose.PlatformSlider
36 import com.android.systemui.brightness.shared.model.GammaBrightness
37 import com.android.systemui.brightness.ui.viewmodel.BrightnessSliderViewModel
38 import com.android.systemui.brightness.ui.viewmodel.Drag
39 import com.android.systemui.common.shared.model.Icon
40 import com.android.systemui.common.shared.model.Text
41 import com.android.systemui.common.ui.compose.Icon
42 import com.android.systemui.utils.PolicyRestriction
43 import kotlinx.coroutines.launch
44
45 @Composable
46 private fun BrightnessSlider(
47 gammaValue: Int,
48 valueRange: IntRange,
49 label: Text.Resource,
50 icon: Icon,
51 restriction: PolicyRestriction,
52 onRestrictedClick: (PolicyRestriction.Restricted) -> Unit,
53 onDrag: (Int) -> Unit,
54 onStop: (Int) -> Unit,
55 modifier: Modifier = Modifier,
56 formatter: (Int) -> String = { "$it" },
57 ) {
<lambda>null58 var value by remember(gammaValue) { mutableIntStateOf(gammaValue) }
59 val animatedValue by
60 animateIntAsState(targetValue = value, label = "BrightnessSliderAnimatedValue")
61 val floatValueRange = valueRange.first.toFloat()..valueRange.last.toFloat()
62 val isRestricted = restriction is PolicyRestriction.Restricted
63
64 PlatformSlider(
65 value = animatedValue.toFloat(),
66 valueRange = floatValueRange,
67 enabled = !isRestricted,
<lambda>null68 onValueChange = {
69 if (!isRestricted) {
70 value = it.toInt()
71 onDrag(value)
72 }
73 },
<lambda>null74 onValueChangeFinished = {
75 if (!isRestricted) {
76 onStop(value)
77 }
78 },
79 modifier =
80 modifier.clickable(
81 enabled = isRestricted,
<lambda>null82 ) {
83 if (restriction is PolicyRestriction.Restricted) {
84 onRestrictedClick(restriction)
85 }
86 },
isDraggingnull87 icon = { isDragging ->
88 if (isDragging) {
89 Text(text = formatter(value))
90 } else {
91 Icon(modifier = Modifier.size(24.dp), icon = icon)
92 }
93 },
<lambda>null94 label = {
95 Text(
96 text = stringResource(id = label.res),
97 style = MaterialTheme.typography.titleMedium,
98 maxLines = 1,
99 )
100 },
101 )
102 }
103
104 @Composable
BrightnessSliderContainernull105 fun BrightnessSliderContainer(
106 viewModel: BrightnessSliderViewModel,
107 modifier: Modifier = Modifier,
108 ) {
109 val state by viewModel.currentBrightness.collectAsStateWithLifecycle()
110 val gamma = state.value
111 val coroutineScope = rememberCoroutineScope()
112 val restriction by
113 viewModel.policyRestriction.collectAsStateWithLifecycle(
114 initialValue = PolicyRestriction.NoRestriction
115 )
116
117 BrightnessSlider(
118 gammaValue = gamma,
119 valueRange = viewModel.minBrightness.value..viewModel.maxBrightness.value,
120 label = viewModel.label,
121 icon = viewModel.icon,
122 restriction = restriction,
123 onRestrictedClick = viewModel::showPolicyRestrictionDialog,
124 onDrag = { coroutineScope.launch { viewModel.onDrag(Drag.Dragging(GammaBrightness(it))) } },
125 onStop = { coroutineScope.launch { viewModel.onDrag(Drag.Stopped(GammaBrightness(it))) } },
126 modifier = modifier.fillMaxWidth(),
127 formatter = viewModel::formatValue,
128 )
129 }
130