Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Copy link
Copy Markdown
Owner

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?

}

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")
Expand All @@ -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
Copy link
Copy Markdown
Owner

@2tanayk 2tanayk Jun 15, 2021

Choose a reason for hiding this comment

The 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?
And really sorry for the delay was caught up with a lot of work :/

}
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) {
Expand Down Expand Up @@ -137,5 +156,22 @@ class LoginFragment : Fragment() {
}
}

//textwatcher
private val textWatcher = object : TextWatcher {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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{
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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) {
Expand Down