Merge pull request #2895 from Isira-Seneviratne/HttpUrl

Use HttpUrl in TextUtils.
This commit is contained in:
Bnyro 2023-01-29 08:28:24 +01:00 committed by GitHub
commit 8a78616114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 35 deletions

View File

@ -1,19 +1,18 @@
package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.databinding.DialogCreatePlaylistBinding
import com.github.libretube.util.TextUtils
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
class CreatePlaylistDialog(
private val onSuccess: () -> Unit = {}
@ -24,14 +23,13 @@ class CreatePlaylistDialog(
binding = DialogCreatePlaylistBinding.inflate(layoutInflater)
binding.clonePlaylist.setOnClickListener {
val playlistUrl = binding.playlistUrl.text.toString()
val playlistId = Uri.parse(playlistUrl).getQueryParameter("list")
if (!TextUtils.validateUrl(playlistUrl) || playlistId == null) {
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
PlaylistsHelper.clonePlaylist(requireContext().applicationContext, playlistId)
val playlistUrl = binding.playlistUrl.text.toString().toHttpUrlOrNull()
playlistUrl?.queryParameter("list")?.let {
PlaylistsHelper.clonePlaylist(requireContext(), it)
dismiss()
} ?: run {
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()
}
}
binding.cancelButton.setOnClickListener {

View File

@ -1,13 +1,12 @@
package com.github.libretube.util
import android.net.Uri
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
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
object TextUtils {
/**
@ -25,19 +24,6 @@ object TextUtils {
*/
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
/**
* Check whether an Url is valid
* @param url The url to test
* @return Whether the URL is valid
*/
fun validateUrl(url: String): Boolean {
runCatching {
URL(url).toURI()
return true
}
return false
}
/**
* Localize the date from a time string
* @param date The date to parse
@ -66,19 +52,12 @@ object TextUtils {
* Get video id if the link is a valid youtube video link
*/
fun getVideoIdFromUri(link: String): String? {
val uri = Uri.parse(link)
if (link.contains("youtube.com")) {
// the link may be in an unsupported format, so we should try/catch it
return try {
uri.getQueryParameter("v")
} catch (e: Exception) {
null
return link.toHttpUrlOrNull()?.let {
when (it.host) {
"www.youtube.com" -> it.queryParameter("v")
"youtu.be" -> it.pathSegments.lastOrNull()
else -> null
}
} else if (link.contains("youtu.be")) {
return uri.lastPathSegment
}
return null
}
}