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.shared.model
18
19 import com.android.systemui.log.table.TableLogBuffer
20 import com.android.systemui.util.kotlin.pairwiseBy
21 import kotlinx.coroutines.flow.Flow
22
23 @JvmInline
24 value class LinearBrightness(val floatValue: Float) {
25 fun clamp(min: LinearBrightness, max: LinearBrightness): LinearBrightness {
26 return if (floatValue < min.floatValue) {
27 min
28 } else if (floatValue > max.floatValue) {
29 max
30 } else {
31 this
32 }
33 }
34
35 val loggableString: String
36 get() = floatValue.formatBrightness()
37 }
38
Floatnull39 fun Float.formatBrightness(): String {
40 return "%.3f".format(this)
41 }
42
logDiffForTablenull43 internal fun Flow<LinearBrightness>.logDiffForTable(
44 tableLogBuffer: TableLogBuffer,
45 columnPrefix: String,
46 columnName: String,
47 initialValue: LinearBrightness?,
48 ): Flow<LinearBrightness> {
49 val initialValueFun = {
50 tableLogBuffer.logChange(
51 columnPrefix,
52 columnName,
53 initialValue?.loggableString,
54 isInitial = true
55 )
56 initialValue
57 }
58 return this.pairwiseBy(initialValueFun) { prevVal: LinearBrightness?, newVal: LinearBrightness
59 ->
60 if (prevVal != newVal) {
61 tableLogBuffer.logChange(columnPrefix, columnName, newVal.loggableString)
62 }
63 newVal
64 }
65 }
66