/app/src/main/java/org/fossasia/susi/ai/helper/CredentialHelper.kt

https://github.com/fossasia/susi_android · Kotlin · 122 lines · 65 code · 10 blank · 47 comment · 13 complexity · e0090d9a85072bcdb42fb63eac342d40 MD5 · raw file

  1. package org.fossasia.susi.ai.helper
  2. import android.content.Context
  3. import android.support.design.widget.TextInputLayout
  4. import android.text.TextUtils
  5. import android.util.Patterns
  6. import java.net.URL
  7. import java.util.regex.Pattern
  8. import org.fossasia.susi.ai.R
  9. import timber.log.Timber
  10. /**
  11. * <h1>Helper class to verify credentials of user during login and sign up.</h1>
  12. * Created by saurabh on 11/10/16.
  13. */
  14. object CredentialHelper {
  15. private val PASSWORD_PATTERN = Pattern.compile("^((?=.*\\d)(?=.*[A-Z])(?=.*[@#\$%])(?=.*\\W).{8,64})$")
  16. private val VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE)
  17. private val VALID_URL_REGEX = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
  18. /**
  19. * Is email valid boolean.
  20. * @param mail the email
  21. * *
  22. * @return the boolean
  23. */
  24. fun isEmailValid(mail: String): Boolean {
  25. Timber.d("isEmailValid: %s", mail)
  26. val email = mail.trim { it <= ' ' }
  27. return !TextUtils.isEmpty(email) && VALID_EMAIL_ADDRESS_REGEX.matcher(email).matches()
  28. }
  29. /**
  30. * Is password valid boolean.
  31. * @param password the password
  32. * *
  33. * @return the boolean
  34. */
  35. fun isPasswordValid(password: String): Boolean {
  36. return PASSWORD_PATTERN.matcher(password).matches()
  37. }
  38. /**
  39. * Clear fields.
  40. * @param layouts the layouts
  41. */
  42. fun clearFields(vararg layouts: TextInputLayout) {
  43. for (inputLayout in layouts) {
  44. if (inputLayout.editText != null) {
  45. inputLayout.editText?.text = null
  46. }
  47. inputLayout.error = null
  48. }
  49. }
  50. /**
  51. * Is url valid boolean.
  52. * @param url the url
  53. * *
  54. * @return the boolean
  55. */
  56. fun isURLValid(url: String): Boolean {
  57. return VALID_URL_REGEX.matcher(url).matches()
  58. }
  59. fun isURLValid(inputLayout: TextInputLayout, context: Context): Boolean {
  60. return if (Patterns.WEB_URL.matcher(inputLayout.editText?.text.toString()).matches()) {
  61. inputLayout.error = null
  62. true
  63. } else {
  64. inputLayout.error = context.getString(R.string.invalid_url)
  65. false
  66. }
  67. }
  68. /**
  69. * Gets valid url.
  70. * @param url the url
  71. * *
  72. * @return the valid url
  73. */
  74. fun getValidURL(url: String): String? {
  75. return try {
  76. return if (url.trim { it <= ' ' }.substring(0, 7) == "http://" || url.trim { it <= ' ' }.substring(0, 8) == "https://") {
  77. val susiURL = URL(url.trim { it <= ' ' })
  78. susiURL.protocol + "://" + susiURL.host
  79. } else {
  80. val susiURL = URL("http://" + url.trim { it <= ' ' })
  81. susiURL.protocol + "://" + susiURL.host
  82. }
  83. } catch (e: Exception) {
  84. Timber.e(e)
  85. null
  86. }
  87. }
  88. /**
  89. * Check if empty boolean.
  90. * @param inputLayout the input layout
  91. * *
  92. * @param context the context
  93. * *
  94. * @return the boolean
  95. */
  96. fun checkIfEmpty(inputLayout: TextInputLayout, context: Context): Boolean {
  97. return if (TextUtils.isEmpty(inputLayout.editText?.text.toString())) {
  98. inputLayout.error = context.getString(R.string.field_cannot_be_empty)
  99. true
  100. } else {
  101. inputLayout.error = null
  102. false
  103. }
  104. }
  105. }