url validity check

This commit is contained in:
Bnyro 2022-06-11 17:07:37 +02:00
parent 97c58edd7f
commit a50eec3a65
2 changed files with 44 additions and 29 deletions

View File

@ -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,7 +36,41 @@ 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 != "") {
try {
// check whether the URL is valid, otherwise catch
val u = URL(instanceApiUrl).toURI()
saveCustomInstance(instanceName, instanceApiUrl)
activity?.recreate()
dismiss()
} catch (e: Exception) {
// invalid URL
Toast.makeText(context, getString(R.string.invalid_url), Toast.LENGTH_SHORT).show()
}
} else {
// at least one empty input
Toast.makeText(
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
).show()
}
}
val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
private fun saveCustomInstance(instanceName: String, instanceApiUrl: String) {
val sharedPreferences = PreferenceManager val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext()) .getDefaultSharedPreferences(requireContext())
@ -65,26 +100,5 @@ class CustomInstanceDialog : DialogFragment() {
.putStringSet("custom_instances_name", HashSet(customInstancesNames)) .putStringSet("custom_instances_name", HashSet(customInstancesNames))
.putStringSet("custom_instances_url", HashSet(customInstancesUrls)) .putStringSet("custom_instances_url", HashSet(customInstancesUrls))
.apply() .apply()
activity?.recreate()
dismiss()
} else {
Toast.makeText(
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
).show()
}
}
val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
} }
} }

View File

@ -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>