aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/foundation/e/privacycentralapp/common
diff options
context:
space:
mode:
authorGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-07-28 06:04:22 +0000
committerGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-07-28 06:04:22 +0000
commitb4d35c1c12120503e74d7ae99edd94302673acf6 (patch)
treef87e29f670323b7173e5e3875112271c8835a5d3 /app/src/main/java/foundation/e/privacycentralapp/common
parent3ca73e64ddd25c7c20eca2e4e0db77032db848c0 (diff)
downloadadvanced-privacy-b4d35c1c12120503e74d7ae99edd94302673acf6.tar.gz
#5444 Fix CPU consumption - remove flow-mvi dependency
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/common')
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt2
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/common/ThrottleFlow.kt36
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/common/extensions/AnyExtension.kt28
3 files changed, 65 insertions, 1 deletions
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<Pair<Int, Int>>()
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 <https://www.gnu.org/licenses/>.
+ */
+
+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 <T> Flow<T>.throttleFirst(windowDuration: Duration): Flow<T> = 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 <https://www.gnu.org/licenses/>.
+ */
+
+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