mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Fix relative time formatting on Android versions below 7.0.
This commit is contained in:
parent
080dc3df1f
commit
65d07b54a4
@ -123,7 +123,7 @@ class VideosAdapter(
|
||||
R.string.trending_views,
|
||||
video.uploaderName,
|
||||
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) }
|
||||
channelImage.setOnClickListener {
|
||||
@ -154,7 +154,9 @@ class VideosAdapter(
|
||||
videoInfo.text = root.context.getString(
|
||||
R.string.normal_views,
|
||||
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) }
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.icu.text.RelativeDateTimeFormatter
|
||||
import android.os.Build
|
||||
import android.text.format.DateUtils
|
||||
import com.github.libretube.R
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
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 now = LocalDateTime.now()
|
||||
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 (timeFormat, time) = when {
|
||||
months / 12 > 0 -> RelativeDateTimeFormatter.RelativeUnit.YEARS to months / 12
|
||||
months > 0 -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to months
|
||||
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to weeks
|
||||
val years = months / 12
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
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 {
|
||||
DateUtils.getRelativeTimeSpanString(unixTime)
|
||||
}
|
||||
|
@ -456,4 +456,17 @@
|
||||
<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_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>
|
Loading…
Reference in New Issue
Block a user