mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
refactor: Simplify getVideoIdFromUrl
This commit is contained in:
parent
fd7ec998c4
commit
28e653120f
@ -1113,10 +1113,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
*/
|
*/
|
||||||
private fun handleLink(link: String) {
|
private fun handleLink(link: String) {
|
||||||
// get video id if the link is a valid youtube video link
|
// 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()) {
|
if (videoId.isNullOrEmpty()) {
|
||||||
// not a YouTube video link, thus handle normally
|
// 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)
|
startActivity(intent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1124,7 +1126,7 @@ class PlayerFragment : Fragment(), 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
|
||||||
link.toUri().getQueryParameter("t")?.toTimeInSeconds()?.let {
|
uri.getQueryParameter("t")?.toTimeInSeconds()?.let {
|
||||||
exoPlayer.seekTo(it * 1000)
|
exoPlayer.seekTo(it * 1000)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.github.libretube.ui.models
|
package com.github.libretube.ui.models
|
||||||
|
|
||||||
|
import androidx.core.net.toUri
|
||||||
import androidx.lifecycle.SavedStateHandle
|
import androidx.lifecycle.SavedStateHandle
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
@ -13,15 +14,12 @@ import com.github.libretube.util.TextUtils
|
|||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
||||||
|
|
||||||
class SearchResultViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
|
class SearchResultViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
|
||||||
private val args = SearchResultFragmentArgs.fromSavedStateHandle(savedStateHandle)
|
private val args = SearchResultFragmentArgs.fromSavedStateHandle(savedStateHandle)
|
||||||
// parse search URLs from YouTube entered in the search bar
|
// parse search URLs from YouTube entered in the search bar
|
||||||
private val searchQuery = args.query.toHttpUrlOrNull()?.let {
|
private val videoId = TextUtils.getVideoIdFromUri(args.query.toUri()) ?: args.query
|
||||||
val videoId = TextUtils.getVideoIdFromUrl(it.toString()) ?: args.query
|
private val searchQuery = "${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId"
|
||||||
"${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId"
|
|
||||||
} ?: args.query
|
|
||||||
|
|
||||||
private val filterMutableData = MutableStateFlow("all")
|
private val filterMutableData = MutableStateFlow("all")
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
@ -2,10 +2,10 @@ package com.github.libretube.util
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.icu.text.RelativeDateTimeFormatter
|
import android.icu.text.RelativeDateTimeFormatter
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.ui.dialogs.ShareDialog
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
@ -15,8 +15,6 @@ import java.time.temporal.ChronoUnit
|
|||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
import kotlinx.datetime.LocalDate as KotlinLocalDate
|
import kotlinx.datetime.LocalDate as KotlinLocalDate
|
||||||
import kotlinx.datetime.toJavaLocalDate
|
import kotlinx.datetime.toJavaLocalDate
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
||||||
|
|
||||||
object TextUtils {
|
object TextUtils {
|
||||||
/**
|
/**
|
||||||
@ -54,17 +52,10 @@ object TextUtils {
|
|||||||
/**
|
/**
|
||||||
* Get video id if the link is a valid youtube video link
|
* Get video id if the link is a valid youtube video link
|
||||||
*/
|
*/
|
||||||
fun getVideoIdFromUrl(link: String): String? {
|
fun getVideoIdFromUri(uri: Uri) = when (uri.host) {
|
||||||
val mainPipedFrontendUrl = ShareDialog.PIPED_FRONTEND_URL.toHttpUrl().host
|
"www.youtube.com", "m.youtube.com", "piped.video" -> uri.getQueryParameter("v")
|
||||||
val unShortenedHosts = listOf("www.youtube.com", "m.youtube.com", mainPipedFrontendUrl)
|
"youtu.be" -> uri.lastPathSegment
|
||||||
|
else -> null
|
||||||
return link.toHttpUrlOrNull()?.let {
|
|
||||||
when (it.host) {
|
|
||||||
in unShortenedHosts -> it.queryParameter("v")
|
|
||||||
"youtu.be" -> it.pathSegments.lastOrNull()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun formatRelativeDate(context: Context, unixTime: Long): CharSequence {
|
fun formatRelativeDate(context: Context, unixTime: Long): CharSequence {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user