-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop usage of LocalConfiguration for screen size retrieving
- Loading branch information
Showing
23 changed files
with
162 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/provider/LocalScreenSize.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.