Skip to content

Commit

Permalink
Add isInBackground property to dachlatten-android module
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Nov 2, 2023
1 parent 9368c94 commit 08cce29
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions dachlatten-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {

dependencies {
implementation(libs.coroutines)
implementation(libs.lifecycle.process)
implementation(libs.annotation.jvm)

testImplementation(libs.coroutines)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.sipgate.dachlatten.android.lifecycle

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

private val lifecycleEventFlow =
callbackFlow {
val lifecycleListener = LifecycleEventObserver { _, event -> trySend(event) }

ProcessLifecycleOwner
.get()
.lifecycle
.addObserver(lifecycleListener)

awaitClose {
ProcessLifecycleOwner
.get()
.lifecycle
.removeObserver(lifecycleListener)
}
}

val isInForeground =
lifecycleEventFlow.map { event ->
when (event) {
Lifecycle.Event.ON_CREATE,
Lifecycle.Event.ON_START,
Lifecycle.Event.ON_RESUME,
-> true

Lifecycle.Event.ON_PAUSE,
Lifecycle.Event.ON_STOP,
Lifecycle.Event.ON_DESTROY,
Lifecycle.Event.ON_ANY,
-> false
}.also {
Log.d("dachlatten-android", "Event $event set Application is in foreground to: $it")
}
}.stateIn(CoroutineScope(Dispatchers.Unconfined), SharingStarted.Eagerly, false)
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ junit = "5.10.0"
coroutines = "1.7.3"
turbine = "1.0.0"
annotation-jvm = "1.7.0"
androidx-lifecycle = "2.6.2"

[libraries]
coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
coroutinesTest = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }

lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "androidx-lifecycle" }

android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
annotation-jvm = { group = "androidx.annotation", name = "annotation-jvm", version.ref = "annotation-jvm" }
Expand Down

0 comments on commit 08cce29

Please sign in to comment.