Does anyone know how to change the selectableDates...
# compose
l
Does anyone know how to change the selectableDates reactively in DatePickerState?
a
Make the dates a mutable state list or a reactive property that tells composition to refresh (live data and flows have methods to do so too). Then pass its current value into the state.
s
The problem is that the value passed inside
rememberDatePickerState
is not used as a key itself inside the remember that is used internally https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]DatePickerState&ss=androidx%2Fplatform%2Fframeworks%2Fsupport And you can not even make your own version of this since
DatePickerStateImpl
is internal to the library 😄
Now why is that the case? I really do not know. You may have to fork the entire implementation in order to edit it yourself as you see fit. And/or create an issue to perhaps get an answer back as to what the expectation is on how to satisfy this use case.
a
Agh that was going to be my follow up question. Is it one of those 😐
Date picker state as an interface you can implement, copy over the original impl code, seems not too large? And then make your own remember method
Or use reflection to construct the class dynamically with just forking the remember method…
b
Can’t you remember your own DatePickerState though from
fun DatePickerState
? Same way you can remember your own DismissState
s
The impl class is internal as we said above. The only way to do it is if you make your own implementation of that interface. Alternatively perhaps you can key the entire thing yourself by wrapping your remember... with
key(your, input) { rememberDate... }
? Not sure that'd work but you can't wrap a remember with another remember so that wouldn't be an option.
s
Ah nice, that’s convenient. You can wrap that around your own remember then and key it to initialDate and selectableDates for example. Only now you won’t be able to do rememberSaveable though as far as I can tell, since the Saver is internal too. Maybe you can copy the implementation again https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]n/kotlin/androidx/compose/material3/DatePicker.kt;l=1205-1227 and on the restore function use the public
DatePickerState
function? Might work.
b
Yeah seems simple enough to copy. Everything should be available to do it