package com.example.heartbeatapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import com.google.firebase.auth.ktx.auth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.ktx.Firebase
class RedactProfilePage : AppCompatActivity() {
private lateinit var list: ListView
private lateinit var adapter :ArrayAdapter<String>
private lateinit var listData: List<String>
private val auth = Firebase.auth
private val currentUser = auth.currentUser
private val USER_KEY = "Users"
private var databaseReference =
currentUser?.let { FirebaseDatabase.getInstance().getReference(USER_KEY).child(it.uid) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.redact_profile_page)
init()
getNameDataFromDb()
}
private fun init(){
list = findViewById<ListView>(R.id.list)
listData = ArrayList()
adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1, listData)
list.adapter
databaseReference = currentUser?.let { FirebaseDatabase.getInstance().getReference(USER_KEY).child(it.uid) }
}
private fun getNameDataFromDb() {
val listener = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (listData.size > 0){
(listData as ArrayList<String>).clear()
}
for (sn: DataSnapshot in snapshot.children) run {
var name: User? = sn.getValue(User::class.java)
name?.let { (listData as ArrayList<String>).add(it.name) }
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
}
databaseReference?.addValueEventListener(listener)
}
}