From a50eec3a6585593a10ec084a429311203399c5a4 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 11 Jun 2022 17:07:37 +0200 Subject: [PATCH 1/2] url validity check --- .../libretube/dialogs/CustomInstanceDialog.kt | 72 +++++++++++-------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt index dfeef48e5..ed5fafe25 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt @@ -14,6 +14,7 @@ import androidx.preference.PreferenceManager import com.github.libretube.R import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.textfield.TextInputEditText +import java.net.URL class CustomInstanceDialog : DialogFragment() { val TAG = "CustomInstanceDialog" @@ -35,39 +36,20 @@ class CustomInstanceDialog : DialogFragment() { addInstanceButton.setOnClickListener { val instanceName = instanceNameEditText.text.toString() val instanceApiUrl = instanceApiUrlEditText.text.toString() + if (instanceName != "" && instanceApiUrl != "") { - val sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(requireContext()) - - // get the names of the other custom instances - var customInstancesNames = try { - sharedPreferences - .getStringSet("custom_instances_name", HashSet())!!.toList() + try { + // check whether the URL is valid, otherwise catch + val u = URL(instanceApiUrl).toURI() + saveCustomInstance(instanceName, instanceApiUrl) + activity?.recreate() + dismiss() } catch (e: Exception) { - emptyList() + // invalid URL + Toast.makeText(context, getString(R.string.invalid_url), Toast.LENGTH_SHORT).show() } - - // get the urls of the other custom instances - var customInstancesUrls = try { - sharedPreferences - .getStringSet("custom_instances_url", HashSet())!!.toList() - } catch (e: Exception) { - emptyList() - } - - // append new instance to the list - customInstancesNames += instanceName - customInstancesUrls += instanceApiUrl - Log.e(TAG, customInstancesNames.toString()) - - // save them to the shared preferences - sharedPreferences.edit() - .putStringSet("custom_instances_name", HashSet(customInstancesNames)) - .putStringSet("custom_instances_url", HashSet(customInstancesUrls)) - .apply() - activity?.recreate() - dismiss() } else { + // at least one empty input Toast.makeText( context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT ).show() @@ -87,4 +69,36 @@ class CustomInstanceDialog : DialogFragment() { builder.create() } ?: throw IllegalStateException("Activity cannot be null") } + + private fun saveCustomInstance(instanceName: String, instanceApiUrl: String) { + val sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(requireContext()) + + // get the names of the other custom instances + var customInstancesNames = try { + sharedPreferences + .getStringSet("custom_instances_name", HashSet())!!.toList() + } catch (e: Exception) { + emptyList() + } + + // get the urls of the other custom instances + var customInstancesUrls = try { + sharedPreferences + .getStringSet("custom_instances_url", HashSet())!!.toList() + } catch (e: Exception) { + emptyList() + } + + // append new instance to the list + customInstancesNames += instanceName + customInstancesUrls += instanceApiUrl + Log.e(TAG, customInstancesNames.toString()) + + // save them to the shared preferences + sharedPreferences.edit() + .putStringSet("custom_instances_name", HashSet(customInstancesNames)) + .putStringSet("custom_instances_url", HashSet(customInstancesUrls)) + .apply() + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 301d8a87f..d0f0bc7a0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -168,4 +168,5 @@ Add Instance You have to fill in the name and the API url. Clear custom instances + Please enter a valid url From 4e241e56d61accf87226e81df5ab70ab3a58a26a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 11 Jun 2022 17:08:05 +0200 Subject: [PATCH 2/2] ktlint --- .../java/com/github/libretube/dialogs/CustomInstanceDialog.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt index ed5fafe25..945a3f405 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt @@ -46,7 +46,9 @@ class CustomInstanceDialog : DialogFragment() { dismiss() } catch (e: Exception) { // invalid URL - Toast.makeText(context, getString(R.string.invalid_url), Toast.LENGTH_SHORT).show() + Toast.makeText( + context, getString(R.string.invalid_url), Toast.LENGTH_SHORT + ).show() } } else { // at least one empty input