Skip to content

Commit

Permalink
Drop usage of LocalConfiguration for screen size retrieving
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 16, 2025
1 parent e01e5ef commit 8651d97
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScreenSize> { error("ScreenSize not present") }
Expand Down Expand Up @@ -88,4 +91,8 @@ fun rememberScreenSize(): ScreenSize {
}
}
}.value
}
}

@Composable
fun rememberCurrentLifecycleEvent(): Lifecycle.Event =
LocalLifecycleOwner.current.lifecycle.observeAsState().value
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@

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
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) {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ 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
import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.needToShowStoragePermissionRequest
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

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit 8651d97

Please sign in to comment.