Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create DateRangePicker inside ModalBottomSheetLayout #27

Closed
StroeskuV opened this issue Feb 11, 2023 · 1 comment
Closed

Can't create DateRangePicker inside ModalBottomSheetLayout #27

StroeskuV opened this issue Feb 11, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@StroeskuV
Copy link

I show in your BottomSheetSample.
When I use CalendarView inside BottomSheetSample with CalendarSelection.Dates - all work correctly, but when I try use CalendarSelection.Period - I catch error. It is library bug or I do something wrong?

package com.mk.sheets.compose.samples

import android.util.Range
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import com.maxkeppeker.sheets.core.models.base.rememberSheetState
import com.maxkeppeler.sheets.calendar.CalendarView
import com.maxkeppeler.sheets.calendar.models.CalendarConfig
import com.maxkeppeler.sheets.calendar.models.CalendarSelection
import com.maxkeppeler.sheets.calendar.models.CalendarStyle
import kotlinx.coroutines.launch
import java.time.LocalDate

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BottomSheetSample(
    state: ModalBottomSheetState,
    screenContent: @Composable () -> Unit,
) {
    val coroutine = rememberCoroutineScope()
    val hideBottomSheet = { coroutine.launch { state.animateTo(ModalBottomSheetValue.Hidden) } }
    val dialogSheetState =
        rememberSheetState(visible = true, onCloseRequest = { hideBottomSheet(); })

    LaunchedEffect(state.currentValue) {
        when (state.currentValue) {
            ModalBottomSheetValue.Hidden,
            ModalBottomSheetValue.Expanded -> {
                dialogSheetState.invokeReset() // Manually reset internal state if required
            }
            ModalBottomSheetValue.HalfExpanded -> Unit

        }
    }

    ModalBottomSheetLayout(
        content = screenContent,
        sheetState = state,
        sheetContent = {
            val selectedDateRange = remember {
                val value = Range(LocalDate.now().minusDays(20), LocalDate.now().minusDays(10))
                mutableStateOf(value)
            }

            Surface(
                color = MaterialTheme.colorScheme.surface,
            ) {
                CalendarView(
                    sheetState = dialogSheetState,
                    config = CalendarConfig(
                        style = CalendarStyle.MONTH
                    ),
                    selection = CalendarSelection.Period(
                        selectedRange = selectedDateRange.value
                    ) { startDate, endDate ->
                        selectedDateRange.value = Range(startDate, endDate)
                    },
                )
            }
        }
    )
}

StackTrace:

 FATAL EXCEPTION: main
 Process: com.mk.sheets.compose, PID: 19557
 java.lang.IndexOutOfBoundsException: index: 0, size: 0
 	at androidx.compose.runtime.external.kotlinx.collections.immutable.internal.ListImplementation.checkElementIndex$runtime_release(ListImplementation.kt:15)
 	at androidx.compose.runtime.external.kotlinx.collections.immutable.implementations.immutableList.SmallPersistentVector.get(SmallPersistentVector.kt:146)
 	at androidx.compose.runtime.snapshots.SnapshotStateList.get(SnapshotStateList.kt:72)
 	at androidx.compose.runtime.snapshots.SnapshotStateList.set(SnapshotStateList.kt:103)
 	at com.maxkeppeler.sheets.calendar.CalendarState.processSelection(CalendarState.kt:242)
 	at com.maxkeppeler.sheets.calendar.CalendarViewKt$CalendarView$onSelection$1.invoke(CalendarView.kt:59)
 	at com.maxkeppeler.sheets.calendar.CalendarViewKt$CalendarView$onSelection$1.invoke(CalendarView.kt:58)
 	at com.maxkeppeler.sheets.calendar.views.CalendarDateItemComponentKt$CalendarDateItemComponent$normalModifier$1.invoke(CalendarDateItemComponent.kt:95)
 	at com.maxkeppeler.sheets.calendar.views.CalendarDateItemComponentKt$CalendarDateItemComponent$normalModifier$1.invoke(CalendarDateItemComponent.kt:95)
 	at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
 	at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
 	at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:222)
 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
 	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
 	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
 	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
 	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
 	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
 	at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
 	at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:566)
 	at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:456)
 	at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:469)
 	at androidx.compose.ui.node.BackwardsCompatNode.onPointerEvent-H0pRuoY(BackwardsCompatNode.kt:394)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:314)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
 	at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:183)
 	at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:102)
 	at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:98)
 	at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1329)
 	at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1275)
 	at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1214)
 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
 	at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:915)
 	at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1957)
 	at android.app.Activity.dispatchTouchEvent(Activity.java:4182)
 	at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:873)
 	at android.view.View.dispatchPointerEvent(View.java:15458)
 	at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7457)
 	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7233)
 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6595)
 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6786)
 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
 	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6843)
 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9880)
 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9718)
 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9671)
 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10014)
 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
 	at android.os.MessageQueue.nativePollOnce(Native Method)
 	at android.os.MessageQueue.next(MessageQueue.java:335)
 	at android.os.Looper.loop(Looper.java:206)
 	at android.app.ActivityThread.main(ActivityThread.java:8653)

                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@544d50b, androidx.compose.runtime.BroadcastFrameClock@8ba3fe8, StandaloneCoroutine{Cancelling}@c361601, AndroidUiDispatcher@381dca6]

@maxkeppeler maxkeppeler added the bug Something isn't working label Feb 19, 2023
@maxkeppeler
Copy link
Owner

Remove dialogSheetState.invokeReset() and call that only once the use-case is done / he bottom sheet is closed, so that it can be used again when needed. I will also modify the internal reset function to not break the view. If reset is called, it should still use the config & selection data, and not empty the data completely.

maxkeppeler added a commit that referenced this issue Feb 19, 2023
Don't reset to null, but to selection data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants