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) { 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 {

View File

@ -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)

View File

@ -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 {