summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt
new file mode 100644
index 000000000..d5c19c681
--- /dev/null
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/LifecycleUtils.kt
@@ -0,0 +1,38 @@
+// SPDX-FileCopyrightText: 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package org.yuzu.yuzu_emu.utils
+
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.launch
+
+/**
+ * Collects this [Flow] with a given [LifecycleOwner].
+ * @param scope [LifecycleOwner] that this [Flow] will be collected with.
+ * @param repeatState When to repeat collection on this [Flow].
+ * @param resetState Optional lambda to reset state of an underlying [MutableStateFlow] after
+ * [stateCollector] has been run.
+ * @param stateCollector Lambda that receives new state.
+ */
+inline fun <reified T> Flow<T>.collect(
+ scope: LifecycleOwner,
+ repeatState: Lifecycle.State = Lifecycle.State.CREATED,
+ crossinline resetState: () -> Unit = {},
+ crossinline stateCollector: (state: T) -> Unit
+) {
+ scope.apply {
+ lifecycleScope.launch {
+ repeatOnLifecycle(repeatState) {
+ this@collect.collect {
+ stateCollector(it)
+ resetState()
+ }
+ }
+ }
+ }
+}