Skip to content

UNT-T21169 - Bugfix Login Issue #2

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

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
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
43 changes: 35 additions & 8 deletions app/src/main/java/simform/gitexcercise/android/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import simform.gitexcercise.android.databinding.ActivityLoginBinding
import simform.gitexcercise.android.validation.Validator
import simform.gitexcercise.android.validation.Validator.ValidCredentials

class LoginActivity : AppCompatActivity() {

Expand All @@ -19,15 +21,40 @@ class LoginActivity : AppCompatActivity() {

private fun setupOnClicks() = with(binding) {
btnLogin.setOnClickListener {
Toast.makeText(
this@LoginActivity,
getString(R.string.msg_login_successfully),
Toast.LENGTH_SHORT
).show()
val intent = Intent(this@LoginActivity, ProfileActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
performLogin()
}
tvForgotPassword.setOnClickListener {
startActivity(Intent(this@LoginActivity, ForgotPasswordActivity::class.java))
}
}

private fun performLogin() = with(binding) {
val txtEmail = etEmail.text.trim().toString()
val txtPassword = etPassword.text.trim().toString()
when (Validator.isValidCredentials(email = txtEmail, password = txtPassword)) {
ValidCredentials.VALID -> {
Toast.makeText(
this@LoginActivity,
getString(R.string.msg_login_successfully),
Toast.LENGTH_SHORT
).show()
gotoProfile()
}

ValidCredentials.INVALID_EMAIL -> {
etEmail.error = getString(R.string.errr_valid_email)
}
startActivity(intent)

ValidCredentials.INVALID_PASSWORD -> {
etPassword.error = getString(R.string.err_password_8_chars)
}
}
}

private fun gotoProfile() {
val intent = Intent(this@LoginActivity, ProfileActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package simform.gitexcercise.android.validation

object Validator {

object Password {
const val MIN_LENGTH = 8
const val MAX_LENGTH = 16
}

enum class ValidCredentials {
VALID, INVALID_EMAIL, INVALID_PASSWORD
}

fun isValidCredentials(email: String, password: String): ValidCredentials {
if (!isValidEmail(email)) return ValidCredentials.INVALID_EMAIL
if (!isValidPassword(password)) return ValidCredentials.INVALID_PASSWORD
return ValidCredentials.VALID
}

fun isValidEmail(email: String) =
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()

fun isValidPassword(password: String): Boolean =
password.isNotBlank() && password.length in Password.MIN_LENGTH..Password.MAX_LENGTH
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
<string name="msg_upcoming_feature">To be Implemented Soon!!</string>
<string name="action_edit">Edit</string>
<string name="data_priyashu_email">[email protected]</string>
<string name="err_password_8_chars">Password must be of at least 8 Characters</string>
<string name="errr_valid_email">Enter valid Email</string>
</resources>