I've implemented a date and time picker dialog in ...
# compose
v
I've implemented a date and time picker dialog in compsoe (https://github.com/vanpra/ComposeDateTimePicker) but for some reason it seems to skip a few frames when I show the dialog. I think this might be due to my hacky implementation of ViewPager for the date picker, so is there any way that I could improve it and make it more efficient? Currently I just Have a HorizontalScroller with 3 pages, one in the centre and one left and right of it and then when the user swipes right I animate it as they drag and when it reaches the boundary I snap it back to the centre. I find that when I'm rendering a simple layout on the pages it works fine but when I use it for the date picker it's a bit laggy.
s
I think your instinct is correct to point to the ViewPager, this code in particular looks like it might cause excessive recomposition: https://github.com/vanpra/ComposeDateTimePicker/blob/master/datetimepicker/src/main/java/com/vanpra/datetimepicker/ViewPager.kt#L112 This state write in composition often creates a recomposition loop. I'd add some logging to recomposition in the ViewPager and optimize the state writes/modifications/recompositions until you get it to do minimal work on changes.
cc @Adam Powell who has also done some early exploration of ViewPager
a
Don't fear
Layout
when creating a custom layout and embrace hoisted state objects. The linked pager goes to a lot of trouble to do in composition what is much easier to do in the layout phase, and the state gets spread out as internal private data that the caller can't control.
This was the exploration @Sean McQuillan [G] is referring to: https://gist.github.com/adamp/07d468f4bcfe632670f305ce3734f511
It's tied into another experiment I've been doing around gesture recognition+fling animation with coroutines but you get the idea
s
Seconding Layout and hoisted state here. As inspiration look at the way ScrollerPosition is used in the layout of VerticalScroller: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:ui/ui-foundation/src/main/java/androidx/ui/foundation/Scroller.kt;l=313?q=VerticalScroller
v
Thanks for the help. Will definitely try this out later