Skip to content

Commit

Permalink
Display absolute time in feed (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp authored Jan 8, 2025
1 parent b7bce47 commit 017a584
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fun rememberCurrentTime(): LocalDateTime {
LaunchedEffect(Unit) {
while (true) {
currentTime = LocalDateTime.now()
delay(30_000)
delay(30 * 60 * 1_000)
}
}

Expand Down
12 changes: 4 additions & 8 deletions app/src/main/java/com/capyreader/app/ui/articles/RelativeTime.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.capyreader.app.ui.articles

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.capyreader.app.R
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZonedDateTime
Expand All @@ -16,15 +14,13 @@ fun relativeTime(
currentTime: LocalDateTime,
): String {
val local = time.withZoneSameInstant(TimeZone.getDefault().toZoneId()).toLocalDateTime()
val format = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
val timeFormat = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
val dateFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)

val duration = Duration.between(local, currentTime)

return when {
duration.toMinutes() < 1 -> stringResource(R.string.relative_time_just_now)
duration.toMinutes() < 60 -> stringResource(R.string.relative_time_minutes, duration.toMinutes())
duration.toHours() < 24 -> stringResource(R.string.relative_time_hours, duration.toHours())
duration.toDays() < 7 -> stringResource(R.string.relative_time_days, duration.toDays())
else -> format.format(local)
duration.toHours() < 24 -> timeFormat.format(local)
else -> dateFormat.format(local)
}
}

0 comments on commit 017a584

Please sign in to comment.