From 445146b5318fb4f2004bdf7ba912280729338963 Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 19 Jul 2023 15:33:38 +0200 Subject: [PATCH] Add a CompositionLocal for getting the current Activity --- .../k9mail/feature/preview/FeatureActivity.kt | 4 ++-- .../app/k9mail/ui/catalog/CatalogActivity.kt | 4 ++-- config/detekt/detekt.yml | 2 +- .../compose/common/activity/LocalActivity.kt | 21 +++++++++++++++++++ .../launcher/FeatureLauncherActivity.kt | 4 ++-- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/activity/LocalActivity.kt diff --git a/app-feature-preview/src/main/java/app/k9mail/feature/preview/FeatureActivity.kt b/app-feature-preview/src/main/java/app/k9mail/feature/preview/FeatureActivity.kt index 596373364..77c90b7a4 100644 --- a/app-feature-preview/src/main/java/app/k9mail/feature/preview/FeatureActivity.kt +++ b/app-feature-preview/src/main/java/app/k9mail/feature/preview/FeatureActivity.kt @@ -2,9 +2,9 @@ package app.k9mail.feature.preview import android.os.Bundle import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.view.WindowCompat +import app.k9mail.core.ui.compose.common.activity.setActivityContent import app.k9mail.feature.preview.ui.FeatureApp class FeatureActivity : ComponentActivity() { @@ -15,7 +15,7 @@ class FeatureActivity : ComponentActivity() { WindowCompat.setDecorFitsSystemWindows(window, false) - setContent { + setActivityContent { FeatureApp() } } diff --git a/app-ui-catalog/src/main/java/app/k9mail/ui/catalog/CatalogActivity.kt b/app-ui-catalog/src/main/java/app/k9mail/ui/catalog/CatalogActivity.kt index 458a19166..799e9c7d1 100644 --- a/app-ui-catalog/src/main/java/app/k9mail/ui/catalog/CatalogActivity.kt +++ b/app-ui-catalog/src/main/java/app/k9mail/ui/catalog/CatalogActivity.kt @@ -2,9 +2,9 @@ package app.k9mail.ui.catalog import android.os.Bundle import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.view.WindowCompat +import app.k9mail.core.ui.compose.common.activity.setActivityContent import app.k9mail.ui.catalog.ui.CatalogScreen class CatalogActivity : ComponentActivity() { @@ -15,7 +15,7 @@ class CatalogActivity : ComponentActivity() { WindowCompat.setDecorFitsSystemWindows(window, false) - setContent { + setActivityContent { CatalogScreen() } } diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index aea91bcca..dc234b904 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -792,7 +792,7 @@ style: Compose: CompositionLocalAllowlist: active: true - allowedCompositionLocals: [LocalColors, LocalElevations, LocalImages, LocalShapes, LocalSizes, LocalSpacings] + allowedCompositionLocals: [LocalColors, LocalElevations, LocalImages, LocalShapes, LocalSizes, LocalSpacings, LocalActivity] ContentEmitterReturningValues: active: true ModifierComposable: diff --git a/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/activity/LocalActivity.kt b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/activity/LocalActivity.kt new file mode 100644 index 000000000..84293ea29 --- /dev/null +++ b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/activity/LocalActivity.kt @@ -0,0 +1,21 @@ +package app.k9mail.core.ui.compose.common.activity + +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionContext +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.staticCompositionLocalOf + +val LocalActivity = staticCompositionLocalOf { + error("No value for LocalActivity provided") +} + +fun ComponentActivity.setActivityContent( + parent: CompositionContext? = null, + content: @Composable () -> Unit, +) { + setContent(parent) { + CompositionLocalProvider(LocalActivity provides this, content = content) + } +} diff --git a/feature/launcher/src/main/kotlin/app/k9mail/feature/launcher/FeatureLauncherActivity.kt b/feature/launcher/src/main/kotlin/app/k9mail/feature/launcher/FeatureLauncherActivity.kt index 93049c832..fe1be03d4 100644 --- a/feature/launcher/src/main/kotlin/app/k9mail/feature/launcher/FeatureLauncherActivity.kt +++ b/feature/launcher/src/main/kotlin/app/k9mail/feature/launcher/FeatureLauncherActivity.kt @@ -4,8 +4,8 @@ import android.app.Activity import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent import androidx.core.view.WindowCompat +import app.k9mail.core.ui.compose.common.activity.setActivityContent import app.k9mail.feature.account.setup.navigation.NAVIGATION_ROUTE_ACCOUNT_SETUP import app.k9mail.feature.launcher.ui.FeatureLauncherApp import app.k9mail.feature.onboarding.navigation.NAVIGATION_ROUTE_ONBOARDING @@ -19,7 +19,7 @@ class FeatureLauncherActivity : ComponentActivity() { val destination = intent.getStringExtra(EXTRA_DESTINATION) - setContent { + setActivityContent { FeatureLauncherApp(startDestination = destination) } }