PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/MyKotlinApplication/app/src/main/java/com/example/aakarshrestha/mykotlinapplication/UI/ForgotPasswordActivity.kt

https://bitbucket.org/aakarshrestha007/nepaligeet
Kotlin | 128 lines | 86 code | 36 blank | 6 comment | 14 complexity | 1616995e9265dc21995cc52bf0757f3a MD5 | raw file
  1. package com.example.aakarshrestha.mykotlinapplication.UI
  2. import android.databinding.DataBindingUtil
  3. import android.graphics.Typeface
  4. import android.support.v7.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.support.v4.content.ContextCompat
  7. import android.text.Editable
  8. import android.text.TextWatcher
  9. import android.util.Log
  10. import android.view.View
  11. import com.example.aakarshrestha.mykotlinapplication.R
  12. import com.example.aakarshrestha.mykotlinapplication.UI.NetworkApi.NetworkApi
  13. import com.example.aakarshrestha.mykotlinapplication.UI.urls.NetworkApiUrl
  14. import com.example.aakarshrestha.mykotlinapplication.databinding.ActivityForgotPasswordBinding
  15. import okhttp3.*
  16. import org.json.JSONException
  17. import org.json.JSONObject
  18. import java.io.IOException
  19. import java.util.regex.Pattern
  20. class ForgotPasswordActivity : AppCompatActivity() {
  21. lateinit var binding: ActivityForgotPasswordBinding
  22. val VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE)
  23. override fun onCreate(savedInstanceState: Bundle?) {
  24. super.onCreate(savedInstanceState)
  25. binding = DataBindingUtil.setContentView(this, R.layout.activity_forgot_password)
  26. val typeFace: Typeface = Typeface.createFromAsset(assets, "fonts/lobstertwobold.ttf")
  27. binding.appname.typeface = typeFace
  28. binding.nameOfApp.typeface = typeFace
  29. binding.appbarlayout.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
  30. if (Math.abs(verticalOffset) == appBarLayout!!.totalScrollRange) {
  31. // Collapsed
  32. binding.nameOfApp.visibility = View.VISIBLE
  33. } else if (verticalOffset == 0) {
  34. // Expanded
  35. binding.nameOfApp.visibility = View.INVISIBLE
  36. }
  37. }
  38. binding.forgotEmail.addTextChangedListener(object : TextWatcher {
  39. override fun afterTextChanged(s: Editable?) {
  40. }
  41. override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
  42. }
  43. override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
  44. if (s?.length!! > 0 && s.toString().trim().isNotEmpty() && s.toString().trim() != "") {
  45. if (validate(s.toString())) {
  46. binding.send.background = ContextCompat.getDrawable(binding.root.context, R.drawable.rounded_corner)
  47. binding.send.setTextColor(ContextCompat.getColor(binding.root.context, android.R.color.holo_purple))
  48. binding.send.isEnabled = true
  49. } else {
  50. binding.send.background = ContextCompat.getDrawable(binding.root.context, R.drawable.rounded_corner_inactive)
  51. binding.send.setTextColor(ContextCompat.getColor(binding.root.context, R.color.silver))
  52. binding.send.isEnabled = false
  53. }
  54. } else {
  55. binding.send.background = ContextCompat.getDrawable(binding.root.context, R.drawable.rounded_corner_inactive)
  56. binding.send.setTextColor(ContextCompat.getColor(binding.root.context, R.color.silver))
  57. binding.send.isEnabled = false
  58. }
  59. }
  60. })
  61. binding.send.setOnClickListener({
  62. // val value = NetworkApi.postForgotPasswordData(binding.emailAddress.text.toString().trim())
  63. // Log.d("studyThisNow", value + " haha")
  64. val email = binding.forgotEmail.text.toString().trim()
  65. postForgotPasswordData(email)
  66. // binding.forgotPasswordLayout.visibility = View.GONE
  67. // binding.resetPasswordLayout.visibility = View.VISIBLE
  68. })
  69. }
  70. fun validate(emailStr: String): Boolean {
  71. val matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr)
  72. return matcher.find()
  73. }
  74. fun postForgotPasswordData(email: String) {
  75. try {
  76. val call: Call = NetworkApi.getDataOnCall(NetworkApiUrl.forgotPasswordUrl()+email)
  77. call.enqueue(object : Callback {
  78. override fun onFailure(call: Call?, e: IOException?) {
  79. }
  80. override fun onResponse(call: Call?, response: Response?) {
  81. if (response?.isSuccessful!!) {
  82. Log.d("studyThisNow", response.body()?.string()!! + " haha")
  83. } else {
  84. Log.d("studyThisNow", response.body()?.string()!! + " NO haha")
  85. }
  86. }
  87. })
  88. } catch (ioe: IOException) {
  89. Log.e("ioe", ioe.message)
  90. }
  91. }
  92. }