Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 32e39c07 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[Pod] Move Diffable extension functions to log.table pod.

For now, leaves the Kairos Diffable extension functions in
SystemUI-core, because I don't think the log.table pod should have a
dependency on Kairos.

Bug: 307607958
Test: m SystemUI-core, and build with studio
Flag: EXEMPT refactor
Change-Id: Ic3f96a03542bf5f8d8cbcb6ec9a5261dacd50d2c
parent 25442579
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ java_library {
    srcs: [
        "*.kt",
    ],
    libs: ["com.android.systemui.util.kotlin"],
    static_libs: ["SystemUILogLib"],
    defaults: [
        "SystemUI_pod_defaults_api",
+8 −107
Original line number Diff line number Diff line
@@ -16,12 +16,6 @@

package com.android.systemui.log.table

import com.android.systemui.kairos.BuildScope
import com.android.systemui.kairos.ExperimentalKairosApi
import com.android.systemui.kairos.State
import com.android.systemui.kairos.changes
import com.android.systemui.kairos.effectSync
import com.android.systemui.kairos.util.NameTag
import com.android.systemui.util.kotlin.pairwiseBy
import kotlinx.coroutines.flow.Flow

@@ -33,7 +27,7 @@ import kotlinx.coroutines.flow.Flow
 *
 * @param columnPrefix a prefix that will be applied to every column name that gets logged.
 */
fun <T : Diffable<T>> Flow<T>.logDiffsForTable(
public fun <T : Diffable<T>> Flow<T>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    initialValue: T,
@@ -55,7 +49,7 @@ fun <T : Diffable<T>> Flow<T>.logDiffsForTable(
// above [logDiffsForTable] method.

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
fun Flow<Boolean>.logDiffsForTable(
public fun Flow<Boolean>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
@@ -74,7 +68,7 @@ fun Flow<Boolean>.logDiffsForTable(
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
fun Flow<Int>.logDiffsForTable(
public fun Flow<Int>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
@@ -93,7 +87,7 @@ fun Flow<Int>.logDiffsForTable(
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
fun Flow<Int?>.logDiffsForTable(
public fun Flow<Int?>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
@@ -112,7 +106,7 @@ fun Flow<Int?>.logDiffsForTable(
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
fun Flow<String?>.logDiffsForTable(
public fun Flow<String?>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
@@ -131,7 +125,7 @@ fun Flow<String?>.logDiffsForTable(
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
fun <T> Flow<List<T>>.logDiffsForTable(
public fun <T> Flow<List<T>>.logDiffsForTable(
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
@@ -155,98 +149,5 @@ fun <T> Flow<List<T>>.logDiffsForTable(
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logIntDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    intState: State<Int?>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    intState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/**
 * Each time the flow is updated with a new value, logs the differences between the previous value
 * and the new value to the given [tableLogBuffer].
 *
 * The new value's [Diffable.logDiffs] method will be used to log the differences to the table.
 *
 * @param columnPrefix a prefix that will be applied to every column name that gets logged.
 */
@ExperimentalKairosApi
fun <T : Diffable<T>> BuildScope.logDiffsForTable(
    name: NameTag,
    diffableState: State<T>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
) {
    val initialValue = diffableState.sampleDeferred()
    effectSync(name) {
        // Fully log the initial value to the table.
        tableLogBuffer.logChange(columnPrefix, isInitial = true) { row ->
            initialValue.value.logFull(row)
        }
    }
    diffableState.changes.observeSync(name) { newState ->
        val prevState = diffableState.sample()
        tableLogBuffer.logDiffs(columnPrefix, prevVal = prevState, newVal = newState)
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logBooleanDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    booleanState: State<Boolean>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    booleanState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logStringDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    stringState: State<String?>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    stringState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logListDiffsForTable")
fun <T> BuildScope.logDiffsForTable(
    name: NameTag,
    listState: State<List<T>>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    listState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new.toString(), isInitial = isInitial)
        isInitial = false
    }
}
// See [com.android.systemui.log.table.DiffableExtensionsKairos] for similar extension functions for
// Kairos states.
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.log.table

import com.android.systemui.kairos.BuildScope
import com.android.systemui.kairos.ExperimentalKairosApi
import com.android.systemui.kairos.State
import com.android.systemui.kairos.changes
import com.android.systemui.kairos.effectSync
import com.android.systemui.kairos.util.NameTag

// See [com.android.systemui.log.table.DiffableExtensions.kt] for non-Kairos extension functions.

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logIntDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    intState: State<Int?>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    intState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/**
 * Each time the flow is updated with a new value, logs the differences between the previous value
 * and the new value to the given [tableLogBuffer].
 *
 * The new value's [Diffable.logDiffs] method will be used to log the differences to the table.
 *
 * @param columnPrefix a prefix that will be applied to every column name that gets logged.
 */
@ExperimentalKairosApi
fun <T : Diffable<T>> BuildScope.logDiffsForTable(
    name: NameTag,
    diffableState: State<T>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
) {
    val initialValue = diffableState.sampleDeferred()
    effectSync(name) {
        // Fully log the initial value to the table.
        tableLogBuffer.logChange(columnPrefix, isInitial = true) { row ->
            initialValue.value.logFull(row)
        }
    }
    diffableState.changes.observeSync(name) { newState ->
        val prevState = diffableState.sample()
        tableLogBuffer.logDiffs(columnPrefix, prevVal = prevState, newVal = newState)
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logBooleanDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    booleanState: State<Boolean>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    booleanState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logStringDiffsForTable")
fun BuildScope.logDiffsForTable(
    name: NameTag,
    stringState: State<String?>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    stringState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new, isInitial = isInitial)
        isInitial = false
    }
}

/** See [logDiffsForTable(TableLogBuffer, String, T)]. */
@ExperimentalKairosApi
@JvmName("logListDiffsForTable")
fun <T> BuildScope.logDiffsForTable(
    name: NameTag,
    listState: State<List<T>>,
    tableLogBuffer: TableLogBuffer,
    columnPrefix: String = "",
    columnName: String,
) {
    var isInitial = true
    listState.observe(name = name) { new ->
        tableLogBuffer.logChange(columnPrefix, columnName, new.toString(), isInitial = isInitial)
        isInitial = false
    }
}