Merge pull request #3225 from Isira-Seneviratne/Fix_relative_formatting

Fix relative time formatting on Android versions below 7.0.
This commit is contained in:
Bnyro 2023-03-03 17:11:50 +01:00 committed by GitHub
commit eb6aece3d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 10 deletions

View File

@ -123,7 +123,7 @@ class VideosAdapter(
R.string.trending_views, R.string.trending_views,
video.uploaderName, video.uploaderName,
video.views.formatShort(), video.views.formatShort(),
video.uploaded?.let { TextUtils.formatRelativeDate(it) } video.uploaded?.let { TextUtils.formatRelativeDate(root.context, it) }
) )
video.duration?.let { thumbnailDuration.setFormattedDuration(it, video.isShort) } video.duration?.let { thumbnailDuration.setFormattedDuration(it, video.isShort) }
channelImage.setOnClickListener { channelImage.setOnClickListener {
@ -154,7 +154,9 @@ class VideosAdapter(
videoInfo.text = root.context.getString( videoInfo.text = root.context.getString(
R.string.normal_views, R.string.normal_views,
video.views.formatShort(), video.views.formatShort(),
video.uploaded?.let { TextUtils.SEPARATOR + TextUtils.formatRelativeDate(it) } video.uploaded?.let {
TextUtils.SEPARATOR + TextUtils.formatRelativeDate(root.context, it)
}
) )
thumbnailDuration.text = video.duration?.let { DateUtils.formatElapsedTime(it) } thumbnailDuration.text = video.duration?.let { DateUtils.formatElapsedTime(it) }

View File

@ -1,8 +1,10 @@
package com.github.libretube.util package com.github.libretube.util
import android.content.Context
import android.icu.text.RelativeDateTimeFormatter import android.icu.text.RelativeDateTimeFormatter
import android.os.Build import android.os.Build
import android.text.format.DateUtils import android.text.format.DateUtils
import com.github.libretube.R
import java.time.Instant import java.time.Instant
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
@ -61,20 +63,31 @@ object TextUtils {
} }
} }
fun formatRelativeDate(unixTime: Long): CharSequence { fun formatRelativeDate(context: Context, unixTime: Long): CharSequence {
val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault()) val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault())
val now = LocalDateTime.now() val now = LocalDateTime.now()
val weeks = date.until(now, ChronoUnit.WEEKS) val weeks = date.until(now, ChronoUnit.WEEKS)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && weeks >= 1) { return if (weeks > 0) {
val months = date.until(now, ChronoUnit.MONTHS) val months = date.until(now, ChronoUnit.MONTHS)
val (timeFormat, time) = when { val years = months / 12
months / 12 > 0 -> RelativeDateTimeFormatter.RelativeUnit.YEARS to months / 12
months > 0 -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to months if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to weeks val (timeFormat, time) = when {
years > 0 -> RelativeDateTimeFormatter.RelativeUnit.YEARS to years
months > 0 -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to months
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to weeks
}
RelativeDateTimeFormatter.getInstance()
.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat)
} else {
val (timeAgoRes, time) = when {
years > 0 -> R.plurals.years_ago to years
months > 0 -> R.plurals.months_ago to months
else -> R.plurals.weeks_ago to weeks
}
context.resources.getQuantityString(timeAgoRes, time.toInt(), time)
} }
RelativeDateTimeFormatter.getInstance()
.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat)
} else { } else {
DateUtils.getRelativeTimeSpanString(unixTime) DateUtils.getRelativeTimeSpanString(unixTime)
} }

View File

@ -456,4 +456,17 @@
<string name="background_channel_description">Shows a notification with buttons to control the audio player.</string> <string name="background_channel_description">Shows a notification with buttons to control the audio player.</string>
<string name="push_channel_name">Notification Worker</string> <string name="push_channel_name">Notification Worker</string>
<string name="push_channel_description">Shows a notification when new streams are available.</string> <string name="push_channel_description">Shows a notification when new streams are available.</string>
<!-- Relative time formatting strings (remove when setting the minSdk to 24) -->
<plurals name="years_ago">
<item quantity="one">1 year ago</item>
<item quantity="other">%d years ago</item>
</plurals>
<plurals name="months_ago">
<item quantity="one">1 month ago</item>
<item quantity="other">%d months ago</item>
</plurals>
<plurals name="weeks_ago">
<item quantity="one">1 week ago</item>
<item quantity="other">%d weeks ago</item>
</plurals>
</resources> </resources>