JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.38k stars 1.18k forks source link

rememberPagerState() forcing overload error due to mismatch between Compose Foundation versions in androidMain and commonMain #3260

Closed cryptrr closed 1 year ago

cryptrr commented 1 year ago

Describe the bug

Using 'rememberPagerState(Int = ..., Float = ...): PagerState' is an error. Please use the overload where you can provide a source of truth for the pageCount.

I'm using the official compose multiplatform template. From here

androidMain is using foundation 1.5.0-beta01 while commonMain is using 1.4.0

old overloads of rememberPagerState has been force disabled in Foundation 1.5.0-beta with the above error.

So it is impossible to use Pagers in commonMain code because the latest version of foundation available for multiplatform is 1.4.0 and does not have the overloads necessary to compile.

So how to downgrade foundation in androidMain since i'm not seeing this gradle dependency in the val androidMain by getting {} block.

Or do I need to copy all the code from foundation 1.5.0-beta?

Affected platforms Select one of the platforms below:

Versions

To Reproduce Steps and/or the code snippet to reproduce the behavior:

  1. Go to https://github.com/JetBrains/compose-multiplatform-ios-android-template and clone
  2. Use VerticalPager or HorizontalPager with rememberPagerState()
  3. See error Using 'rememberPagerState(Int = ..., Float = ...): PagerState' is an error. Please use the overload where you can provide a source of truth for the pageCount.

Expected behavior The foundation versions should be same or there should be a better way to downgrade

cryptrr commented 1 year ago

This error happens during compile time hence the compose compiler must be checking for every usage of rememberPagerState(). So I guess the compose compiler should also be downgraded to prevent this error.

m-sasha commented 1 year ago

How did you get 1.5.0-beta for foundation in androidMain?

I added this snipped to inside the Column in App.kt (in commonMain) and it works perfectly for me:

            val pagerState = rememberPagerState()
            HorizontalPager(
                pageCount = 10,
                modifier = Modifier.fillMaxWidth().padding(top = 32.dp),
                state = pagerState
            ){
                Text(
                    text = "Page $it",
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxWidth().background(Color.LightGray).padding(40.dp)
                )
            }

iOS

Simulator Screenshot - iPhone 14 - 2023-06-13 at 13 11 30

Android

Screenshot_1686651105

cryptrr commented 1 year ago

I have no idea :/ This is my androidMain dependencies list

val androidMain by getting {
            dependencies {
                api("androidx.activity:activity-compose:1.7.2")
                implementation("androidx.media3:media3-exoplayer:1.0.2")
                implementation("androidx.media3:media3-ui:1.0.2")
                implementation("androidx.constraintlayout:constraintlayout-compose:1.1.0-alpha10")
                api("androidx.appcompat:appcompat:1.6.1")
                api("androidx.core:core-ktx:1.10.1")
                implementation ("com.github.skydoves:cloudy:0.1.2")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
                implementation("com.google.accompanist:accompanist-systemuicontroller:0.31.3-beta")
            }
        }

and I'm using the default imports for commonMain

 val commonMain by getting {
            dependencies {

                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material3)
                implementation(compose.material)
                implementation(compose.materialIconsExtended)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)
               ....
}

Still I have foundation 1.5.0-beta in androidMain and 1.4.0 in commonMain

cryptrr commented 1 year ago

All of these dependencies are somehow using 1.5.0-beta in androidMain Screenshot from 2023-06-13 19-25-41

It's got to do with some third party libraries bumping it up.

m-sasha commented 1 year ago

You can run the gradle dependencies task from inside androidApp to see the tree of dependencies. In your case, compose 1.5.0-beta01 is pulled in by

cryptrr commented 1 year ago

Thanks. Is downgrading these dependencies the only way to fix this problem?

cryptrr commented 1 year ago

I checked the dependency tree. Weirdly it says even material3:1.0.1 is depending upon foundation 1.5.0-beta1 Screenshot from 2023-06-13 20-47-06

cryptrr commented 1 year ago

Removing accompanist and constraintlayout removed foundation 1.5.0 . Thanks.

cryptrr commented 1 year ago

Downgrading accompanist to v0.30.1 uses compose 1.4.0 and fixes this.

okushnikov commented 4 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.