From b4d35c1c12120503e74d7ae99edd94302673acf6 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Thu, 28 Jul 2022 06:04:22 +0000 Subject: #5444 Fix CPU consumption - remove flow-mvi dependency --- .../e/privacycentralapp/common/GraphHolder.kt | 2 +- .../e/privacycentralapp/common/ThrottleFlow.kt | 36 ++++++++++++++++++++++ .../common/extensions/AnyExtension.kt | 28 +++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/foundation/e/privacycentralapp/common/ThrottleFlow.kt create mode 100644 app/src/main/java/foundation/e/privacycentralapp/common/extensions/AnyExtension.kt (limited to 'app/src/main/java/foundation/e/privacycentralapp/common') diff --git a/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt b/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt index 32766ca..d7a9dd0 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt @@ -40,7 +40,7 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.MPPointF import foundation.e.privacycentralapp.R -import foundation.e.privacycentralapp.extensions.dpToPxF +import foundation.e.privacycentralapp.common.extensions.dpToPxF class GraphHolder(val barChart: BarChart, val context: Context, val isMarkerAbove: Boolean = true) { var data = emptyList>() diff --git a/app/src/main/java/foundation/e/privacycentralapp/common/ThrottleFlow.kt b/app/src/main/java/foundation/e/privacycentralapp/common/ThrottleFlow.kt new file mode 100644 index 0000000..21e1542 --- /dev/null +++ b/app/src/main/java/foundation/e/privacycentralapp/common/ThrottleFlow.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2022 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.privacycentralapp.common + +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow +import kotlin.time.Duration + +@FlowPreview +fun Flow.throttleFirst(windowDuration: Duration): Flow = flow { + var lastEmissionTime = 0L + collect { upstream -> + val currentTime = System.currentTimeMillis() + val mayEmit = currentTime - lastEmissionTime > windowDuration.inWholeMilliseconds + if (mayEmit) { + lastEmissionTime = currentTime + emit(upstream) + } + } +} diff --git a/app/src/main/java/foundation/e/privacycentralapp/common/extensions/AnyExtension.kt b/app/src/main/java/foundation/e/privacycentralapp/common/extensions/AnyExtension.kt new file mode 100644 index 0000000..5c73df9 --- /dev/null +++ b/app/src/main/java/foundation/e/privacycentralapp/common/extensions/AnyExtension.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2022 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.privacycentralapp.common.extensions + +import android.content.Context + +fun Any.toText(context: Context) = when (this) { + is Int -> context.getString(this) + is String -> this + else -> this.toString() +} + +fun Int.dpToPxF(context: Context): Float = this.toFloat() * context.resources.displayMetrics.density -- cgit v1.2.3