raamcosta / compose-destinations

Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
https://composedestinations.rafaelcosta.xyz
Apache License 2.0
3.23k stars 134 forks source link

Navigation to BottomSheet problem - AnimatedContentTransitionScopeImpl cannot be cast to AnimatedContentScope #460

Closed pwojtowicz-rg closed 1 year ago

pwojtowicz-rg commented 1 year ago

I am trying to add this nice library to my project, navigation between screens works fine, then I tried to add navigation to bottom sheets.

I have read all documentation and library's GitHub page, and I have provided all necessary changes in my code, but I am still getting error. Can you help me a bit? ;)

FATAL EXCEPTION: main
java.lang.ClassCastException: androidx.compose.animation.AnimatedContentTransitionScopeImpl cannot be cast to androidx.compose.animation.AnimatedContentScope
at com.ramcosta.composedestinations.animations.AnimatedNavHostEngine$toAccompanist$1.invoke(AnimatedNavHostEngine.kt:299)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$finalEnter$1$1.invoke(AnimatedNavHost.kt:177)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$finalEnter$1$1.invoke(AnimatedNavHost.kt:167)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$7$1.invoke(AnimatedNavHost.kt:204)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$7$1.invoke(AnimatedNavHost.kt:198)
at androidx.compose.animation.AnimatedContentKt.AnimatedContent(AnimatedContent.kt:787)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt.AnimatedNavHost(AnimatedNavHost.kt:196)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$11.invoke(Unknown Source:27)
at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$11.invoke(Unknown Source:10)
at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:169)
at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2468)
at androidx.compose.runtime.ComposerImpl.skipToGroupEnd(Composer.kt:2760)
at com.rg.mbilet.rybnik.presentation.MainActivity$onCreate$2$1$1.invoke(MainActivity.kt:64)
at com.rg.mbilet.rybnik.presentation.MainActivity$onCreate$2$1$1.invoke(MainActivity.kt:56)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.material.ModalBottomSheetKt$ModalBottomSheetLayout$2.invoke(ModalBottomSheet.kt:574)
at androidx.compose.material.ModalBottomSheetKt$ModalBottomSheetLayout$2.invoke(ModalBottomSheet.kt:571)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:117)
at androidx.compose.runtime.internal.ComposableLambdaImpl$invoke$1.invoke(ComposableLambda.jvm.kt:129)
at androidx.compose.runtime.internal.ComposableLambdaImpl$invoke$1.invoke(ComposableLambda.jvm.kt:128)
at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:169)
at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2468)
at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2737)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3352)
at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3303)
at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:781)
at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1097)
at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:124)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:569)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:537)
at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1299)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1309)
at android.view.Choreographer.doCallbacks(Choreographer.java:923)
at android.view.Choreographer.doFrame(Choreographer.java:847)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1283)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

This is my code with navigation configuration:

val navController = rememberAnimatedNavController()

val bottomSheetNavigator = rememberBottomSheetNavigator()
navController.navigatorProvider += bottomSheetNavigator

ModalBottomSheetLayout(
    bottomSheetNavigator = bottomSheetNavigator,
    sheetShape = RoundedCornerShape(16.dp),
) {

    DestinationsNavHost(
        navGraph = NavGraphs.root,
        navController = navController,
        engine = rememberAnimatedNavHostEngine()
    )
}

and here are all dependenciesI am using:

