Fix DateTimeAgo in future

This commit is contained in:
Koitharu
2025-06-21 18:15:40 +03:00
parent 55ea0d7b2b
commit 61b863ae96
4 changed files with 18 additions and 6 deletions

View File

@@ -10,13 +10,13 @@ import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit
fun calculateTimeAgo(instant: Instant, showMonths: Boolean = false): DateTimeAgo {
// TODO: Use Java 9's LocalDate.ofInstant().
fun calculateTimeAgo(instant: Instant, showMonths: Boolean = false): DateTimeAgo? {
val localDate = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate()
val now = LocalDate.now()
val diffDays = localDate.until(now, ChronoUnit.DAYS)
return when {
diffDays < 0 -> null // in future, probably a bug, not supported
diffDays == 0L -> {
if (instant.until(Instant.now(), ChronoUnit.MINUTES) < 3) DateTimeAgo.JustNow
else DateTimeAgo.Today

View File

@@ -225,7 +225,11 @@ class DownloadsViewModel @Inject constructor(
else -> {
val date = calculateTimeAgo(item.timestamp)
if (prevDate != date) {
destination += ListHeader(date)
destination += if (date != null) {
ListHeader(date)
} else {
ListHeader(R.string.unknown)
}
}
prevDate = date
destination += item

View File

@@ -201,10 +201,14 @@ class HistoryListViewModel @Inject constructor(
private fun MangaHistory.header(order: ListSortOrder): ListHeader? = when (order) {
ListSortOrder.LAST_READ,
ListSortOrder.LONG_AGO_READ -> ListHeader(calculateTimeAgo(updatedAt))
ListSortOrder.LONG_AGO_READ -> calculateTimeAgo(updatedAt)?.let {
ListHeader(it)
} ?: ListHeader(R.string.unknown)
ListSortOrder.OLDEST,
ListSortOrder.NEWEST -> ListHeader(calculateTimeAgo(createdAt))
ListSortOrder.NEWEST -> calculateTimeAgo(createdAt)?.let {
ListHeader(it)
} ?: ListHeader(R.string.unknown)
ListSortOrder.UNREAD,
ListSortOrder.PROGRESS -> ListHeader(

View File

@@ -137,7 +137,11 @@ class FeedViewModel @Inject constructor(
for (item in this) {
val date = calculateTimeAgo(item.createdAt)
if (prevDate != date) {
destination += ListHeader(date)
destination += if (date != null) {
ListHeader(date)
} else {
ListHeader(R.string.unknown)
}
}
prevDate = date
destination += item.toFeedItem()