From 207e6cb6fc72c67997260fd3f1ec5e372b17d3a0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 20 Feb 2023 10:16:58 +0100 Subject: [PATCH] Improved relative date time formatting --- .../libretube/ui/adapters/VideosAdapter.kt | 4 ++-- .../com/github/libretube/util/TextUtils.kt | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 0890a520d..8993fed76 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -142,7 +142,7 @@ class VideosAdapter( video.views.formatShort() + " " + root.context.getString(R.string.views_placeholder) + TextUtils.SEPARATOR + video.uploaded?.let { - DateUtils.getRelativeTimeSpanString(it) + TextUtils.formatRelativeDate(it) } video.duration?.let { thumbnailDuration.setFormattedDuration(it, video.isShort) } channelImage.setOnClickListener { @@ -174,7 +174,7 @@ class VideosAdapter( video.views.formatShort() + " " + root.context.getString(R.string.views_placeholder) + TextUtils.SEPARATOR + video.uploaded?.let { - DateUtils.getRelativeTimeSpanString(it) + TextUtils.formatRelativeDate(it) } thumbnailDuration.text = diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 1ee9ead11..05fb5fdd2 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -1,5 +1,9 @@ package com.github.libretube.util +import android.icu.text.RelativeDateTimeFormatter +import android.os.Build +import android.text.format.DateUtils +import java.time.Instant import java.time.format.DateTimeFormatter import java.time.format.FormatStyle import java.util.* @@ -19,6 +23,10 @@ object TextUtils { */ const val RESERVED_CHARS = "?:\"*|/\\<>\u0000" + private const val weekInMillis: Long = 604800016 + private const val monthInMillis: Long = 2629800000 + private const val yearInMillis: Long = 31557600000 + /** * Localize the date from a time string * @param date The date to parse @@ -51,4 +59,19 @@ object TextUtils { } } } + + fun formatRelativeDate(unixTime: Long): CharSequence { + val timeDiff = Instant.now().toEpochMilli() - unixTime + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && timeDiff > weekInMillis) { + val fmt = RelativeDateTimeFormatter.getInstance() + val (timeFormat, time) = when { + timeDiff >= yearInMillis -> RelativeDateTimeFormatter.RelativeUnit.YEARS to timeDiff / yearInMillis + timeDiff >= monthInMillis -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to timeDiff / monthInMillis + else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to timeDiff / weekInMillis + } + fmt.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat) + } else { + DateUtils.getRelativeTimeSpanString(unixTime) + } + } }