/* * Copyright (C) 2023 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.util.kotlin import android.content.Context import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map class Utils { companion object { fun toTriple(a: A, bc: Pair) = Triple(a, bc.first, bc.second) fun toTriple(ab: Pair, c: C) = Triple(ab.first, ab.second, c) fun toQuad(a: A, b: B, c: C, d: D) = Quad(a, b, c, d) fun toQuad(a: A, bcd: Triple) = Quad(a, bcd.first, bcd.second, bcd.third) fun toQuad(abc: Triple, d: D) = Quad(abc.first, abc.second, abc.third, d) fun toQuint(a: A, b: B, c: C, d: D, e: E) = Quint(a, b, c, d, e) fun toQuint(a: A, bcde: Quad) = Quint(a, bcde.first, bcde.second, bcde.third, bcde.fourth) fun toSextuple(a: A, bcdef: Quint) = Sextuple(a, bcdef.first, bcdef.second, bcdef.third, bcdef.fourth, bcdef.fifth) fun toSeptuple(a: A, bcdefg: Sextuple) = Septuple( a, bcdefg.first, bcdefg.second, bcdefg.third, bcdefg.fourth, bcdefg.fifth, bcdefg.sixth ) /** * Samples the provided flow, performs a filter on the sampled value, then returns the * original value. */ fun Flow.sampleFilter(b: Flow, predicate: (B) -> Boolean): Flow { return this.sample(b, ::Pair).filter { (_, b) -> predicate(b) }.map { (a, _) -> a } } /** * Samples the provided flows, emitting a tuple of the original flow's value as well as each * of the combined flows' values. * * Flow.sample(Flow, Flow) -> (A, B, C) */ fun Flow.sample(b: Flow, c: Flow): Flow> { return this.sample(combine(b, c, ::Pair), ::toTriple) } /** * Samples the provided flows, emitting a tuple of the original flow's value as well as each * of the combined flows' values. * * Flow.sample(Flow, Flow, Flow) -> (A, B, C, D) */ fun Flow.sample( b: Flow, c: Flow, d: Flow ): Flow> { return this.sample(combine(b, c, d, ::Triple), ::toQuad) } /** * Samples the provided flows, emitting a tuple of the original flow's value as well as each * of the combined flows' values. * * Flow.sample(Flow, Flow, Flow, Flow) -> (A, B, C, D, E) */ fun Flow.sample( b: Flow, c: Flow, d: Flow, e: Flow, ): Flow> { return this.sample(combine(b, c, d, e, ::Quad), ::toQuint) } /** * Samples the provided flows, emitting a tuple of the original flow's value as well as each * of the combined flows' values. * * Flow.sample(Flow, Flow, Flow, Flow, Flow) -> (A, B, C, D, E, F) */ fun Flow.sample( b: Flow, c: Flow, d: Flow, e: Flow, f: Flow, ): Flow> { return this.sample(combine(b, c, d, e, f, ::Quint), ::toSextuple) } /** * Samples the provided flows, emitting a tuple of the original flow's value as well as each * of the combined flows' values. * * Flow.sample(Flow, Flow, Flow, Flow, Flow, Flow) -> (A, B, C, D, E, * F, G) */ fun Flow.sample( b: Flow, c: Flow, d: Flow, e: Flow, f: Flow, g: Flow, ): Flow> { return this.sample(combine(b, c, d, e, f, g, ::Sextuple), ::toSeptuple) } } } data class Quad(val first: A, val second: B, val third: C, val fourth: D) data class Quint( val first: A, val second: B, val third: C, val fourth: D, val fifth: E ) data class Sextuple( val first: A, val second: B, val third: C, val fourth: D, val fifth: E, val sixth: F, ) data class Septuple( val first: A, val second: B, val third: C, val fourth: D, val fifth: E, val sixth: F, val seventh: G, ) fun Int.toPx(context: Context): Int { return (this * context.resources.displayMetrics.density).toInt() } fun Int.toDp(context: Context): Int { return (this / context.resources.displayMetrics.density).toInt() }