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 f316fe1 commit e01e5ef
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
Expand Down Expand Up @@ -86,6 +85,7 @@ import ru.tech.imageresizershrinker.core.ui.theme.outlineVariant
import ru.tech.imageresizershrinker.core.ui.utils.helper.AppActivityClass
import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.copyToClipboard
import ru.tech.imageresizershrinker.core.ui.utils.provider.ImageToolboxCompositionLocals
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.utils.provider.setContentWithWindowSizeClass
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedFloatingActionButton
Expand Down Expand Up @@ -162,8 +162,7 @@ class CrashActivity : CrashHandler() {
modifier = Modifier.padding(16.dp)
)
Spacer(modifier = Modifier.height(24.dp))
val screenWidth =
LocalConfiguration.current.screenWidthDp.dp - 32.dp
val screenWidth = LocalScreenSize.current.width - 32.dp
Row(
modifier = Modifier
.padding(horizontal = 16.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ import ru.tech.imageresizershrinker.core.ui.utils.navigation.Screen

fun fancySlideTransition(
isForward: Boolean,
screenWidthDp: Int,
screenWidthPx: Int,
duration: Int = 600
): ContentTransform = if (isForward) {
slideInHorizontally(
animationSpec = tween(duration, easing = FancyTransitionEasing),
initialOffsetX = { screenWidthDp }) + fadeIn(
initialOffsetX = { screenWidthPx }) + fadeIn(
tween(300, 100)
) togetherWith slideOutHorizontally(
animationSpec = tween(duration, easing = FancyTransitionEasing),
targetOffsetX = { -screenWidthDp }) + fadeOut(
targetOffsetX = { -screenWidthPx }) + fadeOut(
tween(300, 100)
)
} else {
slideInHorizontally(
animationSpec = tween(600, easing = FancyTransitionEasing),
initialOffsetX = { -screenWidthDp }) + fadeIn(
initialOffsetX = { -screenWidthPx }) + fadeIn(
tween(300, 100)
) togetherWith slideOutHorizontally(
animationSpec = tween(600, easing = FancyTransitionEasing),
targetOffsetX = { screenWidthDp }) + fadeOut(
targetOffsetX = { screenWidthPx }) + fadeOut(
tween(300, 100)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fun ImageToolboxCompositionLocals(
val confettiHostState = rememberConfettiHostState()
val context = LocalContext.current
val customHapticFeedback = rememberEnhancedHapticFeedback(settingsState.hapticsStrength)
val screenSize = rememberScreenSize()

val values = remember(
toastHostState,
Expand All @@ -58,7 +59,8 @@ fun ImageToolboxCompositionLocals(
editPresetsController,
confettiHostState,
context,
customHapticFeedback
customHapticFeedback,
screenSize
) {
derivedStateOf {
listOfNotNull(
Expand All @@ -68,7 +70,8 @@ fun ImageToolboxCompositionLocals(
LocalEditPresetsController provides editPresetsController,
LocalConfettiHostState provides confettiHostState,
LocalImageLoader provides context.imageLoader,
LocalHapticFeedback provides customHapticFeedback
LocalHapticFeedback provides customHapticFeedback,
LocalScreenSize provides screenSize
).toTypedArray()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/

package ru.tech.imageresizershrinker.core.ui.utils.provider

import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
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 com.t8rin.modalsheet.FullscreenPopup

val LocalScreenSize = compositionLocalOf<ScreenSize> { error("ScreenSize not present") }

@ConsistentCopyVisibility
data class ScreenSize internal constructor(
val width: Dp,
val height: Dp,
val widthPx: Int,
val heightPx: Int
)

private fun Density.ScreenSize(
width: Dp,
height: Dp,
) = ScreenSize(
width = width,
height = height,
widthPx = width.roundToPx(),
heightPx = height.roundToPx()
)

@Composable
fun rememberScreenSize(): ScreenSize {
val configuration = LocalConfiguration.current

var constraints by remember(configuration) {
mutableStateOf<Constraints?>(null)
}

if (constraints == null) {
FullscreenPopup {
BoxWithConstraints(
modifier = Modifier.fillMaxSize()
) {
SideEffect {
constraints = this.constraints
}
}
}
}

val density = LocalDensity.current

return remember(constraints, configuration, density) {
derivedStateOf {
with(density) {
ScreenSize(
width = constraints?.maxWidth?.toDp() ?: configuration.screenWidthDp.dp,
height = constraints?.maxHeight?.toDp() ?: configuration.screenHeightDp.dp,
)
}
}
}.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.utils.animation.fancySlideTransition
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.utils.provider.ProvideContainerDefaults
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedBottomSheetDefaults
Expand Down Expand Up @@ -93,7 +93,7 @@ fun AdaptiveBottomScaffoldLayoutScreen(
autoClearFocus: Boolean = true,
enableNoDataScroll: Boolean = true
) {
val screenWidthDp = LocalConfiguration.current.screenWidthDp
val screenWidthPx = LocalScreenSize.current.widthPx

val settingsState = LocalSettingsState.current

Expand Down Expand Up @@ -162,7 +162,7 @@ fun AdaptiveBottomScaffoldLayoutScreen(
transitionSpec = {
fancySlideTransition(
isForward = targetState,
screenWidthDp = screenWidthDp
screenWidthPx = screenWidthPx
)
}
) { canShowScreenData ->
Expand Down Expand Up @@ -253,13 +253,13 @@ fun AdaptiveBottomScaffoldLayoutScreen(
transitionSpec = {
fancySlideTransition(
isForward = targetState,
screenWidthDp = screenWidthDp
screenWidthPx = screenWidthPx
)
},
modifier = Modifier.fillMaxSize()
) { useScaffold ->
if (useScaffold) {
val screenHeight = LocalConfiguration.current.screenHeightDp.dp
val screenHeight = LocalScreenSize.current.height
BottomSheetScaffold(
modifier = Modifier.fillMaxSize(),
scaffoldState = scaffoldState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource
Expand All @@ -74,6 +73,7 @@ import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.utils.animation.fancySlideTransition
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar
Expand Down Expand Up @@ -167,13 +167,13 @@ fun AdaptiveLayoutScreen(
}
)
underTopAppBarContent?.invoke(this)
val screenWidthDp = LocalConfiguration.current.screenWidthDp
val screenWidthPx = LocalScreenSize.current.widthPx
AnimatedContent(
targetState = canShowScreenData,
transitionSpec = {
fancySlideTransition(
isForward = targetState,
screenWidthDp = screenWidthDp
screenWidthPx = screenWidthPx
)
},
modifier = Modifier.fillMaxSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
Expand All @@ -102,6 +101,7 @@ import ru.tech.imageresizershrinker.core.ui.theme.White
import ru.tech.imageresizershrinker.core.ui.theme.takeColorFromScheme
import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.getFilename
import ru.tech.imageresizershrinker.core.ui.utils.navigation.Screen
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarDefaults
Expand Down Expand Up @@ -144,7 +144,7 @@ fun ImagePager(
) {
val density = LocalDensity.current
val screenHeight =
LocalConfiguration.current.screenHeightDp.dp + WindowInsets.systemBars.asPaddingValues()
LocalScreenSize.current.height + WindowInsets.systemBars.asPaddingValues()
.let { it.calculateTopPadding() + it.calculateBottomPadding() }
val anchors = with(density) {
DraggableAnchors {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.layout
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.Dp
Expand All @@ -80,6 +79,7 @@ import ru.tech.imageresizershrinker.core.settings.domain.model.SliderType
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.theme.outlineVariant
import ru.tech.imageresizershrinker.core.ui.utils.helper.ProvidesValue
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedSlider
import ru.tech.imageresizershrinker.core.ui.widget.modifier.fadingEdges
import ru.tech.imageresizershrinker.core.ui.widget.modifier.materialShadow
Expand Down Expand Up @@ -116,7 +116,7 @@ fun LazyListScope.imageStickyHeader(
}

val haptics = LocalHapticFeedback.current
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val screenWidth = LocalScreenSize.current.width
val density = LocalDensity.current
Column(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
Expand All @@ -85,6 +84,7 @@ import ru.tech.imageresizershrinker.core.resources.icons.BrokenImageVariant
import ru.tech.imageresizershrinker.core.ui.theme.White
import ru.tech.imageresizershrinker.core.ui.theme.takeColorFromScheme
import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.getFilename
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.modifier.advancedShadow
import ru.tech.imageresizershrinker.core.ui.widget.modifier.dragHandler
import ru.tech.imageresizershrinker.core.ui.widget.other.LoadingIndicator
Expand Down Expand Up @@ -152,7 +152,7 @@ fun ImagesPreviewWithSelection(
}
}

val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val screenWidth = LocalScreenSize.current.width
val modifier = modifier ?: if (isPortrait) {
Modifier.height(
(130.dp * imageUris.size).coerceAtMost(420.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.AccessibilityManager
import androidx.compose.ui.platform.LocalAccessibilityManager
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
Expand All @@ -83,6 +82,7 @@ import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.theme.harmonizeWithPrimary
import ru.tech.imageresizershrinker.core.ui.theme.outlineVariant
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.modifier.autoElevatedBorder
import kotlin.coroutines.resume

Expand Down Expand Up @@ -130,8 +130,8 @@ fun Toast(
containerColor: Color = ToastDefaults.color,
contentColor: Color = ToastDefaults.contentColor,
) {
val configuration = LocalConfiguration.current
val sizeMin = configuration.screenWidthDp.coerceAtMost(configuration.screenHeightDp).dp
val screenSize = LocalScreenSize.current
val sizeMin = screenSize.width.coerceAtMost(screenSize.height)

Card(
colors = CardDefaults.cardColors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Constraints
Expand All @@ -72,6 +71,7 @@ import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.utils.content_pickers.ImagePicker
import ru.tech.imageresizershrinker.core.ui.utils.content_pickers.Picker
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalScreenSize
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.OneTimeImagePickingDialog
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedFloatingActionButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedSlider
Expand Down Expand Up @@ -326,7 +326,7 @@ fun CompareScreenContent(
placeable.place(-placeable.width, 0)
}
}
.width((LocalConfiguration.current.screenHeightDp / 2f).dp)
.width(LocalScreenSize.current.height / 2f)

EnhancedSlider(
modifier = modifier,
Expand Down
Loading

0 comments on commit e01e5ef

Please sign in to comment.