mercredi 21 mars 2018

If statment not executing inside DialogInterface.OnClickListener

If statment inside

setPositiveButton("REGISTER" ,
DialogInterface.OnClickListener { dialogInterface, i ->

is not executing although i've checked correctness of conditions

,, and tried adding true explicitly .

I've also tried clean , Rebuild but no change happend .

I'm new to kotlin and wonder if the reason is something from my code or
Android studio , the apk .

         package com.example.agh.uberclone

        import android.content.Context
        import android.content.DialogInterface
        import android.support.v7.app.AppCompatActivity
        import android.os.Bundle
        import android.support.design.widget.Snackbar
        import android.support.v7.app.AlertDialog
        import android.util.Log
        import android.view.LayoutInflater
        import android.view.View
        import android.widget.Button
        import com.google.firebase.auth.FirebaseAuth
        import com.google.firebase.database.DatabaseReference
        import com.google.firebase.database.FirebaseDatabase
        import com.google.firebase.firestore.FirebaseFirestore
        import kotlinx.android.synthetic.main.activity_main.*
        import kotlinx.android.synthetic.main.layout_login.*
        import kotlinx.android.synthetic.main.layout_login.view.*
        import kotlinx.android.synthetic.main.layout_register.*
        import kotlinx.android.synthetic.main.layout_register.view.*
        import uk.co.chrisjenx.calligraphy.CalligraphyConfig
        import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper

        class MainActivity : AppCompatActivity() {

            var auth: FirebaseAuth?=null



            var db : FirebaseFirestore? = null

            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)

                CalligraphyConfig.initDefault(CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/Arkhip_font.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
                )

                setContentView(R.layout.activity_main)



                // Init Firebase
                auth = FirebaseAuth.getInstance() ;
               db =  FirebaseFirestore.getInstance()


                fun showRegisterDialog() {

                    val dialog = AlertDialog.Builder(this)
                    dialog.setTitle("Register")
                    dialog.setMessage("Use Email to Register")

                    val inflater = LayoutInflater.from(this)
                    val  register_Layout = inflater.inflate(R.layout.layout_register ,null ,true)

                    dialog.setView(register_Layout)



                    dialog.setPositiveButton("REGISTER" ,
                            DialogInterface.OnClickListener { dialogInterface, i ->

                                var returno:Boolean =false
        Log.e( "fds: ",register_Layout.etEmail.text.toString().isBlank().toString() );
                                if (/*register_Layout.etEmail.text.toString().isBlank()*/   true
                                        or    ! register_Layout.etEmail.text.toString().contains("@"))
                                        register_Layout.etEmail.error = "Valid Email is Required"
                                         returno = true
                                          Log.e( "gother: ", "");

                                if (register_Layout.etpass.text.toString().isBlank() )
                                { register_Layout.etpass.setError("Password is Required ")
                                    returno = true
                                    Log.e( "gother: ", "");}
                                if (register_Layout.etpass.text.toString().length<6 )
                                {register_Layout.etpass.setError("Password too short ")
                                    returno = true
                                    Log.e( "gother: ", "");}

                                if (register_Layout.etPhone.text.toString().isBlank() )
                                {  register_Layout.etPhone.setError("Phone is Required ")
                                    returno = true
                                    Log.e( "gother: ", "");}

                                if (register_Layout.etName.text.toString().isBlank() )
                                {  register_Layout.etName.setError("Name is Required ")
                                    returno = true
                                    Log.e( "gother: ", "");}

                                if (! returno) {
                                    Log.e( "gother: ", "");

                                    auth!!.createUserWithEmailAndPassword(
                                            register_Layout.etEmail.text.toString() ,register_Layout.etpass.text.toString() )
                                            .addOnCompleteListener {
                                                task ->
                                                if(task.isSuccessful){

                                                    val user = User(register_Layout.etName.text.toString()
                                                            ,register_Layout.etEmail.text.toString(),
                                                            register_Layout.etpass.text.toString(),
                                                            register_Layout. etPhone.text.toString())

                                                    db!!.collection("users")
                                                            .add(user)
                                                            .addOnSuccessListener {
                                                                snack(rootRelative,text = "Signed up Successfully") }
                                                            .addOnFailureListener {
                                                                exception ->
                                                                snack(rootRelative,text = "Failed to Sign up")
                                                                Log.e( "dbExcep ", exception.message);
                                                            }

                                                }else if (!task.isSuccessful){

                                                    snack(rootRelative,text = "Failed to Sign up")
                                                    Log.e( "WithEmailAndPassword ", task.exception.toString());
                                                }


                                            }
                                }



                            })


                    dialog.setNegativeButton("Close" ,
                            DialogInterface.OnClickListener { dialogInterface, i ->

                                dialogInterface.dismiss()
                            })

                    dialog.show()
                }


                // Init View
                btn_signup.setOnClickListener {    showRegisterDialog ( )   }

                fun showLoginDialog() {

                    val dialog = AlertDialog.Builder(this)
                    dialog.setTitle("Login")
                    dialog.setMessage("Use Email to Login")

                    val inflater = LayoutInflater.from(this)
                    val  layout_login = inflater.inflate(R.layout.layout_login ,null ,true)

                    dialog.setView(layout_login)



                    dialog.setPositiveButton("LOGIN" ,
                            DialogInterface.OnClickListener { dialogInterface, i ->

                                var returno:Boolean =false
                                Log.e( ": ", layout_login.etEmailLogin.text.toString());
                                if (layout_login.etEmailLogin.text.toString().isBlank()
                                        ||   ! layout_login.etEmailLogin.text.toString().contains("@"))
                                {  layout_login.etEmailLogin.error = "Valid Email is Required"
                                    returno = true}

                                if (layout_login.etpassLogin.text.toString().isBlank() )
                                { layout_login.etpassLogin.setError("Password is Required ")
                                    returno = true}
                                if (layout_login.etpassLogin.text.toString().length<6 )
                                {layout_login.etpassLogin.setError("Password too short ")
                                    returno = true}



                                if (! returno) {

                                    auth!!.signInWithEmailAndPassword(layout_login.etEmailLogin.text.toString()
                                    ,layout_login.etpassLogin.text.toString())
                                            .addOnCompleteListener {
                                                task ->
                                                if(task.isSuccessful){

                                snack(rootRelative,text = "Signed up Successfully")

                                                            }

                                              else if (!task.isSuccessful){

                                                    snack(rootRelative,text = "Failed to Sign In")
                                                    Log.e( "WithEmailAndPassword ", task.exception.toString());
                                                }



                                }



                            }
                            })


                    dialog.setNegativeButton("Close" ,
                            DialogInterface.OnClickListener { dialogInterface, i ->
                                if (true or false)
                                        dialogInterface.dismiss()
                            })

                    dialog.show()
                }


                btn_signin.setOnClickListener {    showLoginDialog ( )   }



            }




            override fun attachBaseContext(newBase: Context?) {
                super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase))
            }

        }

Aucun commentaire:

Enregistrer un commentaire