android: sort saved posts by creation time

This commit is contained in:
Harsh Shandilya 2022-04-14 18:30:15 +05:30
parent d3c471168b
commit 66718d447a
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 16 additions and 1 deletions

View file

@ -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

View file

@ -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,