refactor: Simplify getVideoIdFromUrl

This commit is contained in:
Isira Seneviratne 2024-01-22 07:03:03 +05:30
parent fd7ec998c4
commit 28e653120f
3 changed files with 13 additions and 22 deletions

View File

@ -1113,10 +1113,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
*/
private fun handleLink(link: String) {
// get video id if the link is a valid youtube video link
val videoId = TextUtils.getVideoIdFromUrl(link)
val uri = link.toUri()
val videoId = TextUtils.getVideoIdFromUri(uri)
if (videoId.isNullOrEmpty()) {
// not a YouTube video link, thus handle normally
val intent = Intent(Intent.ACTION_VIEW, link.toUri())
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
return
}
@ -1124,7 +1126,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// 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
link.toUri().getQueryParameter("t")?.toTimeInSeconds()?.let {
uri.getQueryParameter("t")?.toTimeInSeconds()?.let {
exoPlayer.seekTo(it * 1000)
}
} else {

View File

@ -1,5 +1,6 @@
package com.github.libretube.ui.models
import androidx.core.net.toUri
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
@ -13,15 +14,12 @@ import com.github.libretube.util.TextUtils
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flatMapLatest
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
class SearchResultViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
private val args = SearchResultFragmentArgs.fromSavedStateHandle(savedStateHandle)
// parse search URLs from YouTube entered in the search bar
private val searchQuery = args.query.toHttpUrlOrNull()?.let {
val videoId = TextUtils.getVideoIdFromUrl(it.toString()) ?: args.query
"${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId"
} ?: args.query
private val videoId = TextUtils.getVideoIdFromUri(args.query.toUri()) ?: args.query
private val searchQuery = "${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId"
private val filterMutableData = MutableStateFlow("all")
@OptIn(ExperimentalCoroutinesApi::class)

View File

@ -2,10 +2,10 @@ package com.github.libretube.util
import android.content.Context
import android.icu.text.RelativeDateTimeFormatter
import android.net.Uri
import android.os.Build
import android.text.format.DateUtils
import com.github.libretube.R
import com.github.libretube.ui.dialogs.ShareDialog
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
@ -15,8 +15,6 @@ import java.time.temporal.ChronoUnit
import kotlin.time.Duration
import kotlinx.datetime.LocalDate as KotlinLocalDate
import kotlinx.datetime.toJavaLocalDate
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
object TextUtils {
/**
@ -54,18 +52,11 @@ object TextUtils {
/**
* Get video id if the link is a valid youtube video link
*/
fun getVideoIdFromUrl(link: String): String? {
val mainPipedFrontendUrl = ShareDialog.PIPED_FRONTEND_URL.toHttpUrl().host
val unShortenedHosts = listOf("www.youtube.com", "m.youtube.com", mainPipedFrontendUrl)
return link.toHttpUrlOrNull()?.let {
when (it.host) {
in unShortenedHosts -> it.queryParameter("v")
"youtu.be" -> it.pathSegments.lastOrNull()
fun getVideoIdFromUri(uri: Uri) = when (uri.host) {
"www.youtube.com", "m.youtube.com", "piped.video" -> uri.getQueryParameter("v")
"youtu.be" -> uri.lastPathSegment
else -> null
}
}
}
fun formatRelativeDate(context: Context, unixTime: Long): CharSequence {
val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault())