Fix DateTimeAgo in future
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user