mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #2895 from Isira-Seneviratne/HttpUrl
Use HttpUrl in TextUtils.
This commit is contained in:
commit
8a78616114
@ -1,19 +1,18 @@
|
|||||||
package com.github.libretube.ui.dialogs
|
package com.github.libretube.ui.dialogs
|
||||||
|
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.PlaylistsHelper
|
import com.github.libretube.api.PlaylistsHelper
|
||||||
import com.github.libretube.databinding.DialogCreatePlaylistBinding
|
import com.github.libretube.databinding.DialogCreatePlaylistBinding
|
||||||
import com.github.libretube.util.TextUtils
|
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
|
||||||
class CreatePlaylistDialog(
|
class CreatePlaylistDialog(
|
||||||
private val onSuccess: () -> Unit = {}
|
private val onSuccess: () -> Unit = {}
|
||||||
@ -24,14 +23,13 @@ class CreatePlaylistDialog(
|
|||||||
binding = DialogCreatePlaylistBinding.inflate(layoutInflater)
|
binding = DialogCreatePlaylistBinding.inflate(layoutInflater)
|
||||||
|
|
||||||
binding.clonePlaylist.setOnClickListener {
|
binding.clonePlaylist.setOnClickListener {
|
||||||
val playlistUrl = binding.playlistUrl.text.toString()
|
val playlistUrl = binding.playlistUrl.text.toString().toHttpUrlOrNull()
|
||||||
val playlistId = Uri.parse(playlistUrl).getQueryParameter("list")
|
playlistUrl?.queryParameter("list")?.let {
|
||||||
if (!TextUtils.validateUrl(playlistUrl) || playlistId == null) {
|
PlaylistsHelper.clonePlaylist(requireContext(), it)
|
||||||
|
dismiss()
|
||||||
|
} ?: run {
|
||||||
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
||||||
return@setOnClickListener
|
|
||||||
}
|
}
|
||||||
PlaylistsHelper.clonePlaylist(requireContext().applicationContext, playlistId)
|
|
||||||
dismiss()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.cancelButton.setOnClickListener {
|
binding.cancelButton.setOnClickListener {
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package com.github.libretube.util
|
package com.github.libretube.util
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import java.net.URL
|
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
import java.time.format.FormatStyle
|
import java.time.format.FormatStyle
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.toJavaLocalDate
|
import kotlinx.datetime.toJavaLocalDate
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
|
||||||
object TextUtils {
|
object TextUtils {
|
||||||
/**
|
/**
|
||||||
@ -25,19 +24,6 @@ object TextUtils {
|
|||||||
*/
|
*/
|
||||||
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
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
|
* Localize the date from a time string
|
||||||
* @param date The date to parse
|
* @param date The date to parse
|
||||||
@ -66,19 +52,12 @@ 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 getVideoIdFromUri(link: String): String? {
|
fun getVideoIdFromUri(link: String): String? {
|
||||||
val uri = Uri.parse(link)
|
return link.toHttpUrlOrNull()?.let {
|
||||||
|
when (it.host) {
|
||||||
if (link.contains("youtube.com")) {
|
"www.youtube.com" -> it.queryParameter("v")
|
||||||
// the link may be in an unsupported format, so we should try/catch it
|
"youtu.be" -> it.pathSegments.lastOrNull()
|
||||||
return try {
|
else -> null
|
||||||
uri.getQueryParameter("v")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
} else if (link.contains("youtu.be")) {
|
|
||||||
return uri.lastPathSegment
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user