Merge pull request #459 from Bnyro/custom

check custom instance urls for validity
This commit is contained in:
Bnyro 2022-06-11 17:09:31 +02:00 committed by GitHub
commit 44e7d06029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 29 deletions

View File

@ -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,7 +36,43 @@ class CustomInstanceDialog : DialogFragment() {
addInstanceButton.setOnClickListener {
val instanceName = instanceNameEditText.text.toString()
val instanceApiUrl = instanceApiUrlEditText.text.toString()
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
.getDefaultSharedPreferences(requireContext())
@ -65,26 +102,5 @@ class CustomInstanceDialog : DialogFragment() {
.putStringSet("custom_instances_name", HashSet(customInstancesNames))
.putStringSet("custom_instances_url", HashSet(customInstancesUrls))
.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="empty_instance">You have to fill in the name and the API url.</string>
<string name="clear_customInstances">Clear custom instances</string>
<string name="invalid_url">Please enter a valid url</string>
</resources>