kizitonwose / Calendar

A highly customizable calendar view and compose library for Android.
MIT License
4.5k stars 492 forks source link

App crashes when I use the 1.2.0-alpha02 version of material3 and call HorizontalCalendar #479

Closed kamdaou49 closed 11 months ago

kamdaou49 commented 12 months ago

Library information:

Describe the bug**

The app crashes with the following error message, when I use the 1.2.0-alpha02 version of material3; but it works well when I downgrade to 1.1.0 Error message:

Process: com.example.calendarbug, PID: 29186
                                                                                                    java.lang.NoSuchMethodError: No static method SnapLayoutInfoProvider(Landroidx/compose/foundation/lazy/LazyListState;Lkotlin/jvm/functions/Function3;)Landroidx/compose/foundation/gestures/snapping/SnapLayoutInfoProvider; in class Landroidx/compose/foundation/gestures/snapping/LazyListSnapLayoutInfoProviderKt; or its super classes (declaration of 'androidx.compose.foundation.gestures.snapping.LazyListSnapLayoutInfoProviderKt' appears in /data/app/com.example.calendarbug-DySF2vsA_aAp3K0KFMdfGA==/base.apk)
                                                                                                        at com.kizitonwose.calendar.compose.CalendarDefaults.pagedFlingBehavior(CalendarDefaults.kt:26)
                                                                                                        at com.kizitonwose.calendar.compose.CalendarDefaults.flingBehavior(CalendarDefaults.kt:37)
                                                                                                        at com.kizitonwose.calendar.compose.CalendarKt.Calendar(Calendar.kt:173)
                                                                                                        at com.kizitonwose.calendar.compose.CalendarKt.HorizontalCalendar(Calendar.kt:74)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:57)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:55)
                                                                                                        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.material3.SurfaceKt$Surface$1.invoke(Surface.kt:133)
                                                                                                        at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:114)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.material3.SurfaceKt.Surface-T9BRK9s(Surface.kt:111)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:52)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:50)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.material3.TextKt.ProvideTextStyle(Text.kt:352)
                                                                                                        at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81)
                                                                                                        at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.material3.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:73)
                                                                                                        at com.example.calendarbug.ui.theme.ThemeKt.CalendarBugTheme(Theme.kt:65)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-4$1.invoke(MainActivity.kt:50)
                                                                                                        at com.example.calendarbug.ComposableSingletons$MainActivityKt$lambda-4$1.invoke(MainActivity.kt:49)
                                                                                                        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.ui.platform.ComposeView.Content(ComposeView.android.kt:428)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
2023-07-06 15:57:58.818 29186-29186 AndroidRuntime          com.example.calendarbug              E      at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:195)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:119)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:118)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:110)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:158)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:157)
                                                                                                        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.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:157)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:142)
                                                                                                        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.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:78)
                                                                                                        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3340)
                                                                                                        at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3273)
                                                                                                        at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:588)
                                                                                                        at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:1009)
                                                                                                        at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:520)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:142)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133)
                                                                                                        at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1181)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:133)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:183)
                                                                                                        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.kt:314)
                                                                                                        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.kt:192)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133)
                                                                                                        at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1256)
                                                                                                        at android.view.View.dispatchAttachedToWindow(View.java:20107)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3619)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3626)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3626)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3626)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3626)
                                                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2571)
                                                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2188)
                                                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8918)
                                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1280)
                                                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:1019)
                                                                                                        at android.view.Choreographer.doFrame(Choreographer.java:911)
2023-07-06 15:57:58.818 29186-29186 AndroidRuntime          com.example.calendarbug              E      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1248)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:900)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                                        at android.os.Looper.loop(Looper.java:219)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8676)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

To Reproduce (if applicable)

Steps to reproduce the behavior:

  1. Create a project in android studio and add jetpack compose dependencies
  2. update material3 version to 1.2.0-alpha02
  3. add kizitonwose dependencies with the 2.3.0 version
  4. create a HorizontalCalendar in the main activity
  5. Launch the app and it will crash

Expected behavior (if applicable)

The calendar should be shown, as normal. This behavior can be seen when we downgrade the material3 version to 1.1.0

Code

This is the code in my mainActivity

package com.example.calendarbug

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.calendarbug.ui.theme.CalendarBugTheme
import com.kizitonwose.calendar.compose.HorizontalCalendar
import com.kizitonwose.calendar.compose.rememberCalendarState
import com.kizitonwose.calendar.core.CalendarDay
import com.kizitonwose.calendar.core.DayPosition

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CalendarBugTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val calendarState = rememberCalendarState()
                    HorizontalCalendar(
                        state = calendarState,
                        dayContent = { day ->
                            Day(
                                day = day,
                                isSelected = false,
                                onClick = {},
                            )
                        }
                    )
                }
            }
        }
    }
}

@Composable
fun Day(
    day: CalendarDay,
    isSelected: Boolean = false,
    colors: List<Color> = emptyList(),
    onClick: (CalendarDay) -> Unit = {},
    selectedBackgroundColor: Color = MaterialTheme.colorScheme.onBackground,
    unselectedBackgroundColor: Color = MaterialTheme.colorScheme.background,
    selectedContentColor: Color = MaterialTheme.colorScheme.background,
    unselectedContentColor: Color = MaterialTheme.colorScheme.onBackground,
    enabled: Boolean = day.position == DayPosition.MonthDate
) {
    Box(
        modifier = Modifier
            .aspectRatio(1f) // This is important for square-sizing!
            .clip(CircleShape)
            .background(color = if (isSelected) selectedBackgroundColor else unselectedBackgroundColor)
            .clickable(
                enabled = day.position == DayPosition.MonthDate,
                onClick = { onClick(day) },
            ),
        contentAlignment = Alignment.Center
    ) {
        Text(
            modifier = Modifier
                .align(Alignment.Center)
                .padding(4.dp),
            text = day.date.dayOfMonth.toString(),
            color = if (isSelected) {
                if (enabled) selectedContentColor else selectedContentColor.copy(alpha = 0.3f)
            } else {
                if (enabled) unselectedContentColor else unselectedContentColor.copy(alpha = 0.3f)
            },
            style = MaterialTheme.typography.bodyMedium,
        )
        Row(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(16.dp),
            horizontalArrangement = Arrangement.spacedBy(2.dp),
        ) {
            for (color in colors) {
                Box(
                    modifier = Modifier
                        .height(4.dp)
                        .aspectRatio(1f)
                        .clip(CircleShape)
                        .background(color),
                )
            }
        }
    }
}
kizitonwose commented 11 months ago

This is due to a breaking change in compose 1.5. You need calendar library version 2.4.0-beta01