When i click the button to save user's informations, the app just close and i can't access the database to see if the infos got saved or not because of that. When i comment the "verifyUser(mail)" part, i only get the toast as an answer but the app close too. I really don't know what the bug is.
here is my code
package be.heh.projet_inventaire
import android.content.AsyncQueryHandler
import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Patterns
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import be.heh.projet_inventaire.bd.MyDB
import be.heh.projet_inventaire.bd.User
import be.heh.projet_inventaire.bd.UserRecord
import kotlinx.android.synthetic.main.activity_main.bt_Inscription_Connexion
import kotlinx.android.synthetic.main.activity_main.bt_Inscription_main
import kotlinx.android.synthetic.main.activity_main.et_email
import kotlinx.android.synthetic.main.activity_main.et_pwd
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AsyncTask.execute {MyDB.getDatabase(this).userDao()}
val viewModel: UserViewModel by viewModels()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
et_email.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(s: Editable?) {
mailValidator(s.toString())
}
})
et_pwd.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(s: Editable?) {
PwdValidator(s.toString())
}
})
}
fun xmlClickEvent(v: View) {
when(v.id){
bt_Inscription_main.id -> addUser()
bt_Inscription_Connexion.id -> versConnexion()
}
}
private fun validMail(email: String): Boolean {
val emailPattern = Patterns.EMAIL_ADDRESS
return emailPattern.matcher(email).matches()
}
private fun mailValidator(email: String): Boolean {
if (validMail(email)) {
tvEmailError.visibility = View.GONE
return true
} else {
tvEmailError.text = "Adresse e-mail non valide"
tvEmailError.visibility = View.VISIBLE
return false
}
}
private fun PwdValidator(password: String): Boolean {
if (password.length >= 4) {
tvPwdError.visibility = View.GONE
return true
} else {
tvPwdError.text = "Le mot de passe doit contenir au moins 4 caractères"
tvPwdError.visibility = View.VISIBLE
return false
}
}
fun verifyUser(email: String) : Boolean{
var user = UserRecord (0,"","","")
AsyncTask.execute { user = MyDB.getDatabase(this).userDao().get(email) }
if (user == null){return true}
else(return false)
}
fun addUser(){
try {
val mail = et_email.text.toString()
val pwd = et_pwd.text.toString()
if(mailValidator(mail) && PwdValidator(pwd)){
if(verifyUser(mail)) {
val u = User(
0, mail,
pwd
)
var role = ""
AsyncTask.execute{
val db = MyDB.getDatabase(this)
val dao = db.userDao()
val liste = dao.get()
if (liste.isEmpty()) {
role = "S_U"
} else {
role = "basic"
}
val u1 = UserRecord(0, u.email, u.pwd, role)
dao.insertUser(u1)
}
Toast.makeText(this, "user créé", Toast.LENGTH_LONG).show();
}
else{
tvEmailError.text = "Cet utilisateur existe déjà"
tvEmailError.visibility = View.VISIBLE
}
}
else{
tvEmailError.text = "Entrez une adresse mail valide"
tvEmailError.visibility = View.VISIBLE
tvPwdError.text = "Le mot de passe doit contenir au moins 4 caractères"
tvPwdError.visibility = View.VISIBLE
}
}
catch (e:Exception){
e.printStackTrace()
}
}
fun versConnexion(){
val connexion = Intent(this,ConnexionActivity::class.java)
startActivity(connexion)
}
}
What I have tried:
I tried many things but i don't even know why it's not working so it was a bit random