2022-11-06 21:05:36 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
2023-01-19 21:38:57 +05:30
|
|
|
import android.net.Uri
|
2022-12-26 21:12:47 +05:30
|
|
|
import java.net.URL
|
2023-01-09 20:51:14 +05:30
|
|
|
import java.time.format.DateTimeFormatter
|
|
|
|
import java.time.format.FormatStyle
|
|
|
|
import java.util.*
|
2023-01-23 00:15:25 +05:30
|
|
|
import kotlin.time.Duration
|
2023-01-18 07:01:06 +05:30
|
|
|
import kotlinx.datetime.LocalDate
|
|
|
|
import kotlinx.datetime.toJavaLocalDate
|
2022-12-26 21:12:47 +05:30
|
|
|
|
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
|
|
|
|
2022-12-01 19:58:41 +05:30
|
|
|
/**
|
|
|
|
* Regex to check for e-mails
|
|
|
|
*/
|
|
|
|
const val EMAIL_REGEX = "^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})"
|
|
|
|
|
2022-12-25 13:17:17 +05:30
|
|
|
/**
|
|
|
|
* Reserved characters by unix which can not be used for file name.
|
|
|
|
*/
|
|
|
|
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
|
|
|
|
2022-12-26 21:12:47 +05:30
|
|
|
/**
|
|
|
|
* Check whether an Url is valid
|
|
|
|
* @param url The url to test
|
|
|
|
* @return Whether the URL is valid
|
|
|
|
*/
|
|
|
|
fun validateUrl(url: String): Boolean {
|
|
|
|
runCatching {
|
|
|
|
URL(url).toURI()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-01-09 20:51:14 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-01-18 07:01:06 +05:30
|
|
|
fun localizeDate(date: LocalDate, locale: Locale): String {
|
2023-01-09 20:51:14 +05:30
|
|
|
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale)
|
2023-01-18 07:01:06 +05:30
|
|
|
return date.toJavaLocalDate().format(formatter)
|
2023-01-09 20:51:14 +05:30
|
|
|
}
|
2023-01-19 21:38:57 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Get time in seconds from a youtube video link
|
2023-01-23 00:15:25 +05:30
|
|
|
* @param t The time string to parse
|
|
|
|
* @return Time in seconds
|
2023-01-19 21:38:57 +05:30
|
|
|
*/
|
2023-01-23 00:15:25 +05:30
|
|
|
fun parseTimestamp(t: String): Long? {
|
|
|
|
if (t.all { c -> c.isDigit() }) {
|
|
|
|
return t.toLong()
|
2023-01-19 21:38:57 +05:30
|
|
|
}
|
|
|
|
|
2023-01-23 00:15:25 +05:30
|
|
|
return Duration.parseOrNull(t)?.inWholeSeconds
|
2023-01-19 21:38:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get video id if the link is a valid youtube video link
|
|
|
|
*/
|
|
|
|
fun getVideoIdFromUri(link: String): String? {
|
|
|
|
val uri = Uri.parse(link)
|
|
|
|
|
|
|
|
if (link.contains("youtube.com")) {
|
|
|
|
// the link may be in an unsupported format, so we should try/catch it
|
|
|
|
return try {
|
|
|
|
uri.getQueryParameter("v")
|
|
|
|
} catch (e: Exception) {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
} else if (link.contains("youtu.be")) {
|
|
|
|
return uri.lastPathSegment
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
2022-11-06 21:05:36 +05:30
|
|
|
}
|