From 2d9e334a0f3f1a37b1c19dc56fb5959bb28b9a0c Mon Sep 17 00:00:00 2001 From: "luis.reyes" Date: Tue, 19 Mar 2024 18:30:29 -0600 Subject: [PATCH] Actualizaciones en la pantalla Compra --- .../example/pantallacompra/ChairAdapter.kt | 55 ++++++++++++++----- .../com/example/pantallacompra/ChairList.kt | 36 ++++++++---- .../java/com/example/pantallacompra/compra.kt | 17 +++++- 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/example/pantallacompra/ChairAdapter.kt b/app/src/main/java/com/example/pantallacompra/ChairAdapter.kt index 3647954..155c913 100644 --- a/app/src/main/java/com/example/pantallacompra/ChairAdapter.kt +++ b/app/src/main/java/com/example/pantallacompra/ChairAdapter.kt @@ -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): RecyclerView.Adapter() { private lateinit var mListener : onItemClickListener - private val selectedItems = ArrayList() + private lateinit var database: FirebaseDatabase + //private lateinit var myRef: DatabaseReference + private val selectedItems = HashSet() + private val occupiedSeats = ArrayList() 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): 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): 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.itemView.setOnClickListener { - toggleSelection(position) - } + holder.bind(position) + } + fun actulizarValor(status : String, reference: DatabaseReference){ + reference.setValue(status) } + fun getOccupiedSeats(): List { + return occupiedSeats + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/pantallacompra/ChairList.kt b/app/src/main/java/com/example/pantallacompra/ChairList.kt index 1067959..5c71b7a 100644 --- a/app/src/main/java/com/example/pantallacompra/ChairList.kt +++ b/app/src/main/java/com/example/pantallacompra/ChairList.kt @@ -19,7 +19,9 @@ class ChairList : AppCompatActivity() { private lateinit var dbreference : DatabaseReference private lateinit var recycleView: RecyclerView private lateinit var eventList : ArrayList - public lateinit var seleccionados : ArrayList + // private lateinit var chairs : ArrayList + //var arraySillas = arrayListOf() + 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() + // chairs= arrayListOf() + 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() { }) } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/pantallacompra/compra.kt b/app/src/main/java/com/example/pantallacompra/compra.kt index 5f8243d..cf0b553 100644 --- a/app/src/main/java/com/example/pantallacompra/compra.kt +++ b/app/src/main/java/com/example/pantallacompra/compra.kt @@ -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()