From 8651d97d9cd85cb92cb85d4dc0e309672afbc0d9 Mon Sep 17 00:00:00 2001 From: T8RIN Date: Thu, 16 Jan 2025 17:37:08 +0300 Subject: [PATCH] Drop usage of LocalConfiguration for screen size retrieving --- .../core/ui/utils/provider/LocalScreenSize.kt | 9 ++++++++- .../core/ui/widget/modifier/Padding.kt | 5 ++--- .../core/ui/widget/utils/AvailableHeight.kt | 12 +++++------- .../components/MediaPickerRootContent.kt | 10 +++------- .../components/dialogs/PermissionDialog.kt | 6 +++--- .../presentation/components/ClearCacheSettingItem.kt | 9 +++------ 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/provider/LocalScreenSize.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/provider/LocalScreenSize.kt index 92ca0a1f4d..306d9d4ab7 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/provider/LocalScreenSize.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/provider/LocalScreenSize.kt @@ -34,6 +34,9 @@ import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.compose.LocalLifecycleOwner +import com.t8rin.dynamic.theme.observeAsState import com.t8rin.modalsheet.FullscreenPopup val LocalScreenSize = compositionLocalOf { error("ScreenSize not present") } @@ -88,4 +91,8 @@ fun rememberScreenSize(): ScreenSize { } } }.value -} \ No newline at end of file +} + +@Composable +fun rememberCurrentLifecycleEvent(): Lifecycle.Event = + LocalLifecycleOwner.current.lifecycle.observeAsState().value \ No newline at end of file diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/modifier/Padding.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/modifier/Padding.kt index 2436151400..14fb794b32 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/modifier/Padding.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/modifier/Padding.kt @@ -17,7 +17,6 @@ package ru.tech.imageresizershrinker.core.ui.widget.modifier -import android.content.res.Configuration import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.asPaddingValues import androidx.compose.foundation.layout.displayCutoutPadding @@ -25,8 +24,8 @@ import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.navigationBarsPadding import androidx.compose.ui.Modifier import androidx.compose.ui.composed -import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.unit.dp +import ru.tech.imageresizershrinker.core.ui.utils.helper.isLandscapeOrientationAsState fun Modifier.navBarsPaddingOnlyIfTheyAtTheEnd(enabled: Boolean = true) = this.composed { if (WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding() == 0.dp && enabled) { @@ -37,7 +36,7 @@ fun Modifier.navBarsPaddingOnlyIfTheyAtTheEnd(enabled: Boolean = true) = this.co } fun Modifier.navBarsLandscapePadding(enabled: Boolean = true) = this.composed { - if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE && enabled) { + if (isLandscapeOrientationAsState().value && enabled) { Modifier .navigationBarsPadding() .displayCutoutPadding() diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/utils/AvailableHeight.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/utils/AvailableHeight.kt index 45bba8e85b..bad71f83b7 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/utils/AvailableHeight.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/widget/utils/AvailableHeight.kt @@ -35,14 +35,13 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.onSizeChanged -import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp -import androidx.lifecycle.compose.LocalLifecycleOwner -import com.t8rin.dynamic.theme.observeAsState import com.t8rin.modalsheet.FullscreenPopup import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState +import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize +import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberCurrentLifecycleEvent import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarDefaults import ru.tech.imageresizershrinker.core.ui.widget.image.ImageHeaderState @@ -69,10 +68,9 @@ fun rememberAvailableHeight( @OptIn(ExperimentalMaterial3Api::class) @Composable fun rememberFullHeight(): Dp { - var fullHeight by remember( - LocalConfiguration.current, - LocalLifecycleOwner.current.lifecycle.observeAsState().value - ) { mutableStateOf(0.dp) } + val screenSize = LocalScreenSize.current + val currentLifecycleEvent = rememberCurrentLifecycleEvent() + var fullHeight by remember(screenSize, currentLifecycleEvent) { mutableStateOf(0.dp) } val density = LocalDensity.current diff --git a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaPickerRootContent.kt b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaPickerRootContent.kt index 10484022f7..49e4947131 100644 --- a/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaPickerRootContent.kt +++ b/feature/media-picker/src/main/java/ru/tech/imageresizershrinker/feature/media_picker/presentation/components/MediaPickerRootContent.kt @@ -57,11 +57,10 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.app.ActivityCompat -import androidx.lifecycle.compose.LocalLifecycleOwner -import com.t8rin.dynamic.theme.observeAsState import ru.tech.imageresizershrinker.core.resources.R import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.isInstalledFromPlayStore import ru.tech.imageresizershrinker.core.ui.utils.permission.PermissionUtils.hasPermissionAllowed +import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberCurrentLifecycleEvent import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar @@ -101,11 +100,8 @@ internal fun MediaPickerActivity.MediaPickerRootContent( } } - val lifecycleEvent by LocalLifecycleOwner.current.lifecycle.observeAsState() - LaunchedEffect( - lifecycleEvent, - invalidator - ) { + val lifecycleEvent = rememberCurrentLifecycleEvent() + LaunchedEffect(lifecycleEvent, invalidator) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { val permission = Manifest.permission.READ_MEDIA_IMAGES isPermissionAllowed = hasPermissionAllowed(permission) diff --git a/feature/root/src/main/java/ru/tech/imageresizershrinker/feature/root/presentation/components/dialogs/PermissionDialog.kt b/feature/root/src/main/java/ru/tech/imageresizershrinker/feature/root/presentation/components/dialogs/PermissionDialog.kt index b5fd96972d..47045b4143 100644 --- a/feature/root/src/main/java/ru/tech/imageresizershrinker/feature/root/presentation/components/dialogs/PermissionDialog.kt +++ b/feature/root/src/main/java/ru/tech/imageresizershrinker/feature/root/presentation/components/dialogs/PermissionDialog.kt @@ -32,8 +32,6 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.res.stringResource import androidx.core.app.ActivityCompat -import androidx.lifecycle.compose.LocalLifecycleOwner -import com.t8rin.dynamic.theme.observeAsState import kotlinx.coroutines.delay import ru.tech.imageresizershrinker.core.resources.R import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState @@ -41,6 +39,7 @@ import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.needToShow import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.requestStoragePermission import ru.tech.imageresizershrinker.core.ui.utils.permission.PermissionUtils.hasPermissionAllowed import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalComponentActivity +import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberCurrentLifecycleEvent import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedAlertDialog import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton @@ -51,11 +50,12 @@ internal fun PermissionDialog() { var showDialog by remember { mutableStateOf(false) } + val currentLifecycleEvent = rememberCurrentLifecycleEvent() LaunchedEffect( showDialog, context, settingsState, - LocalLifecycleOwner.current.lifecycle.observeAsState().value + currentLifecycleEvent ) { showDialog = context.needToShowStoragePermissionRequest() == true while (showDialog) { diff --git a/feature/settings/src/main/java/ru/tech/imageresizershrinker/feature/settings/presentation/components/ClearCacheSettingItem.kt b/feature/settings/src/main/java/ru/tech/imageresizershrinker/feature/settings/presentation/components/ClearCacheSettingItem.kt index 7c4ec5f8d9..5376bcbb2a 100644 --- a/feature/settings/src/main/java/ru/tech/imageresizershrinker/feature/settings/presentation/components/ClearCacheSettingItem.kt +++ b/feature/settings/src/main/java/ru/tech/imageresizershrinker/feature/settings/presentation/components/ClearCacheSettingItem.kt @@ -31,9 +31,8 @@ import androidx.compose.ui.graphics.Shape import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.lifecycle.compose.LocalLifecycleOwner -import com.t8rin.dynamic.theme.observeAsState import ru.tech.imageresizershrinker.core.resources.R +import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberCurrentLifecycleEvent import ru.tech.imageresizershrinker.core.ui.widget.modifier.ContainerShapeDefaults import ru.tech.imageresizershrinker.core.ui.widget.preferences.PreferenceItem @@ -45,10 +44,8 @@ fun ClearCacheSettingItem( modifier: Modifier = Modifier.padding(horizontal = 8.dp) ) { val context = LocalContext.current - var cache by remember( - context, - LocalLifecycleOwner.current.lifecycle.observeAsState().value - ) { mutableStateOf(value) } + val currentLifecycleEvent = rememberCurrentLifecycleEvent() + var cache by remember(context, currentLifecycleEvent) { mutableStateOf(value) } PreferenceItem( shape = shape,