From fd319a0d499f28afb06e2193bd16ea2e1f7455f3 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 29 Jan 2023 07:41:13 +0530 Subject: [PATCH] Use HttpUrl in TextUtils. --- .../ui/dialogs/CreatePlaylistDialog.kt | 14 ++++---- .../com/github/libretube/util/TextUtils.kt | 33 ++++--------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index 5fbce2408..63e1e54d9 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -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) { + 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() - return@setOnClickListener } - PlaylistsHelper.clonePlaylist(requireContext().applicationContext, playlistId) - dismiss() } binding.cancelButton.setOnClickListener { diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 699c45643..1da931d67 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -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 } }