dependencies {

    // androidx.activity
    def activity_version = '1.7.2'
    implementation "androidx.activity:activity-compose:$activity_version"
    implementation "androidx.activity:activity-ktx:$activity_version"

    def camerax_version = "1.2.3"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-view:${camerax_version}"

    // androidx.compose.material
    def compose_material_version = '1.4.3'
    implementation "androidx.compose.material:material:$compose_material_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_material_version"

    // androidx.compose.ui
    def compose_ui_version = '1.4.3'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"

    // androidx.core
    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.core:core-splashscreen:1.0.1'

    // androidx.datastore
    implementation 'androidx.datastore:datastore-preferences:1.0.0'

    // androidx.lifecycle
    def lifecycle_version = '2.6.1'
    implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    // androidx.navigation
    def navigation_version = '2.7.0-beta02'
    implementation "androidx.navigation:navigation-compose:$navigation_version"

    // com.google.accompanist
    def accompanist_version = '0.31.3-beta'
    implementation "com.google.accompanist:accompanist-flowlayout:$accompanist_version"
    implementation "com.google.accompanist:accompanist-navigation-animation:$accompanist_version"
    implementation "com.google.accompanist:accompanist-navigation-material:$accompanist_version"
    implementation "com.google.accompanist:accompanist-permissions:$accompanist_version"
    implementation "com.google.accompanist:accompanist-systemuicontroller:$accompanist_version"

    // com.google.android.gms
    implementation 'com.google.android.gms:play-services-location:21.0.1'
    implementation 'com.google.android.gms:play-services-maps:18.1.0'

    // com.google.mlkit
    implementation 'com.google.mlkit:barcode-scanning:17.1.0'

    // com.google.maps
    implementation 'com.google.maps.android:maps-compose:2.11.4'

    // com.google.zxing
    implementation 'com.google.zxing:core:3.5.1'

    // BottomSheetDialog
    // implementation "com.holix.android:bottomsheetdialog-compose:1.2.1"

    // Coil
    def coil_version = '2.3.0'
    implementation "io.coil-kt:coil:$coil_version"
    implementation "io.coil-kt:coil-compose:$coil_version"
    implementation "io.coil-kt:coil-gif:$coil_version"
    implementation "io.coil-kt:coil-svg:$coil_version"

    // Compose Destinations
    def compose_destinations_version = '1.8.42-beta'
    implementation "io.github.raamcosta.compose-destinations:animations-core:$compose_destinations_version"
    ksp "io.github.raamcosta.compose-destinations:ksp:$compose_destinations_version"

    // Dagger Hilt
    implementation 'androidx.hilt:hilt-navigation-compose:1.1.0-alpha01'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation 'com.google.dagger:hilt-android:2.45'
    kapt 'com.google.dagger:hilt-android-compiler:2.45'

    // Lottie
    implementation "com.airbnb.android:lottie-compose:6.0.0"

    // Retrofit
    implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2'
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'

    // Shimmer
    implementation 'com.valentinilk.shimmer:compose-shimmer:1.0.4'

    // Timber
    implementation 'com.jakewharton.timber:timber:5.0.1'
}

and my plugins configuration just in case:

plugins {
    id 'com.android.application'
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    id 'com.google.devtools.ksp' version '1.8.20-1.0.11'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'org.jetbrains.kotlin.android'
}
pwojtowicz-rg commented 1 year ago

Seems I found solution 😁

I upgraded compileSdk and targetSdk from 33 to 34 and io.github.raamcosta.compose-destinations:animations-core from 1.8.42-beta to 1.9.42-beta.

Seems to work, but also seems that I am lacking some knowledge ;)

I do not know why I had problem with Jetpack Compose version 1.4.3 and your library 1.8.42-beta.

Now I have 1.9.42-beta and it should be used with Jetpack Compose 1.5, but in my dependencies I have now Jetpack Compose version 1.4.3. Does this explicitly stated version 1.4.3 is ignored somewhere behind scenes when synchronizing dependencies? I have heard something about transitive dependencies, but I am not expert here.

All in all, maybe my problemas was related to this change https://android-review.googlesource.com/c/platform/frameworks/support/+/2449824 and there was some conflict in my dependencies?

raamcosta commented 1 year ago

Yes, if you use 1.9.x it will update compose in your final apk to 1.5 via transitive dependencies.

Anyway, check 1.9.50 where we stopped using accompanist navigation. You should make an effort to use that one since it's now all official compose navigation!

Check release notes (working on them atm) to see the small changes you might have to do!