mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
url validity check
This commit is contained in:
parent
97c58edd7f
commit
a50eec3a65
@ -14,6 +14,7 @@ import androidx.preference.PreferenceManager
|
|||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.google.android.material.textfield.TextInputEditText
|
import com.google.android.material.textfield.TextInputEditText
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
class CustomInstanceDialog : DialogFragment() {
|
class CustomInstanceDialog : DialogFragment() {
|
||||||
val TAG = "CustomInstanceDialog"
|
val TAG = "CustomInstanceDialog"
|
||||||
@ -35,39 +36,20 @@ class CustomInstanceDialog : DialogFragment() {
|
|||||||
addInstanceButton.setOnClickListener {
|
addInstanceButton.setOnClickListener {
|
||||||
val instanceName = instanceNameEditText.text.toString()
|
val instanceName = instanceNameEditText.text.toString()
|
||||||
val instanceApiUrl = instanceApiUrlEditText.text.toString()
|
val instanceApiUrl = instanceApiUrlEditText.text.toString()
|
||||||
|
|
||||||
if (instanceName != "" && instanceApiUrl != "") {
|
if (instanceName != "" && instanceApiUrl != "") {
|
||||||
val sharedPreferences = PreferenceManager
|
try {
|
||||||
.getDefaultSharedPreferences(requireContext())
|
// check whether the URL is valid, otherwise catch
|
||||||
|
val u = URL(instanceApiUrl).toURI()
|
||||||
// get the names of the other custom instances
|
saveCustomInstance(instanceName, instanceApiUrl)
|
||||||
var customInstancesNames = try {
|
activity?.recreate()
|
||||||
sharedPreferences
|
dismiss()
|
||||||
.getStringSet("custom_instances_name", HashSet())!!.toList()
|
|
||||||
} catch (e: Exception) {
|
} 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 {
|
} else {
|
||||||
|
// at least one empty input
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
|
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
|
||||||
).show()
|
).show()
|
||||||
@ -87,4 +69,36 @@ class CustomInstanceDialog : DialogFragment() {
|
|||||||
builder.create()
|
builder.create()
|
||||||
} ?: throw IllegalStateException("Activity cannot be null")
|
} ?: 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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,4 +168,5 @@
|
|||||||
<string name="addInstance">Add Instance</string>
|
<string name="addInstance">Add Instance</string>
|
||||||
<string name="empty_instance">You have to fill in the name and the API url.</string>
|
<string name="empty_instance">You have to fill in the name and the API url.</string>
|
||||||
<string name="clear_customInstances">Clear custom instances</string>
|
<string name="clear_customInstances">Clear custom instances</string>
|
||||||
|
<string name="invalid_url">Please enter a valid url</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user