https://kotlinlang.org logo
#compose
Title
# compose
k

Klaas Kabini

02/01/2020, 10:01 AM
Just few questions regarding compose 1. Are there any plans to release composables that implements the same behaviour found in recycler views and view pagers? For curiosity reasons, what is the development status of ScrollingList that was shown on code snippets at Android Dev summit last year? 2. Are there any plans to add support for Lottie style animations? In other words, implementing a composable that can render animations defined in Adobe AfterEffects json file. 3. I understand that view models are scoped to the lifecycle of their Lifecycle owners such as Fragments and Activities.  However, is there a possibility that you add support for scoping ViewModels to lifecycle of composable functions. I am asking this question because it feels so cumbersome to initialise a ViewModel inside an activity then pass down its reference to a composable function in order to call functions inside the view model that fetches data. 4. I have find it impossible to arrange items in a Column or Row with Arrangement.Begin for example to place them at the beginning of main axis but still define fixed spacing between the items to space them evenly without having to manually put Spacers between the items. The problem with SpaceBetween, SpaceEvenly, SpaceAround is that it distributes the items over the main axis based on the width or height of the screen especially when the column is root of a composable. Take a look at the example below. If I want to arrange children of a Column to the beginning but still space them evenly, I have to end up writing this boilerplate code of putting spacers between items.
Copy code
Column(modifier = LayoutPadding(16.dp), arrangement = Arrangement.Begin) {
  Text(text = "Klaas", style = MaterialTheme.typography().body1)
  Spacer(LayoutHeight(12.dp))
  Text(text = "James", style = MaterialTheme.typography().body1)
  Spacer(LayoutHeight(12.dp))
  Text(text = "Tom", style = MaterialTheme.typography().body1)
  Spacer(LayoutHeight(12.dp))
  Text(text = "Martin", style = MaterialTheme.typography().body1)
  Spacer(LayoutHeight(12.dp))
  ColoredRect(color = Color.Red, modifier = LayoutWidth.Fill, height = 200.dp)
}
Can’t we have something like this where we can arrange the children of a layout to the beginning but still give the fixed spacing to spaces them evenly without having to put spacers between the items.  
Copy code
Column(modifier = LayoutPadding(16.dp) + ChildSpacing(16.dp), arrangement = Arrangement.Begin) {
  Text(text = "Klaas", style = MaterialTheme.typography().body1)
  Text(text = "James", style = MaterialTheme.typography().body1)
  Text(text = "Tom", style = MaterialTheme.typography().body1)
  Text(text = "Martin", style = MaterialTheme.typography().body1)
  ColoredRect(color = Color.Red, modifier = LayoutWidth.Fill, height = 200.dp)
}
a

Adam Powell

02/01/2020, 3:30 PM
0. Please break things like this up into one comment per question/topic so that we can have some meaningful threads about them 🙂
2. The composable vector graphics and compose animation APIs should permit doing this pretty smoothly, give it a look if you haven't already, the folks working on each of those API surfaces would love the feedback. It doesn't prescribe a file format.
👍 1
3. Are you asking for composable-scoped ViewModels specifically, or the ability to
remember {}
things across config changes? The latter is more our thinking at the moment and would likely use
ViewModel
under the hood.
4. That code looks like it's begging for a loop 🙂 you can also factor out the
LayoutHeight
modifier into a
val
and other usual kotlin tactics for avoiding repetition.
In general I'd prefer our API surface for things like Row/Column not grow a lot of special-purpose bells and whistles as it means more to copy/duplicate when someone wants to write a new/different layout that has similarities
usually when I've had repeated elements that I want to break up like that, I find myself doing things like centering and padding within a min-height for each element rather than adding explicit spacers in between
👍 1
k

Klaas Kabini

02/01/2020, 3:41 PM
I just did a copy and paste of the same line to do a quick test. So i don't normally write code like that @Adam Powell. I just posted a rough sketch of what I was experimenting with.
a

Adam Powell

02/01/2020, 3:41 PM
not judging 😄
k

Klaas Kabini

02/01/2020, 3:45 PM
3. I was asking asking specifically about composable scoped view models.
a

Adam Powell

02/01/2020, 3:46 PM
what about
ViewModel
in particular are you looking for?
k

Klaas Kabini

02/01/2020, 4:08 PM
My question was specifically around whether it is possible to have a view model that exists only for the lifetime of a composable or not. I would love to initialize the view model inside a composables function using ViewModelProvider and have its lifecycle just scoped to that composable so that when I leave that screen the data is destroyed. Maybe that's a bad idea but I think it is not because with fragment one was able to scope the view model to a fragment such that is was destroyed when the fragment lifecycle goes out of scope.
Does that make sense?
amin "such when the fragment is destroyed the view model also goes out of scope"
a

Adam Powell

02/01/2020, 4:34 PM
is there something specific about using
ViewModelProvider
for that that you're looking for, or do you want something with equivalent scoping properties? Combining
remember
,
onCommit
and
onDispose
will give you the scoping properties and cleanup if that's the part you're after
k

Klaas Kabini

02/01/2020, 4:55 PM
I see what you mean @Adam Powell. Yes combining
remember
,
onCommit
and
onDispose
will infact provide me with what I want. Thanks.
👍 1
m

Mihai Popa

02/01/2020, 6:58 PM
Regarding the
Column
issue, in addition to what Adam said, note that you can also create your own
Arrangement
👍 1
3 Views