Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit e720526

Browse files
committed
ComposeMail - Add some comments to the paging implementation
1 parent cf30944 commit e720526

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

demoProjects/ComposeMail/app/src/main/java/com/example/composemail/model/ComposeMailModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ComposeMailModel(application: Application) : AndroidViewModel(application)
4444
val conversations: Flow<PagingData<MailEntryInfo>> = Pager(
4545
config = PagingConfig(
4646
pageSize = LOAD_LIMIT,
47+
// Enable placeholders when loading indicators are supported, see MailItem.kt
4748
enablePlaceholders = true,
4849
prefetchDistance = REFRESH_THRESHOLD,
4950
initialLoadSize = LOAD_LIMIT

demoProjects/ComposeMail/app/src/main/java/com/example/composemail/model/paging/MailsSource.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class MailsSource(private val mailRepo: MailRepository) : PagingSource<Int, Mail
3333
data = nextMails.conversations,
3434
prevKey = if (nextPage == 0) null else nextMails.page - 1,
3535
nextKey = nextMails.page + 1,
36+
// An additional item that will work as a loading placeholder
37+
// while the next page is produced
3638
itemsAfter = 1
3739
)
3840
}

demoProjects/ComposeMail/app/src/main/java/com/example/composemail/ui/mails/MailItem.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,36 @@ import com.example.composemail.model.data.MailEntryInfo
5757
import com.example.composemail.ui.components.CheapText
5858
import com.example.composemail.ui.theme.textBackgroundColor
5959

60+
/**
61+
* Composable to display a mail entry, given by [MailEntryInfo].
62+
*
63+
* When [info] is null, it will display a loading indicator, if the info
64+
* then changes to not-null it will animate the transition to show the info and
65+
* dismiss the loading indicator.
66+
*
67+
* @see PreviewConversationLoading
68+
*/
6069
@Composable
6170
fun MailItem(
6271
modifier: Modifier = Modifier,
6372
state: MailItemState = MailItemState(-1) { _, _ -> },
6473
info: MailEntryInfo?
6574
) {
66-
val csTarget: MotionMailState =
75+
// The layout (as a ConstraintSet ID) we want the Composable to take,
76+
// MotionLayout will animate the transition to that layout
77+
val targetState: MotionMailState =
6778
when {
79+
// No info to display, show a Loading state
6880
info == null -> MotionMailState.Loading
81+
// The item is selected, show as selected
6982
state.isSelected -> MotionMailState.Selected
83+
// The 'normal' state that just displays the given info
7084
else -> MotionMailState.Normal
7185
}
7286
MotionLayoutMail(
7387
modifier = modifier,
7488
info = info ?: MailEntryInfo.Default,
75-
targetState = csTarget,
89+
targetState = targetState,
7690
onSelectedMail = {
7791
// Toggle selection
7892
state.setSelected(!state.isSelected)
@@ -82,6 +96,11 @@ fun MailItem(
8296

8397
const val ANIMATION_DURATION: Int = 400
8498

99+
/**
100+
* An enum that represents the different layout states of the Composable.
101+
*
102+
* Each corresponds to a ConstraintSet in the MotionScene.
103+
*/
85104
enum class MotionMailState(val tag: String) {
86105
Loading("empty"),
87106
Normal("normal"),

demoProjects/ComposeMail/app/src/main/java/com/example/composemail/ui/mails/MailList.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ fun MailList(
3434
listState: MailListState,
3535
observableConversations: Flow<PagingData<MailEntryInfo>>
3636
) {
37+
// The items provided through the model using Pager through a Flow
3738
val lazyMailItems: LazyPagingItems<MailEntryInfo> =
3839
observableConversations.collectAsLazyPagingItems()
3940

4041
LazyColumn(
4142
modifier = modifier,
4243
verticalArrangement = Arrangement.spacedBy(4.dp)
4344
) {
44-
itemsIndexed(lazyMailItems) { _, mailItem ->
45+
itemsIndexed(lazyMailItems) { _, mailInfo ->
46+
// The Pager, configured with placeholders may initially provide
47+
// a null mailInfo when it reaches the current end of the list,
48+
// it will then provide a non-null mailInfo for the same Composable,
49+
// MailItem animates the transition from those two values
4550
MailItem(
46-
info = mailItem,
47-
state = listState.stateFor(mailItem?.id)
51+
info = mailInfo,
52+
state = listState.stateFor(mailInfo?.id)
4853
)
4954
}
5055
}

0 commit comments

Comments
 (0)