mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Use Duration#parse
for parsing timestamps
This commit is contained in:
parent
054846eb22
commit
a04fb31947
@ -9,7 +9,7 @@ import com.github.libretube.constants.IntentData
|
|||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.ui.base.BaseActivity
|
import com.github.libretube.ui.base.BaseActivity
|
||||||
import com.github.libretube.util.NavigationHelper
|
import com.github.libretube.util.NavigationHelper
|
||||||
import kotlin.time.Duration
|
import com.github.libretube.util.TextUtils
|
||||||
|
|
||||||
class RouterActivity : BaseActivity() {
|
class RouterActivity : BaseActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -65,14 +65,14 @@ class RouterActivity : BaseActivity() {
|
|||||||
|
|
||||||
intent.putExtra(IntentData.videoId, videoId)
|
intent.putExtra(IntentData.videoId, videoId)
|
||||||
uri.getQueryParameter("t")
|
uri.getQueryParameter("t")
|
||||||
?.let { intent.putExtra(IntentData.timeStamp, parseTimestamp(it)) }
|
?.let { intent.putExtra(IntentData.timeStamp, TextUtils.parseTimestamp(it)) }
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val videoId = uri.path!!.replace("/", "")
|
val videoId = uri.path!!.replace("/", "")
|
||||||
|
|
||||||
intent.putExtra(IntentData.videoId, videoId)
|
intent.putExtra(IntentData.videoId, videoId)
|
||||||
uri.getQueryParameter("t")
|
uri.getQueryParameter("t")
|
||||||
?.let { intent.putExtra(IntentData.timeStamp, parseTimestamp(it)) }
|
?.let { intent.putExtra(IntentData.timeStamp, TextUtils.parseTimestamp(it)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return intent
|
return intent
|
||||||
@ -87,12 +87,4 @@ class RouterActivity : BaseActivity() {
|
|||||||
startActivity(resolveType(intent!!, uri))
|
startActivity(resolveType(intent!!, uri))
|
||||||
finishAndRemoveTask()
|
finishAndRemoveTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseTimestamp(t: String): Long? {
|
|
||||||
if (t.all { c -> c.isDigit() }) {
|
|
||||||
return t.toLong()
|
|
||||||
}
|
|
||||||
|
|
||||||
return Duration.parseOrNull(t)?.inWholeSeconds
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1063,7 +1063,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
|||||||
// check if the video is the current video and has a valid time
|
// check if the video is the current video and has a valid time
|
||||||
if (videoId == this.videoId) {
|
if (videoId == this.videoId) {
|
||||||
// try finding the time stamp of the url and seek to it if found
|
// try finding the time stamp of the url and seek to it if found
|
||||||
TextUtils.getTimeInSeconds(uri)?.let {
|
TextUtils.parseTimestamp(uri.getQueryParameter("t") ?: return)?.let {
|
||||||
exoPlayer.seekTo(it * 1000)
|
exoPlayer.seekTo(it * 1000)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,6 +5,7 @@ import java.net.URL
|
|||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
import java.time.format.FormatStyle
|
import java.time.format.FormatStyle
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.time.Duration
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.toJavaLocalDate
|
import kotlinx.datetime.toJavaLocalDate
|
||||||
|
|
||||||
@ -50,30 +51,15 @@ object TextUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get time in seconds from a youtube video link
|
* Get time in seconds from a youtube video link
|
||||||
|
* @param t The time string to parse
|
||||||
|
* @return Time in seconds
|
||||||
*/
|
*/
|
||||||
fun getTimeInSeconds(uri: Uri): Long? {
|
fun parseTimestamp(t: String): Long? {
|
||||||
var time = uri.getQueryParameter("t") ?: return -1L
|
if (t.all { c -> c.isDigit() }) {
|
||||||
|
return t.toLong()
|
||||||
var timeInSeconds: Long? = null
|
|
||||||
|
|
||||||
// Find all spans containing hours, minutes or seconds
|
|
||||||
listOf(Pair("h", 60 * 60), Pair("m", 60), Pair("s", 1)).forEach { (separator, timeFactor) ->
|
|
||||||
if (time.contains(separator)) {
|
|
||||||
time.substringBefore(separator).toLongOrNull()?.let {
|
|
||||||
timeInSeconds = (timeInSeconds ?: 0L) + it * timeFactor
|
|
||||||
time = time.substringAfter(separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time may not contain h, m or s. In that case, it is just a number of seconds
|
return Duration.parseOrNull(t)?.inWholeSeconds
|
||||||
if (timeInSeconds == null) {
|
|
||||||
time.toLongOrNull()?.let {
|
|
||||||
timeInSeconds = it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return timeInSeconds
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user