Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -47,6 +48,8 @@ import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -72,6 +75,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.w3c.dom.Text

private object ListsSnippetsColumn {
// [START android_compose_layouts_list_column]
Expand Down Expand Up @@ -644,7 +648,9 @@ fun LazyStaggeredGridSnippet() {
model = photo,
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier.fillMaxWidth().wrapContentHeight()
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
)
}
},
Expand All @@ -666,15 +672,17 @@ fun LazyStaggeredGridSnippetFixed() {
model = photo,
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier.fillMaxWidth().wrapContentHeight()
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
)
}
},
modifier = Modifier.fillMaxSize()
)
// [END android_compose_layouts_lazy_staggered_grid_fixed]
}
private class Message(val id: Long)
class Message(val id: Long)
private class Item

private data class Contact(val firstName: String)
Expand Down Expand Up @@ -742,3 +750,53 @@ private val randomSizedPhotos = listOf(
randomSampleImageUrl(width = 1600, height = 900),
randomSampleImageUrl(width = 500, height = 500),
)
// [START android_compose_layouts_lazily_load_list]
@Composable
fun MessageList(
modifier: Modifier,
pager: Pager<Int, Message>
) {
val lazyPagingItems = pager.flow.collectAsLazyPagingItems()

LazyColumn {
items(
lazyPagingItems.itemCount,
key = lazyPagingItems.itemKey { it.id }
) { index ->
val message = lazyPagingItems[index]
if (message != null) {
MessageRow(message)
} else {
MessagePlaceholder()
}
}
}

@Composable
fun MessagePlaceholder(modifier: Modifier) {
Box(
Modifier
.fillMaxWidth()
.height(48.dp)
) {
CircularProgressIndicator()
}
}

@Composable
fun MessageRow(
modifier: Modifier,
message: Text
) {
Card(modifier = Modifier.padding(8.dp)) {
Column(
modifier = Modifier.padding(8.dp),
verticalArrangement = Arrangement.Center
) {
// Text(message.sender), where "message" is an object with a sender property
// Text(message.text), where "text" is an object with a text property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be uncommented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Because there's no actual message class anywhere that has a sender or text property, and this example was too complex for me to set something up idiomatically (at least in a time-efficient way).

If you feel it's necessary to have this in code, could I ask you to implement it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a simple class to represent the message structure is a common practice in DAC snippets. It helps make the code compile and snippets readable instead of the code being commented out.

While it's not strictly necessary for this particular example as you could just put a Text("Lazy list text"), it could be a good opportunity to practice this pattern, as it's frequently used in other snippets and real-world scenarios.

Would you be open to giving it a try? I'm happy to provide guidance and support if you need it.

Something like a dummy class like this would match this fake class:

data class Message(sender: String, text: String)

Copy link
Contributor Author

@thedmail thedmail Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure--thank you for helping me learn!!

There's actually a reason why I didn't go ahead and create a data class as you're suggesting. What has me a little mixed-up are these lines:

lazyPagingItems = pager.flow.collectAsLazyPagingItems()

message = lazyPagingItems[index]

So I don't see how I can make message be an object of a new data class (such as I'd create) when it's already a variable set equal of LazyPagingItems type.

This was the complexity that stopped me from just defining my own class, and instead had me resort to commenting.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it for you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tyvm

}
}
}
}
// [END android_compose_layouts_lazily_load_list]