mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 10:37:05 +05:30
android: show a sticky header in saved posts
This commit is contained in:
parent
66718d447a
commit
0cb1124640
2 changed files with 46 additions and 7 deletions
|
@ -0,0 +1,31 @@
|
||||||
|
package dev.msfjarvis.claw.android.ui.decorations
|
||||||
|
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.wrapContentHeight
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import dev.msfjarvis.claw.android.ui.surfaceColorAtNavigationBarElevation
|
||||||
|
import java.time.Month
|
||||||
|
import java.time.format.TextStyle as JTextStyle
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun MonthHeader(month: Month) {
|
||||||
|
Box(
|
||||||
|
Modifier.fillMaxWidth()
|
||||||
|
.wrapContentHeight()
|
||||||
|
.background(MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation())
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = month.getDisplayName(JTextStyle.FULL, Locale.getDefault()),
|
||||||
|
style = MaterialTheme.typography.headlineSmall,
|
||||||
|
modifier = Modifier.padding(horizontal = 12.dp, vertical = 12.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +1,18 @@
|
||||||
package dev.msfjarvis.claw.android.ui.lists
|
package dev.msfjarvis.claw.android.ui.lists
|
||||||
|
|
||||||
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.LazyListState
|
import androidx.compose.foundation.lazy.LazyListState
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import dev.msfjarvis.claw.android.ui.asZonedDateTime
|
import dev.msfjarvis.claw.android.ui.asZonedDateTime
|
||||||
|
import dev.msfjarvis.claw.android.ui.decorations.MonthHeader
|
||||||
import dev.msfjarvis.claw.common.posts.PostActions
|
import dev.msfjarvis.claw.common.posts.PostActions
|
||||||
import dev.msfjarvis.claw.common.ui.Divider
|
import dev.msfjarvis.claw.common.ui.Divider
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
|
|
||||||
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun DatabasePosts(
|
fun DatabasePosts(
|
||||||
items: List<SavedPost>,
|
items: List<SavedPost>,
|
||||||
|
@ -18,18 +21,23 @@ fun DatabasePosts(
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
|
val sorted = items.sortedBy { post -> post.createdAt.asZonedDateTime() }
|
||||||
|
val grouped = sorted.groupBy { post -> post.createdAt.asZonedDateTime().month }
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
state = listState,
|
state = listState,
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
) {
|
) {
|
||||||
items(items.sortedBy { post -> post.createdAt.asZonedDateTime() }) { item ->
|
grouped.forEach { (month, posts) ->
|
||||||
ListItem(
|
stickyHeader { MonthHeader(month = month) }
|
||||||
item = item,
|
items(posts) { item ->
|
||||||
isSaved = isSaved,
|
ListItem(
|
||||||
postActions = postActions,
|
item = item,
|
||||||
)
|
isSaved = isSaved,
|
||||||
|
postActions = postActions,
|
||||||
|
)
|
||||||
|
|
||||||
Divider()
|
Divider()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue