From 77ba869d7201d490440b0c34d49a98ec500809a1 Mon Sep 17 00:00:00 2001 From: mosterTeco Date: Sun, 21 Apr 2024 00:07:43 -0600 Subject: [PATCH] Funcionalidad Pantalla Agregar --- .../medicalhealth/AgregarActivity.kt | 33 ++++++++++++++- .../medicalhealth/DoctorDataBaseHelper.kt | 42 +++++++++++++++++-- .../medicalhealth/EliminarActivity.kt | 11 ++++- .../medicalhealth/MainActivity.kt | 8 ++++ .../res/layout/activity_agregar_paciente.xml | 36 ++++++++++------ .../res/layout/activity_eliminar_paciente.xml | 2 + app/src/main/res/layout/activity_main.xml | 11 +++++ 7 files changed, 123 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt index 038b22f..52bccc0 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt @@ -1,15 +1,44 @@ package com.terratenientes.medicalhealth import android.os.Bundle +import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import com.terratenientes.medicalhealth.databinding.ActivityAgregarPacienteBinding +import com.terratenientes.medicalhealth.databinding.ActivityMainBinding class AgregarActivity : AppCompatActivity() { + + private lateinit var binding: ActivityAgregarPacienteBinding + private lateinit var db: DoctorDataBaseHelper + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - enableEdgeToEdge() - setContentView(R.layout.activity_agregar_paciente) + binding = ActivityAgregarPacienteBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) + db = DoctorDataBaseHelper(this@AgregarActivity) + + binding.btnConfirmar.setOnClickListener { + agregarPaciente() + } + } + private fun agregarPaciente() { + val nombre = binding.edtxNombre.text.toString() + val apellidoPaterno = binding.edtxApellidoPat.text.toString() + val apellidoMaterno = binding.edtxApellidoMat.text.toString() + val edad = binding.edtxEdad.text.toString().toIntOrNull() + val sexo = binding.edtxSexo.text.toString() + val domicilio = binding.edtxDomicilio.text.toString() + + if (nombre.isNotEmpty() && apellidoPaterno.isNotEmpty() && apellidoMaterno.isNotEmpty() && edad != null && sexo.isNotEmpty() && domicilio.isNotEmpty()) { + val paciente = Paciente(0, nombre, apellidoPaterno, apellidoMaterno, edad, sexo, domicilio) + db.agregarPaciente(paciente) + Toast.makeText(this@AgregarActivity, "Paciente registrado correctamente", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(this@AgregarActivity, "Por favor ingrese todos los campos", Toast.LENGTH_SHORT).show() + } } } \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt index 62af9c8..9181da4 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt @@ -19,19 +19,35 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA private const val COLUMN_TELEFONO="Telefono" private const val COLUMN_CONSULTORIO="Consultorio" private const val COLUMN_CONTRASENA="Contrasena" + private const val TABLE_PACIENTES = "Pacientes" + private const val COLUMN_ID_PACIENTE = "IdPaciente" + private const val COLUMN_NOMBRE_PACIENTE = "NombrePaciente" + private const val COLUMN_APELLIDO_PATERNO_PACIENTE = "ApellidoPaternoPaciente" + private const val COLUMN_APELLIDO_MATERNO_PACIENTE = "ApellidoMaternoPaciente" + private const val COLUMN_EDAD = "Edad" + private const val COLUMN_SEXO = "Sexo" + private const val COLUMN_DOMICILIO = "Domicilio" } - - @SuppressLint("SuspiciousIndentation") override fun onCreate(db: SQLiteDatabase?) { val crearTablaDoctores="CREATE TABLE $TABLE_NAME($COLUMN_CEDULA VARCHAR(30) PRIMARY KEY, $COLUMN_NOMBRE VARCHAR(30)," + "$COLUMN_APELLIDO VARCHAR(30), $COLUMN_TELEFONO VARCHAR(30), $COLUMN_CONSULTORIO INTEGER, $COLUMN_CONTRASENA VARCHAR(30));" db?.execSQL(crearTablaDoctores) + + val crearTablaPacientes = + "CREATE TABLE $TABLE_PACIENTES($COLUMN_ID_PACIENTE INTEGER PRIMARY KEY AUTOINCREMENT, " + + "$COLUMN_NOMBRE_PACIENTE VARCHAR(30), $COLUMN_APELLIDO_PATERNO_PACIENTE VARCHAR(30), " + + "$COLUMN_APELLIDO_MATERNO_PACIENTE VARCHAR(30), $COLUMN_EDAD INTEGER, $COLUMN_SEXO VARCHAR(10), " + + "$COLUMN_DOMICILIO VARCHAR(100));" + db?.execSQL(crearTablaPacientes) } override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) { val dropTableDoctores = "DROP TABLE IF EXISTS $TABLE_NAME" db?.execSQL(dropTableDoctores) + + val dropTablePacientes = "DROP TABLE IF EXISTS $TABLE_PACIENTES" + db?.execSQL(dropTablePacientes) } fun agregarDoctores(doctor : Doctor, contrasena: String){ val db=writableDatabase @@ -46,6 +62,19 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA db.insert(TABLE_NAME,null, values) db.close() } + fun agregarPaciente(paciente: Paciente) { + val db = writableDatabase + val values = ContentValues().apply { + put(COLUMN_NOMBRE_PACIENTE, paciente.nombre) + put(COLUMN_APELLIDO_PATERNO_PACIENTE, paciente.apellidoPaterno) + put(COLUMN_APELLIDO_MATERNO_PACIENTE, paciente.apellidoMaterno) + put(COLUMN_EDAD, paciente.edad) + put(COLUMN_SEXO, paciente.sexo) + put(COLUMN_DOMICILIO, paciente.domicilio) + } + db.insert(TABLE_PACIENTES, null, values) + db.close() + } fun createTable(){ val db=writableDatabase val crearTablaDoctores="CREATE TABLE $TABLE_NAME($COLUMN_CEDULA VARCHAR(30) PRIMARY KEY, $COLUMN_NOMBRE VARCHAR(30)," + @@ -66,5 +95,12 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA return isValid } + fun eliminarPaciente(paciente: Paciente) { + val db = writableDatabase + val selection = "$COLUMN_ID_PACIENTE = ?" + val selectionArgs = arrayOf(paciente.id.toString()) + db.delete(TABLE_PACIENTES, selection, selectionArgs) + db.close() + } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt index 64889e0..8205c88 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt @@ -5,11 +5,18 @@ import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import com.terratenientes.medicalhealth.databinding.ActivityAgregarPacienteBinding +import com.terratenientes.medicalhealth.databinding.ActivityEliminarPacienteBinding class EliminarActivity : AppCompatActivity() { + + private lateinit var binding: ActivityEliminarPacienteBinding + private lateinit var db: DoctorDataBaseHelper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - enableEdgeToEdge() - setContentView(R.layout.activity_eliminar_paciente) + binding = ActivityEliminarPacienteBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) + db = DoctorDataBaseHelper(this@EliminarActivity) } } \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt index 8323827..6366694 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt @@ -1,6 +1,7 @@ package com.terratenientes.medicalhealth import android.annotation.SuppressLint +import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast @@ -20,10 +21,17 @@ class MainActivity : AppCompatActivity() { var res=db.validarDatos(binding.tvUsuario.text.toString(),binding.tvContrasena.text.toString()) if(res==true){ Toast.makeText(this@MainActivity,"VALIDADO",Toast.LENGTH_LONG).show() + val intent = Intent(this@MainActivity, AgregarActivity::class.java) + startActivity(intent) }else{ Toast.makeText(this@MainActivity," NO VALIDADO",Toast.LENGTH_LONG).show() } } + + binding.btnPrueba.setOnClickListener { + val intent = Intent(this@MainActivity, AgregarActivity::class.java) + startActivity(intent) + } } @SuppressLint("SuspiciousIndentation") private fun cargarDatos(){ diff --git a/app/src/main/res/layout/activity_agregar_paciente.xml b/app/src/main/res/layout/activity_agregar_paciente.xml index 6e7c019..31502e5 100644 --- a/app/src/main/res/layout/activity_agregar_paciente.xml +++ b/app/src/main/res/layout/activity_agregar_paciente.xml @@ -10,7 +10,7 @@ @@ -52,56 +55,63 @@ android:layout_marginStart="0dp" android:text="@string/et_modificar_sexo" /> - + android:layout_marginStart="15dp" + android:layout_marginEnd="10dp" />