Localize the date in the PlayerFragment

This commit is contained in:
Bnyro 2023-01-09 16:21:14 +01:00
parent 8735f7ae8c
commit 99182b34af
2 changed files with 36 additions and 8 deletions

View File

@ -107,15 +107,15 @@ import com.google.android.exoplayer2.ui.StyledPlayerView
import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.upstream.DefaultDataSource
import com.google.android.exoplayer2.util.MimeTypes import com.google.android.exoplayer2.util.MimeTypes
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.chromium.net.CronetEngine import org.chromium.net.CronetEngine
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
class PlayerFragment : BaseFragment(), OnlinePlayerOptions { class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
@ -478,7 +478,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
private fun toggleDescription() { private fun toggleDescription() {
var viewInfo = if (!isLive) TextUtils.SEPARATOR + streams.uploadDate else "" var viewInfo = if (!isLive) TextUtils.SEPARATOR + localizedDate(streams.uploadDate) else ""
if (binding.descLinLayout.isVisible) { if (binding.descLinLayout.isVisible) {
// hide the description and chapters // hide the description and chapters
binding.playerDescriptionArrow.animate().rotation(0F).setDuration(250).start() binding.playerDescriptionArrow.animate().rotation(0F).setDuration(250).start()
@ -791,6 +791,12 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
} }
private fun localizedDate(date: String?): String? {
return if (SDK_INT >= Build.VERSION_CODES.N) {
TextUtils.localizeDate(date, resources.configuration.locales[0])
} else TextUtils.localizeDate(date)
}
private fun handleLiveVideo() { private fun handleLiveVideo() {
playerBinding.exoPosition.visibility = View.GONE playerBinding.exoPosition.visibility = View.GONE
playerBinding.liveDiff.visibility = View.VISIBLE playerBinding.liveDiff.visibility = View.VISIBLE
@ -814,7 +820,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.apply { binding.apply {
playerViewsInfo.text = playerViewsInfo.text =
context?.getString(R.string.views, streams.views.formatShort()) + context?.getString(R.string.views, streams.views.formatShort()) +
if (!isLive) TextUtils.SEPARATOR + streams.uploadDate else "" if (!isLive) TextUtils.SEPARATOR + localizedDate(streams.uploadDate) else ""
textLike.text = streams.likes.formatShort() textLike.text = streams.likes.formatShort()
textDislike.text = streams.dislikes.formatShort() textDislike.text = streams.dislikes.formatShort()
@ -1163,8 +1169,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
for (vid in videoStreams) { for (vid in videoStreams) {
if (resolutions.any { if (resolutions.any {
it.resolution == vid.quality.qualityToInt() it.resolution == vid.quality.qualityToInt()
} || vid.url == null } || vid.url == null
) { ) {
continue continue
} }

View File

@ -1,6 +1,10 @@
package com.github.libretube.util package com.github.libretube.util
import java.net.URL import java.net.URL
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*
object TextUtils { object TextUtils {
/** /**
@ -29,4 +33,22 @@ object TextUtils {
} }
return false return false
} }
/**
* Localize the date from a time string
* @param date The date to parse
* @param locale The locale to use, otherwise uses system default
* return Localized date string
*/
fun localizeDate(date: String?, locale: Locale? = null): String? {
date ?: return null
// relative time span
if (!date.contains("-")) return date
val dateObj = LocalDate.parse(date)
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale)
return dateObj.format(formatter)
}
} }