Merge pull request #2829 from Bnyro/master

Fix that clicking description links plays next video
This commit is contained in:
Bnyro 2023-01-22 19:46:32 +01:00 committed by GitHub
commit ab12c86e76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 34 deletions

View File

@ -9,7 +9,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.extensions.TAG
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.util.NavigationHelper
import kotlin.time.Duration
import com.github.libretube.util.TextUtils
class RouterActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -65,14 +65,14 @@ class RouterActivity : BaseActivity() {
intent.putExtra(IntentData.videoId, videoId)
uri.getQueryParameter("t")
?.let { intent.putExtra(IntentData.timeStamp, parseTimestamp(it)) }
?.let { intent.putExtra(IntentData.timeStamp, TextUtils.parseTimestamp(it)) }
}
else -> {
val videoId = uri.path!!.replace("/", "")
intent.putExtra(IntentData.videoId, videoId)
uri.getQueryParameter("t")
?.let { intent.putExtra(IntentData.timeStamp, parseTimestamp(it)) }
?.let { intent.putExtra(IntentData.timeStamp, TextUtils.parseTimestamp(it)) }
}
}
return intent
@ -87,12 +87,4 @@ class RouterActivity : BaseActivity() {
startActivity(resolveType(intent!!, uri))
finishAndRemoveTask()
}
private fun parseTimestamp(t: String): Long? {
if (t.all { c -> c.isDigit() }) {
return t.toLong()
}
return Duration.parseOrNull(t)?.inWholeSeconds
}
}

View File

@ -1057,15 +1057,17 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// not a youtube video link, thus handle normally
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
return
}
// check if the video is the current video and has a valid time
if (videoId == this.videoId) {
// 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)
}
} else {
// youtube video link without time or not the current video, thus open new player
// youtube video link without time or not the current video, thus load in player
playNextVideo(videoId)
}
}

View File

@ -5,6 +5,7 @@ import java.net.URL
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*
import kotlin.time.Duration
import kotlinx.datetime.LocalDate
import kotlinx.datetime.toJavaLocalDate
@ -50,30 +51,15 @@ object TextUtils {
/**
* 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? {
var time = uri.getQueryParameter("t") ?: return -1L
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)
}
}
fun parseTimestamp(t: String): Long? {
if (t.all { c -> c.isDigit() }) {
return t.toLong()
}
// Time may not contain h, m or s. In that case, it is just a number of seconds
if (timeInSeconds == null) {
time.toLongOrNull()?.let {
timeInSeconds = it
}
}
return timeInSeconds
return Duration.parseOrNull(t)?.inWholeSeconds
}
/**