From 218cfc6bb46d92b6bf42de4bb673c9783ceb2e55 Mon Sep 17 00:00:00 2001 From: 1HazArd1 Date: Sat, 26 Feb 2022 18:16:28 +0530 Subject: [PATCH] Add fix for empty pass/email crash bug --- .idea/misc.xml | 1 + .../java/com/hazard/samarpan/LoginFragment.kt | 29 ++++++++---- .../java/com/hazard/samarpan/MainActivity.kt | 1 + .../hazard/samarpan/UserRegisterFragment.kt | 45 ++++++++++++------- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 2aec464..ff6cc32 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -15,6 +15,7 @@ + diff --git a/app/src/main/java/com/hazard/samarpan/LoginFragment.kt b/app/src/main/java/com/hazard/samarpan/LoginFragment.kt index e1a9af2..6abd534 100644 --- a/app/src/main/java/com/hazard/samarpan/LoginFragment.kt +++ b/app/src/main/java/com/hazard/samarpan/LoginFragment.kt @@ -60,15 +60,28 @@ class LoginFragment: Fragment() { password?.error = "Password cannot be empty" } else{ - registerAuth.signInWithEmailAndPassword(mail,pass).addOnCompleteListener{ task -> + try { + registerAuth.signInWithEmailAndPassword(mail, pass) + .addOnCompleteListener { task -> - if(task.isSuccessful){ - // logic for navigating to the dashboard - Toast.makeText(activity,"Logged in Successfully", Toast.LENGTH_SHORT).show() - } - else{ - Toast.makeText(activity,task.exception!!.message.toString(), Toast.LENGTH_SHORT).show() - } + if (task.isSuccessful) { + // logic for navigating to the dashboard + Toast.makeText( + activity, + "Logged in Successfully", + Toast.LENGTH_SHORT + ).show() + } else { + Toast.makeText( + activity, + task.exception!!.message.toString(), + Toast.LENGTH_SHORT + ).show() + } + } + } + catch (e:IllegalArgumentException){ + Toast.makeText(context,"Fields cannot be empty",Toast.LENGTH_SHORT) } } } diff --git a/app/src/main/java/com/hazard/samarpan/MainActivity.kt b/app/src/main/java/com/hazard/samarpan/MainActivity.kt index 94478fa..bd1a0ad 100644 --- a/app/src/main/java/com/hazard/samarpan/MainActivity.kt +++ b/app/src/main/java/com/hazard/samarpan/MainActivity.kt @@ -11,6 +11,7 @@ class MainActivity : AppCompatActivity() { setTheme(R.style.Theme_Samarpan) setContentView(R.layout.activity_main) addFragment() + } private fun addFragment() { diff --git a/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt b/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt index c8a8f57..b04f28e 100644 --- a/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt +++ b/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt @@ -9,6 +9,7 @@ import android.widget.Button import android.widget.LinearLayout import android.widget.Toast import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager import com.google.android.gms.tasks.OnCompleteListener import com.google.android.material.textfield.TextInputEditText import com.google.firebase.auth.AuthResult @@ -48,6 +49,7 @@ class UserRegisterFragment: Fragment(){ btnSignIn?.setOnClickListener { val userLogin=LoginFragment() activity?.supportFragmentManager?.beginTransaction()?.replace(R.id.fragment_container1,userLogin)?.commit() + } btnRegister?.setOnClickListener{ val mail=email?.text.toString().trim() @@ -64,28 +66,39 @@ class UserRegisterFragment: Fragment(){ if(!(isValidPassword(pass))){ password?.error="Password format is invalid" } - else{ + else { //user registration - registerAuth.createUserWithEmailAndPassword(mail,pass).addOnCompleteListener( - OnCompleteListener { task -> + try { + registerAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener( + OnCompleteListener { task -> - //if registration is done successfully - if(task.isSuccessful){ - Toast.makeText(activity,"You were registered successfully.", Toast.LENGTH_SHORT).show() - /* add the intent to go to the main dashboard of the application and also add finish() so that + //if registration is done successfully + if (task.isSuccessful) { + Toast.makeText( + activity, + "You were registered successfully.", + Toast.LENGTH_SHORT + ).show() + /* add the intent to go to the main dashboard of the application and also add finish() so that if the user presses back after registration they don't go back to the registration page it basically clears all the previous activities */ - } - // if the registration failed somehow - else{ - Toast.makeText(activity, - task.exception!!.message.toString(), - Toast.LENGTH_SHORT).show() - } + } + // if the registration failed somehow + else { + Toast.makeText( + activity, + task.exception!!.message.toString(), + Toast.LENGTH_SHORT + ).show() + } - } - ) + } + ) + } + catch(e:IllegalArgumentException){ + Toast.makeText(context,"Fields cannot be empty",Toast.LENGTH_SHORT) + } } } return itemView