mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #3026 from Isira-Seneviratne/URL_validation
Simplify custom instance URL validation.
This commit is contained in:
commit
32533ca11c
@ -11,56 +11,37 @@ import com.github.libretube.db.DatabaseHolder.Database
|
|||||||
import com.github.libretube.db.obj.CustomInstance
|
import com.github.libretube.db.obj.CustomInstance
|
||||||
import com.github.libretube.extensions.query
|
import com.github.libretube.extensions.query
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import java.net.URL
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
|
||||||
class CustomInstanceDialog : DialogFragment() {
|
class CustomInstanceDialog : DialogFragment() {
|
||||||
private lateinit var binding: DialogCustomInstanceBinding
|
|
||||||
|
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
binding = DialogCustomInstanceBinding.inflate(layoutInflater)
|
val binding = DialogCustomInstanceBinding.inflate(layoutInflater)
|
||||||
|
|
||||||
binding.cancel.setOnClickListener {
|
binding.cancel.setOnClickListener {
|
||||||
dismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.addInstance.setOnClickListener {
|
binding.addInstance.setOnClickListener {
|
||||||
val customInstance = CustomInstance(
|
val instanceName = binding.instanceName.text.toString()
|
||||||
name = binding.instanceName.text.toString(),
|
val apiUrl = binding.instanceApiUrl.text.toString()
|
||||||
apiUrl = binding.instanceApiUrl.text.toString(),
|
val frontendUrl = binding.instanceFrontendUrl.text.toString()
|
||||||
frontendUrl = binding.instanceFrontendUrl.text.toString()
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
|
||||||
customInstance.name != "" &&
|
|
||||||
customInstance.apiUrl != "" &&
|
|
||||||
customInstance.frontendUrl != ""
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
// check whether the URL is valid, otherwise catch
|
|
||||||
URL(customInstance.apiUrl).toURI()
|
|
||||||
URL(customInstance.frontendUrl).toURI()
|
|
||||||
|
|
||||||
|
if (instanceName.isNotEmpty() && apiUrl.isNotEmpty() && frontendUrl.isNotEmpty()) {
|
||||||
|
if (apiUrl.toHttpUrlOrNull() != null && frontendUrl.toHttpUrlOrNull() != null) {
|
||||||
query {
|
query {
|
||||||
Database.customInstanceDao().insertAll(customInstance)
|
Database.customInstanceDao()
|
||||||
|
.insertAll(CustomInstance(instanceName, apiUrl, frontendUrl))
|
||||||
}
|
}
|
||||||
|
|
||||||
ActivityCompat.recreate(requireActivity())
|
ActivityCompat.recreate(requireActivity())
|
||||||
dismiss()
|
dismiss()
|
||||||
} catch (e: Exception) {
|
} else {
|
||||||
// invalid URL
|
Toast.makeText(requireContext(), R.string.invalid_url, Toast.LENGTH_SHORT)
|
||||||
Toast.makeText(
|
.show()
|
||||||
context,
|
|
||||||
getString(R.string.invalid_url),
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// at least one empty input
|
// at least one empty input
|
||||||
Toast.makeText(
|
Toast.makeText(requireContext(), R.string.empty_instance, Toast.LENGTH_SHORT).show()
|
||||||
context,
|
|
||||||
context?.getString(R.string.empty_instance),
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user