LibreTube/app/src/main/java/com/github/libretube/util/TextUtils.kt

101 lines
3.4 KiB
Kotlin
Raw Normal View History

2022-11-06 21:05:36 +05:30
package com.github.libretube.util
import android.content.Context
2023-02-20 14:46:58 +05:30
import android.icu.text.RelativeDateTimeFormatter
import android.os.Build
import android.text.format.DateUtils
import com.github.libretube.R
2023-02-20 14:46:58 +05:30
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.ChronoUnit
import kotlin.time.Duration
import kotlinx.datetime.LocalDate
import kotlinx.datetime.toJavaLocalDate
2023-01-29 07:41:13 +05:30
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
2022-11-06 21:05:36 +05:30
object TextUtils {
/**
* Separator used for descriptions
*/
const val SEPARATOR = ""
2022-11-09 22:31:59 +05:30
/**
* Reserved characters by unix which can not be used for file name.
*/
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
/**
2023-03-01 19:49:27 +05:30
* Date time formatter which uses the [FormatStyle.MEDIUM] format style.
*/
private val MEDIUM_DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
/**
* Localize the date from a date string, using the medium format.
* @param date The date to parse
2023-03-01 19:49:27 +05:30
* @return localized date string
*/
2023-03-01 19:49:27 +05:30
fun localizeDate(date: LocalDate): String {
return date.toJavaLocalDate().format(MEDIUM_DATE_FORMATTER)
}
/**
* Get time in seconds from a youtube video link
* @param t The time string to parse
* @return Time in seconds
*/
fun parseTimestamp(t: String): Long? {
2023-02-01 05:44:27 +05:30
return t.toLongOrNull() ?: Duration.parseOrNull(t)?.inWholeSeconds
}
/**
* Get video id if the link is a valid youtube video link
*/
fun getVideoIdFromUri(link: String): String? {
2023-01-29 07:41:13 +05:30
return link.toHttpUrlOrNull()?.let {
when (it.host) {
"www.youtube.com" -> it.queryParameter("v")
"youtu.be" -> it.pathSegments.lastOrNull()
else -> null
}
}
}
2023-02-20 14:46:58 +05:30
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 (weeks > 0) {
val months = date.until(now, ChronoUnit.MONTHS)
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)
2023-02-20 14:46:58 +05:30
}
} else {
DateUtils.getRelativeTimeSpanString(unixTime)
}
}
2023-02-22 15:45:43 +05:30
fun formatBitrate(bitrate: Int?): String {
bitrate ?: return ""
return "${bitrate / 1024}kbps"
}
2022-11-06 21:05:36 +05:30
}