Home > database >  How can I set the fetched data of my app from firebase to my bottomSheetView? (Kotlin)
How can I set the fetched data of my app from firebase to my bottomSheetView? (Kotlin)

Time:09-12

its my first time in making question here in stackoverflow and I hope you understand how I structure this question like a newbie. So, I was working in a capstone project especifically a tracking device and I've been making progress just kinda stuck here in this part. So what I want to happen is that I want the data fetched from the firebase database to display in my bottomsheetview. This part is from the log-in page.

private lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login_page)

    auth= FirebaseAuth.getInstance()
    register.setOnClickListener {
        var intent = Intent(this,Register_page::class.java)
        startActivity(intent)
        finish()
    }

    loginpage()
}

fun loginpage() {
    val sharedPref = this.getSharedPreferences("logIn", Context.MODE_PRIVATE)?:return
    val editor = sharedPref.edit()
    editor.putBoolean("Finished", true)
    editor.apply()


    login.setOnClickListener {
        if(checking()){
            val email=email.text.toString()
            val password= password.text.toString()
            auth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        var intent = Intent(this,MapsActivity::class.java)
                        intent.putExtra("email",email)
                        startActivity(intent)
                        Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show()
                        finish()
                    } else {
                        Toast.makeText(this, "Wrong Details", Toast.LENGTH_LONG).show()
                    }
                }
        }
        else{
            Toast.makeText(this,"Enter the Details", Toast.LENGTH_LONG).show()
        }
    }
}


private fun checking():Boolean
{
    if(email.text.toString().trim{it<=' '}.isNotEmpty()
        && password.text.toString().trim{it<=' '}.isNotEmpty())
    {
        return true
    }
    return false
}

and this part is from my logged-in page where I want the data fetched in logging in to be set in the bottomsheetview.

   menu.setOnClickListener {


        val sharedPref = this.getSharedPreferences("inMap", Context.MODE_PRIVATE)
        val isLogin = sharedPref.getString("Email","1")
        val editor = sharedPref.edit()
        editor.putBoolean("Finished", true)
        editor.apply()
       var email = intent.getStringExtra("email")
       Toast.makeText(this, "${email}", Toast.LENGTH_LONG).show()



       if(isLogin=="1"){
            var email = intent.getStringExtra("email")
            if(email != null){
                setText(email)
                with(sharedPref.edit())
                {
                    putString("email",email)
                    apply()
                }
            }else{
                var intent = Intent(this,Login_page::class.java)
                startActivity(intent)
                finish()
            }
        }else{
            db= FirebaseFirestore.getInstance()
            var email=intent.getStringExtra("email")
            if (email != null) {
                Toast.makeText(this, "Woi, waya", Toast.LENGTH_LONG).show()

                db.collection("USERS").document(email).get()
                    .addOnSuccessListener {
                            tasks->
                        name.text=tasks.get("Name").toString()
                        phone.text=tasks.get("Phone").toString()
                        emailLog.text=tasks.get("email").toString()

                    }
            }
        }

        val bottomSheetDialog = BottomSheetDialog(
            this, R.style.BottomSheetDialogTheme
        )

        val bottomSheetView = LayoutInflater.from(applicationContext).inflate(
            R.layout.fragment_bottom_sheet,
            findViewById<LinearLayout>(R.id.bottomSheet)
        )


        bottomSheetView.findViewById<View>(R.id.logout).setOnClickListener{
            val sharedPref = this.getSharedPreferences("inMap", Context.MODE_PRIVATE)
            sharedPref.edit().remove("Email").apply()
            var intent = Intent(this,Login_page::class.java)
            sharedPref.getBoolean("Finished", false)
            startActivity(intent)
            finish()

        }

        bottomSheetDialog.setContentView(bottomSheetView)
        bottomSheetDialog.show()

    }

and here is the xml file where I'm trying to make reference place the data fetched

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/bottomSheet"
    android:layout_width="match_parent"
    android:background="@drawable/bottom_sheet_background"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SIIT - SBMARS"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">

        <com.makeramen.roundedimageview.RoundedImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:scaleType="centerCrop"
            android:src="@drawable/siitlogo"
            app:riv_corner_radius="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@ id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Rodgin P. Misterio"
                android:textColor="@color/black"
                android:textSize="17dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@ id/phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="[email protected]"
                android:textColor="@color/black"
                android:textSize="15dp"/>

            <TextView
                android:id="@ id/emailLog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Digoy Residence"
                android:textColor="@color/black"
                android:textSize="17dp"
                android:textStyle="bold"/>

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="12dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="10dp"
            android:src="@drawable/logs"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:text="Sessions"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="12dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="10dp"
            android:src="@drawable/logs"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:text="Logs"/>

    </LinearLayout>

    <Button
        android:id="@ id/logout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Log-out"
        android:background="@drawable/custom_button"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:textStyle="bold"/>

</LinearLayout>

please help huhu

CodePudding user response:

As far as I understand your question, Simple solution to this would creating a function to open bottom sheet and passing the 3 required values as parameters from inside .addOnSuccessListener

Function declaration -

fun openBottomsheet(name: String, email: String, phone: String) {
    // open bottom sheet and pass these values to it
}

CodePudding user response:

It would be nice if you can use data-binding and create a view-model to handle the data presentation. Your data would automatically be updated onto your bottom sheet from your database.

  • Related