-
Notifications
You must be signed in to change notification settings - Fork 4
Authentication edit #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ package com.myapp.travelize.authentication | |
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import android.text.Editable | ||
| import android.text.TextUtils | ||
| import android.text.TextWatcher | ||
| import android.util.Log | ||
| import android.util.Patterns | ||
| import androidx.fragment.app.Fragment | ||
|
|
@@ -27,6 +29,7 @@ class LoginFragment : Fragment() { | |
| lateinit var passwordEditText: TextInputEditText | ||
| lateinit var emailEditText: TextInputEditText | ||
| lateinit var resetTextView: TextView | ||
| lateinit var signinBtn: Button | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| auth = FirebaseAuth.getInstance() | ||
|
|
@@ -37,36 +40,52 @@ class LoginFragment : Fragment() { | |
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| // Inflate the layout for this fragment | ||
| val view = inflater.inflate(R.layout.fragment_login, container, false) | ||
| return inflater.inflate(R.layout.fragment_login, container, false) | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| val signupInsteadBtn = view.findViewById<TextView>(R.id.signupTextView) | ||
| val signinBtn = view.findViewById<Button>(R.id.signinBtn) | ||
| signinBtn = view.findViewById<Button>(R.id.signinBtn) | ||
| emailEditText = view.findViewById(R.id.emailEditText2) | ||
| passwordEditText = view.findViewById(R.id.passwordEditText2) | ||
| resetTextView = view.findViewById(R.id.forgotPasswordTextView) | ||
| Log.e("Checking something:", activity.toString()) | ||
|
|
||
| //disablesignin button | ||
| signinBtn.isEnabled = false | ||
|
|
||
| signupInsteadBtn.setOnClickListener { | ||
| (activity as? MainActivity)?.signupInstead() | ||
| } | ||
|
|
||
| //set text change listener | ||
| emailEditText.addTextChangedListener( textWatcher) | ||
| passwordEditText.addTextChangedListener( textWatcher) | ||
|
|
||
| signinBtn.setOnClickListener { | ||
| // Log.e("Checking Register form", name.text.toString()) | ||
| // Log.e("Checking Register form", email.text.toString()) | ||
| // Log.e("Checking Register form", password.text.toString()) | ||
| // Log.e("Checking Register form", confirmPassword.text.toString()) | ||
|
|
||
| val email = emailEditText.text.toString().trim() | ||
| val password = passwordEditText.text.toString().trim() | ||
| val email =getText( emailEditText) | ||
| val password = getText(passwordEditText) | ||
|
|
||
| validate(email, password) | ||
| if( validate(email, password) ){ | ||
| signin(email, password) | ||
| } | ||
| } | ||
| resetTextView.setOnClickListener { | ||
| val email = emailEditText.text.toString().trim() | ||
| val email = getText( emailEditText) | ||
| invokeAlertDialog(email) | ||
| } | ||
| return view | ||
| } | ||
|
|
||
| // get text from edit text | ||
| fun getText( editText : TextInputEditText ) : String = | ||
| editText.text.toString().trim() | ||
|
|
||
| private fun invokeAlertDialog(email: String) { | ||
| val dialog = MaterialAlertDialogBuilder(requireActivity()) | ||
| .setTitle("Reset Password") | ||
|
|
@@ -93,16 +112,16 @@ class LoginFragment : Fragment() { | |
|
|
||
| } | ||
|
|
||
| private fun validate(email: String, password: String) { | ||
| private fun validate(email: String, password: String) : Boolean { | ||
| if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { | ||
| Toast.makeText(activity, "Some Field is Empty!", Toast.LENGTH_SHORT).show() | ||
| return | ||
| return false | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you have disabled the sign in button on an empty email/password, this if statement is effectively redundant so can you remove it? |
||
| } | ||
| if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) { | ||
| emailEditText.error = "Invalid email address!" | ||
| return | ||
| return false | ||
| } | ||
| signin(email, password) | ||
| return true | ||
| } | ||
|
|
||
| private fun signin(email: String, password: String) { | ||
|
|
@@ -137,5 +156,22 @@ class LoginFragment : Fragment() { | |
| } | ||
| } | ||
|
|
||
| //textwatcher | ||
| private val textWatcher = object : TextWatcher { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use intuitive and specific names like signInTextWatcher for the sake of code maintenance? |
||
| override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | ||
|
|
||
| override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | ||
| // | ||
| val email = getText(emailEditText) | ||
| val password = getText(passwordEditText) | ||
|
|
||
| // eabled when all texts are filled | ||
| signinBtn.isEnabled = | ||
| ( !TextUtils.isEmpty(email) && !TextUtils.isEmpty( password ) ) | ||
| } | ||
|
|
||
| override fun afterTextChanged(s: Editable?) { } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,17 @@ package com.myapp.travelize.authentication | |
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import android.text.Editable | ||
| import android.text.TextUtils | ||
| import android.text.TextWatcher | ||
| import android.util.Log | ||
| import android.util.Patterns | ||
| import androidx.fragment.app.Fragment | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.Button | ||
| import android.widget.EditText | ||
| import android.widget.Toast | ||
| import android.widget.Toolbar | ||
| import com.google.android.material.textfield.TextInputEditText | ||
|
|
@@ -23,6 +26,7 @@ class RegisterFragment : Fragment() { | |
| lateinit var nameEditText: TextInputEditText | ||
| lateinit var passwordEditText: TextInputEditText | ||
| lateinit var confirmPasswordEditText: TextInputEditText | ||
| private lateinit var signupBtn: Button | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
@@ -34,57 +38,100 @@ class RegisterFragment : Fragment() { | |
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| // Inflate the layout for this fragment | ||
| val view = inflater.inflate(R.layout.fragment_register, container, false) | ||
| return inflater.inflate(R.layout.fragment_register, container, false) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as before here, so can you make the requested changes? |
||
|
|
||
| }//onCreateView ends | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
|
|
||
| val toolbar = view.findViewById<Toolbar>(R.id.backToolBar) | ||
| val signupBtn = view.findViewById<Button>(R.id.signupBtn) | ||
| signupBtn = view.findViewById<Button>(R.id.signupBtn) | ||
| auth = FirebaseAuth.getInstance() | ||
| nameEditText = view.findViewById(R.id.nameEditText) | ||
| emailEditText = view.findViewById(R.id.emailEditText) | ||
| passwordEditText = view.findViewById(R.id.passwordEditText) | ||
| confirmPasswordEditText = view.findViewById(R.id.confirmEditText) | ||
|
|
||
| //disable button | ||
| signupBtn.isEnabled = false | ||
|
|
||
| activity?.setActionBar(toolbar) | ||
| activity?.title = "" | ||
| activity?.actionBar?.setDisplayHomeAsUpEnabled(true) | ||
| toolbar.setNavigationOnClickListener { | ||
| activity?.supportFragmentManager?.popBackStack() | ||
| } | ||
|
|
||
| //set text change listener | ||
| nameEditText.addTextChangedListener( textWatcher) | ||
| emailEditText.addTextChangedListener( textWatcher) | ||
| passwordEditText.addTextChangedListener( textWatcher) | ||
| confirmPasswordEditText.addTextChangedListener( textWatcher) | ||
|
|
||
| signupBtn.setOnClickListener { | ||
| // Log.e("Checking Register form", name.text.toString()) | ||
| // Log.e("Checking Register form", email.text.toString()) | ||
| // Log.e("Checking Register form", password.text.toString()) | ||
| // Log.e("Checking Register form", confirmPassword.text.toString()) | ||
| val name = nameEditText.text.toString().trim() | ||
| val email = emailEditText.text.toString().trim() | ||
| val password = passwordEditText.text.toString().trim() | ||
| val confirmPassword = confirmPasswordEditText.text.toString().trim() | ||
| val name = getText( nameEditText ) | ||
| val email = getText(emailEditText) | ||
| val password = getText( passwordEditText ) | ||
| val confirmPassword = getText( confirmPasswordEditText) | ||
|
|
||
| validate(name, email, password, confirmPassword) | ||
| if( validate(name, email, password, confirmPassword) ){ | ||
| signup(email, password, name) | ||
| } | ||
| } | ||
| return view | ||
| }//onCreateView ends | ||
| } | ||
|
|
||
| private fun validate(name: String, email: String, password: String, confirmPassword: String) { | ||
| // get text from edit text | ||
| fun getText( editText : TextInputEditText ) : String = | ||
| editText.text.toString().trim() | ||
|
|
||
| // make function independent | ||
| private fun validate(name: String, email: String, password: String, confirmPassword: String) : Boolean { | ||
| if (TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as before, this is redundant now and must be removed |
||
| confirmPassword | ||
| ) | ||
| ) { | ||
| Toast.makeText(activity, "Some Field is Empty!", Toast.LENGTH_SHORT).show() | ||
| return | ||
| return false | ||
| } | ||
| if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) { | ||
| emailEditText.error = "Invalid email address!" | ||
| return | ||
| return false | ||
| } | ||
| if (password.length < 6) { | ||
| passwordEditText.error = "Password too short!" | ||
| return | ||
| return false | ||
| } | ||
| if (!password.equals(confirmPassword)) { | ||
| confirmPasswordEditText.error = "Password doesn't match!" | ||
| return | ||
| return false | ||
| } | ||
| signup(email, password,name) | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| //textwatcher | ||
| val textWatcher : TextWatcher = object : TextWatcher{ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as before here too,use a specific name |
||
| override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | ||
|
|
||
| override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | ||
| // | ||
| val name = getText(nameEditText) | ||
| val email = getText(emailEditText) | ||
| val password = getText(passwordEditText) | ||
| val confirmPassword = getText(confirmPasswordEditText) | ||
|
|
||
| // eabled when all texts are filled | ||
| signupBtn.isEnabled = | ||
| ( !TextUtils.isEmpty(name) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty( password) && !TextUtils.isEmpty(confirmPassword) ) | ||
| } | ||
|
|
||
| override fun afterTextChanged(s: Editable?) { } | ||
|
|
||
| } | ||
|
|
||
| private fun signup(email: String, password: String, name: String) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although this gets rid of the boilerplate and redundancy, I prefer
val view = inflater.inflate(R.layout.fragment_login, container, false), just incase one needs to instantiate some view from the layout. So can you change it back to the way it was before?