mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 08:17:04 +05:30
android: sort saved posts by creation time
This commit is contained in:
parent
d3c471168b
commit
66718d447a
2 changed files with 16 additions and 1 deletions
|
@ -29,6 +29,10 @@ import dev.msfjarvis.claw.common.theme.DarkThemeColors
|
|||
import dev.msfjarvis.claw.common.theme.LightThemeColors
|
||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.Locale
|
||||
import kotlin.math.ln
|
||||
|
||||
private const val AnimationDuration = 100
|
||||
|
@ -49,6 +53,16 @@ fun slideOutAnimation(): ExitTransition {
|
|||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a given [String] into a [ZonedDateTime]. This method only works on dates in the format
|
||||
* returned by the Lobsters API, and is not a general purpose parsing solution.
|
||||
*/
|
||||
fun String.asZonedDateTime(): ZonedDateTime {
|
||||
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US)
|
||||
val date = checkNotNull(sdf.parse(this))
|
||||
return date.toInstant().atZone(ZoneId.systemDefault())
|
||||
}
|
||||
|
||||
// The destination needs to be tracked like this rather than used directly since
|
||||
// `NavController#currentDestination` is not a Composable state.
|
||||
@Composable
|
||||
|
|
|
@ -5,6 +5,7 @@ import androidx.compose.foundation.lazy.LazyListState
|
|||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import dev.msfjarvis.claw.android.ui.asZonedDateTime
|
||||
import dev.msfjarvis.claw.common.posts.PostActions
|
||||
import dev.msfjarvis.claw.common.ui.Divider
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
|
@ -21,7 +22,7 @@ fun DatabasePosts(
|
|||
state = listState,
|
||||
modifier = modifier,
|
||||
) {
|
||||
items(items) { item ->
|
||||
items(items.sortedBy { post -> post.createdAt.asZonedDateTime() }) { item ->
|
||||
ListItem(
|
||||
item = item,
|
||||
isSaved = isSaved,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue