From 7cb96529ef4c2061ec4261150853ee3f87ff764c Mon Sep 17 00:00:00 2001 From: Jan Seeger Date: Thu, 21 Dec 2023 10:41:34 +0100 Subject: [PATCH] Add convenience Activity accessor --- dachlatten-android/build.gradle.kts | 1 + .../dachlatten/android/activity/ContextExt.kt | 13 +++++++++++++ gradle/libs.versions.toml | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/activity/ContextExt.kt diff --git a/dachlatten-android/build.gradle.kts b/dachlatten-android/build.gradle.kts index 16c260d..9f056d5 100644 --- a/dachlatten-android/build.gradle.kts +++ b/dachlatten-android/build.gradle.kts @@ -9,6 +9,7 @@ dependencies { implementation(libs.coroutines) implementation(libs.lifecycle.process) implementation(libs.annotation.jvm) + implementation(libs.androidx.activity) testImplementation(libs.coroutines) } diff --git a/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/activity/ContextExt.kt b/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/activity/ContextExt.kt new file mode 100644 index 0000000..ce2ebdf --- /dev/null +++ b/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/activity/ContextExt.kt @@ -0,0 +1,13 @@ +package de.sipgate.dachlatten.android.activity + +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper +import androidx.activity.ComponentActivity + +fun Context.getActivity(): Activity? = + when (this) { + is ComponentActivity -> this + is ContextWrapper -> baseContext.getActivity() + else -> null + } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1570a7d..97b9ac9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,6 +14,7 @@ compose-compiler = "1.5.5" kotlinx-datetime = "0.5.0" kover = "0.7.5" mockk = "1.13.8" +androidx-activity = "1.8.2" [libraries] coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" } @@ -22,6 +23,7 @@ coroutinesTest = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-t support-annotations = { module = "com.android.support:support-annotations", version.ref = "support-annotations" } turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } junit4 = { group = "junit", name = "junit", version.ref = "junit4" } +androidx-activity = { group = "androidx.activity", name = "activity-compose", version.ref = "androidx-activity" } lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "androidx-lifecycle" } compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "compose" }