Skip to content

Commit

Permalink
added basic validation on client form
Browse files Browse the repository at this point in the history
  • Loading branch information
itszechs committed Dec 25, 2024
1 parent 5b32c61 commit 1a30c41
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.google.android.material.textfield.TextInputLayout
import com.google.android.material.textview.MaterialTextView
import zechs.drive.stream.R
import zechs.drive.stream.data.model.Client
import zechs.drive.stream.utils.util.GoogleClientValidator
import zechs.drive.stream.utils.util.UrlValidator

class DialogAddClient(
context: Context,
Expand All @@ -23,8 +25,11 @@ class DialogAddClient(
) : Dialog(context, R.style.ThemeOverlay_Fade_MaterialAlertDialog) {

private lateinit var title: MaterialTextView
private lateinit var tfClientId: TextInputLayout
private lateinit var etClientId: EditText
private lateinit var tfClientSecret: TextInputLayout
private lateinit var etClientSecret: EditText
private lateinit var tfRedirectUri: TextInputLayout
private lateinit var etRedirectUri: EditText
private lateinit var btnClientInfo: AppCompatImageButton
private lateinit var submitButton: MaterialButton
Expand All @@ -35,14 +40,20 @@ class DialogAddClient(
setContentView(R.layout.dialog_client)

title = findViewById(R.id.tv_title)
etClientId = findViewById<TextInputLayout>(R.id.tf_client_id).editText!!
etClientSecret = findViewById<TextInputLayout>(R.id.tf_client_secret).editText!!
etRedirectUri = findViewById<TextInputLayout>(R.id.tf_redirect_uri).editText!!
tfClientId = findViewById(R.id.tf_client_id)
etClientId = tfClientId.editText!!
tfClientSecret = findViewById(R.id.tf_client_secret)
etClientSecret = tfClientSecret.editText!!
tfRedirectUri = findViewById(R.id.tf_redirect_uri)
etRedirectUri = tfRedirectUri.editText!!
btnClientInfo = findViewById(R.id.btnClientInfo)
val chipScope = findViewById<Chip>(R.id.chipScope)
submitButton = findViewById(R.id.btn_submit)

submitButton.setOnClickListener {
if (!areInputsValid()) {
return@setOnClickListener
}
onSubmitClickListener.invoke(
Client(
id = etClientId.text.trim().toString(),
Expand All @@ -65,6 +76,48 @@ class DialogAddClient(
setupDialog()
}

private fun areInputsValid(): Boolean {
val clientId = etClientId.text.trim().toString()
val clientSecret = etClientSecret.text.trim().toString()
val redirectUri = etRedirectUri.text.trim().toString()

var isValid = true

// Validate Client ID
if (client == null) {
if (!GoogleClientValidator.isValidClientId(clientId)) {
tfClientId.isErrorEnabled = true
tfClientId.error = "Invalid Client ID. Use the one provided by Google."
isValid = false
} else {
tfClientId.isErrorEnabled = false
tfClientId.error = null
}
}

// Validate Client Secret
if (clientSecret.isBlank()) {
tfClientSecret.isErrorEnabled = true
tfClientSecret.error = "Client Secret is required."
isValid = false
} else {
tfClientSecret.isErrorEnabled = false
tfClientSecret.error = null
}

// Validate Redirect URI
if (!UrlValidator.startsWithHttpOrHttps(redirectUri)) {
tfRedirectUri.isErrorEnabled = true
tfRedirectUri.error = "Invalid URI. It must start with 'http' or 'https'."
isValid = false
} else {
tfRedirectUri.isErrorEnabled = false
tfRedirectUri.error = null
}

return isValid
}

private fun setupDialog() {
if (client != null) {
title.text = context.getString(R.string.edit_client)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package zechs.drive.stream.utils.util

object GoogleClientValidator {

fun isValidClientId(clientId: String?): Boolean {
if (clientId.isNullOrBlank()) return false
return clientId.endsWith(".apps.googleusercontent.com")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zechs.drive.stream.utils.util

object UrlValidator {

fun startsWithHttpOrHttps(url: String): Boolean {
return url.trim().matches(Regex("^(http|https)://.*$"))
}

}

0 comments on commit 1a30c41

Please sign in to comment.