Actualizaciones en la pantalla Compra

This commit is contained in:
luis.reyes 2024-03-19 18:30:29 -06:00
parent add78b3a4a
commit 2d9e334a0f
3 changed files with 81 additions and 27 deletions

View File

@ -6,17 +6,34 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
class ChairAdapter (private val item : ArrayList<Asiento>): RecyclerView.Adapter<ChairAdapter.ViewHolder>() {
private lateinit var mListener : onItemClickListener
private val selectedItems = ArrayList<Int>()
private lateinit var database: FirebaseDatabase
//private lateinit var myRef: DatabaseReference
private val selectedItems = HashSet<Int>()
private val occupiedSeats = ArrayList<String>()
fun toggleSelection(position: Int) {
var asiento=item[position].noAsiento.toString()
database = FirebaseDatabase.getInstance()
var myRef = database.getReference("Asientos/$asiento/status")
val asientoprueba = item[position]
val asientoNo = asientoprueba.noAsiento.toString()
if (selectedItems.contains(position)) {
actulizarValor("Libre", myRef)
selectedItems.remove(position)
occupiedSeats.remove(asientoNo)
} else {
actulizarValor("Ocupado", myRef)
selectedItems.add(position)
occupiedSeats.add(asientoNo)
}
notifyItemChanged(position)
}
@ -29,16 +46,28 @@ class ChairAdapter (private val item : ArrayList<Asiento>): RecyclerView.Adapter
mListener=listener
}
class ViewHolder(itemView: View, listener: onItemClickListener): RecyclerView.ViewHolder(itemView){
inner class ViewHolder(itemView: View, listener: onItemClickListener): RecyclerView.ViewHolder(itemView){
val chair : TextView = itemView.findViewById(R.id.chair)
init {
itemView.setOnClickListener{
val position=adapterPosition
if(position!=RecyclerView.NO_POSITION){
(itemView.context as? ChairAdapter) ?.toggleSelection((position))
toggleSelection(position)
notifyDataSetChanged()
}
}
}
fun bind(position: Int){
val item = item[position]
chair.text= item.noAsiento.toString()
if (selectedItems.contains(position)) {
itemView.setBackgroundColor(Color.RED)
} else {
itemView.setBackgroundColor(Color.parseColor("#4a8a9e"))
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -49,20 +78,16 @@ class ChairAdapter (private val item : ArrayList<Asiento>): RecyclerView.Adapter
return item.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//holder.chair.text=item[position].noAsiento.toString()
// val chair = item[position]
holder.chair.text = item[position].noAsiento.toString()
if (selectedItems.contains(position)) {
holder.itemView.setBackgroundColor(Color.RED)
} else {
holder.itemView.setBackgroundColor(Color.parseColor("#225c6e"))
holder.bind(position)
}
fun actulizarValor(status : String, reference: DatabaseReference){
reference.setValue(status)
}
holder.itemView.setOnClickListener {
toggleSelection(position)
fun getOccupiedSeats(): List<String> {
return occupiedSeats
}
}
}

View File

@ -19,7 +19,9 @@ class ChairList : AppCompatActivity() {
private lateinit var dbreference : DatabaseReference
private lateinit var recycleView: RecyclerView
private lateinit var eventList : ArrayList<Asiento>
public lateinit var seleccionados : ArrayList<String>
// private lateinit var chairs : ArrayList<String>
//var arraySillas = arrayListOf<String>()
var total : Int =0
override fun onCreate(savedInstanceState: Bundle?) {
@ -30,6 +32,8 @@ class ChairList : AppCompatActivity() {
val bundle= intent.extras
val nombreEvento=bundle?.getString("nombreEvento")
val noSala=bundle?.getString("sala")
val btnComprar : Button = findViewById(R.id.btnConfirmar)
val btnSalir : Button = findViewById(R.id.btncancelar)
val nEvento:TextView =findViewById(R.id.eventoText)
nEvento.text=nombreEvento
@ -37,25 +41,33 @@ class ChairList : AppCompatActivity() {
val nSala:TextView=findViewById(R.id.noSalaText)
nSala.text=noSala
val btnConfirmar:Button = findViewById(R.id.btnConfirmar)
val btnCancelar:Button = findViewById(R.id.btncancelar)
recycleView = findViewById(R.id.chairRecycleView)
recycleView.layoutManager = GridLayoutManager(this,4)
recycleView.layoutManager = GridLayoutManager(this,2)
recycleView.setHasFixedSize(true)
eventList = arrayListOf<Asiento>()
// chairs= arrayListOf<String>()
getEvents()
btnConfirmar.setOnClickListener(View.OnClickListener() {
btnComprar.setOnClickListener{
val adapter = recycleView.adapter as ChairAdapter
val asientosOcupados = adapter.getOccupiedSeats()
val intent = Intent(this, compra::class.java)
intent.putStringArrayListExtra("asientosSeleccionados", ArrayList(asientosOcupados))
startActivity(intent)
})
btnCancelar.setOnClickListener(View.OnClickListener() {
val mensajeToast = if (asientosOcupados.isNotEmpty()) {
"Asientos ocupados: ${asientosOcupados.joinToString(", ")}"
} else {
"No hay asientos ocupados."
}
Toast.makeText(this@ChairList, mensajeToast, Toast.LENGTH_LONG).show()
}
btnSalir.setOnClickListener{
val intent = Intent(this, EventList::class.java)
startActivity(intent)
})
}
}
private fun getEvents(){
@ -73,11 +85,11 @@ class ChairList : AppCompatActivity() {
adapter.setOnItemClickListener(object : ChairAdapter.onItemClickListener{
override fun onItemClick(position: Int) {
Toast.makeText(this@ChairList,"No deja",Toast.LENGTH_SHORT).show()
adapter.toggleSelection(position)
}
})
// total= adapter.getTotal()
}
}
@ -88,4 +100,6 @@ class ChairList : AppCompatActivity() {
})
}
}

View File

@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
@ -18,6 +19,14 @@ class compra: AppCompatActivity() {
val editTextNombre: EditText = findViewById(R.id.editTextNombre)
val editTextTelefono: EditText = findViewById(R.id.editTextTelefono)
val textViewAsientos: TextView = findViewById(R.id.textView5Result) // Este es tu TextView
// Obtener la lista de asientos seleccionados del intent
val asientosSeleccionados = intent.getStringArrayListExtra("asientosSeleccionados")
// Mostrar la lista de asientos seleccionados en el TextView
textViewAsientos.text = asientosSeleccionados?.joinToString(", ")
btnComprar.setOnClickListener {
val nombre = editTextNombre.text.toString()
val telefono = editTextTelefono.text.toString()
@ -40,9 +49,15 @@ class compra: AppCompatActivity() {
}
private fun mostrarMensaje(mensaje: String) {
val asientosSeleccionados = intent.getStringArrayListExtra("asientosSeleccionados")
val mensajeAsientos = asientosSeleccionados?.joinToString(", ") ?: "Ningún asiento seleccionado"
val mensajeCompleto = "$mensaje\n\nAsientos seleccionados:\n$mensajeAsientos"
val builder = AlertDialog.Builder(this)
builder.setTitle("Datos de la compra")
builder.setMessage(mensaje)
builder.setMessage(mensajeCompleto)
builder.setPositiveButton("Aceptar", null)
val dialog = builder.create()
dialog.show()