aczeccssa / LifeMark-KMM

Redesigned LifeMark 2024 based on Kotlin Multiplatform Mobile
2 stars 0 forks source link

Specific platform resolution problem #1

Open aczeccssa opened 2 months ago

aczeccssa commented 2 months ago

The render resolution is different with pixel resolution(screen), but the specific of between iOS and android is not the same.

aczeccssa commented 2 months ago

Multiplatformin using UIKit

aczeccssa commented 2 months ago

Resolve(Original author)

With the help of expect/actual, you can use LocalConfiguration.current.screenWidthDp on Android and LocalWindowInfo.current.containerSize on non-Android (starting from 1.5.10-beta01).

Structer

/** Getting screen size info for UI-related calculations */
data class ScreenSizeInfo(val hPX: Int, val wPX: Int, val hDP: Dp, val wDP: Dp)

@Composable expect fun getScreenSizeInfo(): ScreenSizeInfo

iosMain:

@OptIn(ExperimentalComposeUiApi::class)
@Composable
actual fun getScreenSizeInfo(): ScreenSizeInfo {
    val density = LocalDensity.current
    val config = LocalWindowInfo.current.containerSize

    return remember(density, config) {
        ScreenSizeInfo(
            hPX = config.height,
            wPX = config.width,
            hDP = with(density) { config.height.toDp() },
            wDP = with(density) { config.width.toDp() }
        )
    }
}

androidMain

@Composable
actual fun getScreenSizeInfo(): ScreenSizeInfo {
    val density = LocalDensity.current
    val config = LocalConfiguration.current
    val hDp = config.screenHeightDp.dp
    val wDp = config.screenWidthDp.dp

    return remember(density, config) {
        ScreenSizeInfo(
            hPX = with(density) { hDp.roundToPx() },
            wPX = with(density) { wDp.roundToPx() },
            hDP = hDp,
            wDP = wDp
        )
    }
}
aczeccssa commented 2 months ago

iOS

iOS

Android

Android

aczeccssa commented 2 months ago

We still find something wrong in customized Android system, this solution still not resolve in these system

Example: