From b49828a221c14ddb2effdcfd1eea186edc42b997 Mon Sep 17 00:00:00 2001 From: sebastiancc27 Date: Thu, 9 May 2024 22:27:17 -0600 Subject: [PATCH 1/9] Descargar PDF en pruebas --- .idea/deploymentTargetDropDown.xml | 15 +- app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 3 + .../main/java/Adapters/CellClickListener.kt | 7 + .../java/Adapters/DoctorDataBaseHelper.kt | 34 ++++- .../java/Adapters/RecetaDataBaseHelper.kt | 39 +++++- .../main/java/Adapters/RecetaViewAdapter.kt | 46 ++++++ app/src/main/java/Data/Receta.kt | 2 +- .../medicalhealth/MainActivity.kt | 8 ++ .../PacientePrincipalActivity.kt | 132 ++++++++++++++++++ .../layout/activity_paciente_principal.xml | 20 +++ app/src/main/res/layout/rc_receta_item.xml | 70 ++++++++++ 12 files changed, 354 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/Adapters/CellClickListener.kt create mode 100644 app/src/main/java/Adapters/RecetaViewAdapter.kt create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/PacientePrincipalActivity.kt create mode 100644 app/src/main/res/layout/activity_paciente_principal.xml create mode 100644 app/src/main/res/layout/rc_receta_item.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index ab80313..0c0c338 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -3,20 +3,7 @@ - - - - - - - - - - - - - - + diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8e3077e..e562c0c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -51,6 +51,7 @@ dependencies { implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1") implementation("androidx.navigation:navigation-fragment-ktx:2.6.0") + implementation("com.dmitryborodin:pdfview-android:1.1.0") implementation("androidx.navigation:navigation-ui-ktx:2.6.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b61de05..1d28abf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,9 @@ android:supportsRtl="true" android:theme="@style/Theme.MedicalHealth" tools:targetApi="31"> + diff --git a/app/src/main/java/Adapters/CellClickListener.kt b/app/src/main/java/Adapters/CellClickListener.kt new file mode 100644 index 0000000..4a3db70 --- /dev/null +++ b/app/src/main/java/Adapters/CellClickListener.kt @@ -0,0 +1,7 @@ +package Adapters + +import Data.Receta + +interface CellClickListener { + fun onCellClickListener(position : Int) +} \ No newline at end of file diff --git a/app/src/main/java/Adapters/DoctorDataBaseHelper.kt b/app/src/main/java/Adapters/DoctorDataBaseHelper.kt index 8c97e4d..7aa73f2 100644 --- a/app/src/main/java/Adapters/DoctorDataBaseHelper.kt +++ b/app/src/main/java/Adapters/DoctorDataBaseHelper.kt @@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import Data.Doctor import Data.Paciente +import android.database.Cursor class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABASE_NAME,null, DATABASE_VERSION @@ -114,16 +115,43 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA val dropQuery="DROP TABLE IF EXISTS $TABLE_NAME" db?.execSQL(dropQuery) } - fun validarDatos(cedula :String , contrasena : String): Boolean{ + + fun validarDatos(usuario :String , contrasena : String): Boolean{ val db = readableDatabase - val validarQuery = "SELECT * FROM $TABLE_NAME WHERE $COLUMN_CEDULA = '$cedula' AND $COLUMN_CONTRASENA = '$contrasena'" + val validarQuery = "SELECT $COLUMN_CEDULA,$COLUMN_CONTRASENA FROM $TABLE_NAME, $TABLE_PACIENTES WHERE $COLUMN_CEDULA = '$usuario' AND $COLUMN_CONTRASENA = '$contrasena' " val cursor = db.rawQuery(validarQuery, null) val isValid = cursor.count > 0 cursor.close() return isValid } + @SuppressLint("Range") + fun seleccionarDoctores(): List{ + val doctores= mutableListOf() + val db= readableDatabase + val query="SELECT * FROM $TABLE_NAME " + val cursor=db.query(TABLE_NAME,null,null,null,null,null,null) + while (cursor.moveToNext()){ + val cedula=cursor.getString(cursor.getColumnIndex(COLUMN_CEDULA)) + val nombre=cursor.getString(cursor.getColumnIndex(COLUMN_NOMBRE)) + val apellido=cursor.getString(cursor.getColumnIndex(COLUMN_APELLIDO)) + val telefono = cursor.getString(cursor.getColumnIndex(COLUMN_TELEFONO)) + val consultorio=cursor.getInt(cursor.getColumnIndex(COLUMN_CONSULTORIO)) + val contrasena=cursor.getString(cursor.getColumnIndex(COLUMN_CONTRASENA)) + val doctor = Doctor(cedula,nombre,apellido,telefono,consultorio) + doctores.add(doctor) + } + return doctores + } - fun edadPaciente(nombrePaciente : String) : Int{ + fun validarDatosPaciente(usuario : String, contrasena: String): Boolean{ + val db = readableDatabase + val validarQuery = "SELECT $COLUMN_ID_PACIENTE,$COLUMN_NOMBRE_PACIENTE FROM $TABLE_PACIENTES WHERE $COLUMN_ID_PACIENTE = '$usuario' AND $COLUMN_NOMBRE_PACIENTE = '$contrasena' " + val cursor = db.rawQuery(validarQuery, null) + val isValid = cursor.count > 0 + cursor.close() + return isValid + } + fun edadPaciente(nombrePaciente : String) : Int{ val db=readableDatabase var edadPaciente =0 val queryEdad= "SELECT $COLUMN_EDAD FROM $TABLE_PACIENTES WHERE $COLUMN_NOMBRE_PACIENTE='${nombrePaciente}'" diff --git a/app/src/main/java/Adapters/RecetaDataBaseHelper.kt b/app/src/main/java/Adapters/RecetaDataBaseHelper.kt index 592cb2f..fba00ca 100644 --- a/app/src/main/java/Adapters/RecetaDataBaseHelper.kt +++ b/app/src/main/java/Adapters/RecetaDataBaseHelper.kt @@ -4,6 +4,7 @@ import Data.Receta import android.annotation.SuppressLint import android.content.ContentValues import android.content.Context +import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper @@ -53,15 +54,38 @@ class RecetaDataBaseHelper(context: Context) : SQLiteOpenHelper(context, DATABAE val recetas = mutableListOf() val db = readableDatabase val cursor = db.rawQuery("SELECT * FROM $TABLE_NAME", null) + //val cursor : Cursor =db.query(TABLE_NAME,null,null,null,null,null,null) + if(cursor!=null){ + while (cursor.moveToNext()) { + val nombrePaciente = cursor.getString(cursor.getColumnIndex(COLUMN_NOMBRE_PACIENTE)) + val edadPaciente = cursor.getInt(cursor.getColumnIndex(COLUMN_EDAD_PACIENTE)) + val pesoPaciente = cursor.getDouble(cursor.getColumnIndex(COLUMN_PESO_PACIENTE)) + val medicamento = cursor.getString(cursor.getColumnIndex(COLUMN_MEDICAMENTO)) + val fecha = cursor.getString(cursor.getColumnIndex(COLUMN_FECHA)) + val dosis = cursor.getString(cursor.getColumnIndex(COLUMN_DOSIS_PACIENTE)) + val duracion = cursor.getString(cursor.getColumnIndex(DURACION_MEDICAMENTO)) + val receta = Receta(nombrePaciente, edadPaciente, pesoPaciente, medicamento, fecha, dosis, duracion) + recetas.add(receta) + } + cursor.close() + } + db.close() + return recetas + } + @SuppressLint("Range") + fun obtenerRecetasPaciente(nombrePaciente : String): List{ + val recetas = mutableListOf() + val db = readableDatabase + val cursor = db.rawQuery("SELECT $COLUMN_NOMBRE_PACIENTE,$COLUMN_MEDICAMENTO, $COLUMN_FECHA FROM $TABLE_NAME WHERE $COLUMN_NOMBRE_PACIENTE = '$nombrePaciente'", null) while (cursor.moveToNext()) { val nombrePaciente = cursor.getString(cursor.getColumnIndex(COLUMN_NOMBRE_PACIENTE)) - val edadPaciente = cursor.getInt(cursor.getColumnIndex(COLUMN_EDAD_PACIENTE)) - val pesoPaciente = cursor.getDouble(cursor.getColumnIndex(COLUMN_PESO_PACIENTE)) + //val edadPaciente = cursor.getInt(cursor.getColumnIndex(COLUMN_EDAD_PACIENTE)) + //val pesoPaciente = cursor.getDouble(cursor.getColumnIndex(COLUMN_PESO_PACIENTE)) val medicamento = cursor.getString(cursor.getColumnIndex(COLUMN_MEDICAMENTO)) val fecha = cursor.getString(cursor.getColumnIndex(COLUMN_FECHA)) - val dosis = cursor.getString(cursor.getColumnIndex(COLUMN_DOSIS_PACIENTE)) - val duracion = cursor.getString(cursor.getColumnIndex(DURACION_MEDICAMENTO)) - val receta = Receta(nombrePaciente, edadPaciente, pesoPaciente, medicamento, fecha, dosis, duracion) + //val dosis = cursor.getString(cursor.getColumnIndex(COLUMN_DOSIS_PACIENTE)) + //val duracion = cursor.getString(cursor.getColumnIndex(DURACION_MEDICAMENTO)) + val receta = Receta(nombrePaciente, 0, 0.0, medicamento, fecha, "dosis", "duracion") recetas.add(receta) } cursor.close() @@ -73,4 +97,9 @@ class RecetaDataBaseHelper(context: Context) : SQLiteOpenHelper(context, DATABAE db.delete(TABLE_NAME, "$COLUMN_NOMBRE_PACIENTE = ? AND $COLUMN_FECHA = ?", arrayOf(receta.NombrePaciente, receta.fecha)) db.close() } + fun getCursor() : Cursor?{ + val db = writableDatabase + val cursor = db.rawQuery("SELECT * FROM $TABLE_NAME", null) + return cursor + } } diff --git a/app/src/main/java/Adapters/RecetaViewAdapter.kt b/app/src/main/java/Adapters/RecetaViewAdapter.kt new file mode 100644 index 0000000..84607bf --- /dev/null +++ b/app/src/main/java/Adapters/RecetaViewAdapter.kt @@ -0,0 +1,46 @@ +package Adapters + +import Data.Receta +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView.OnItemClickListener +import android.widget.Button +import android.widget.TextView +import android.widget.Toast +import androidx.recyclerview.widget.RecyclerView +import com.terratenientes.medicalhealth.R + +class RecetaViewAdapter(private var notes : List, context: Context,val cellClickListener: CellClickListener) : + RecyclerView.Adapter() { + class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){ + val tituloReceta : TextView = itemView.findViewById(R.id.tv_titulo_receta) + val fechaReceta : TextView = itemView.findViewById(R.id.tv_fecha_receta) + val btnDescargar : Button = itemView.findViewById(R.id.btn_descargar_receta) + + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.rc_receta_item, parent, false) + return NoteViewHolder(view) + } + + override fun getItemCount(): Int { + return notes.size + } + + override fun onBindViewHolder(holder: NoteViewHolder, position: Int) { + val note = notes[position] + holder.tituloReceta.text = note.nombreMedicamento + holder.fechaReceta.text = note.fecha + holder.btnDescargar.setOnClickListener{ + cellClickListener.onCellClickListener(position) + } + } + + fun refreshData( newNotes : List){ + this.notes=newNotes + notifyDataSetChanged() + } +} \ No newline at end of file diff --git a/app/src/main/java/Data/Receta.kt b/app/src/main/java/Data/Receta.kt index d31a06c..22fab2c 100644 --- a/app/src/main/java/Data/Receta.kt +++ b/app/src/main/java/Data/Receta.kt @@ -1,3 +1,3 @@ package Data -data class Receta(val NombrePaciente : String, val edadPaciente : Int, val pesoPaciente: Double, val nombreMedicamento : String, val fecha : String, val dosis : String, val duracion :String) +data class Receta(var NombrePaciente : String, var edadPaciente : Int, var pesoPaciente: Double, var nombreMedicamento : String, var fecha : String, var dosis : String, var duracion :String) diff --git a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt index ef65f3b..9493193 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt @@ -19,11 +19,19 @@ class MainActivity : AppCompatActivity() { setContentView(view) db = DoctorDataBaseHelper(this@MainActivity) + //db.createTable() binding.btnIniciarSesion.setOnClickListener { var res=db.validarDatos(binding.tvUsuario.text.toString(),binding.tvContrasena.text.toString()) + var resPaciente = db.validarDatosPaciente(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, PrincipalActivity::class.java) + //intent.putExtra("IDPaciente",binding.tvUsuario.text.toString()) + startActivity(intent) + }else if(resPaciente==true){ + Toast.makeText(this@MainActivity,"VALIDADO",Toast.LENGTH_LONG).show() + val intent = Intent(this@MainActivity, PacientePrincipalActivity::class.java) + intent.putExtra("IDPaciente",binding.tvContrasena.text.toString()) startActivity(intent) }else{ Toast.makeText(this@MainActivity," NO VALIDADO",Toast.LENGTH_LONG).show() diff --git a/app/src/main/java/com/terratenientes/medicalhealth/PacientePrincipalActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/PacientePrincipalActivity.kt new file mode 100644 index 0000000..9849528 --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/PacientePrincipalActivity.kt @@ -0,0 +1,132 @@ +package com.terratenientes.medicalhealth + +import Adapters.CellClickListener +import Adapters.RecetaDataBaseHelper +import Adapters.RecetaViewAdapter +import Data.Receta +import android.Manifest +import android.content.pm.PackageManager +import android.graphics.Typeface +import android.graphics.pdf.PdfDocument +import android.os.Bundle +import android.os.Environment +import android.text.TextPaint +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat +import androidx.recyclerview.widget.LinearLayoutManager +import com.terratenientes.medicalhealth.databinding.ActivityPacientePrincipalBinding +import java.io.File +import java.io.FileOutputStream + +class PacientePrincipalActivity : AppCompatActivity(), CellClickListener { + private lateinit var binding: ActivityPacientePrincipalBinding + private var idPaciente : String ="" + private lateinit var db: RecetaDataBaseHelper + private lateinit var recycleAdapter : RecetaViewAdapter + private lateinit var recetas : List + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityPacientePrincipalBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) + idPaciente= intent.getStringExtra("IDPaciente")!! + db= RecetaDataBaseHelper(this) + recetas=db.obtenerRecetasPaciente(idPaciente) + recycleAdapter = RecetaViewAdapter(db.obtenerRecetasPaciente(idPaciente), this, this@PacientePrincipalActivity) + + binding.rvRecetasPaciente.layoutManager=LinearLayoutManager(this) + binding.rvRecetasPaciente.adapter= recycleAdapter + } + + override fun onResume() { + super.onResume() + recycleAdapter.refreshData(db.obtenerRecetasPaciente(idPaciente)) + } + + override fun onCellClickListener(position: Int) { + val receta = recetas[position] + val nombrePaciente = receta.NombrePaciente + val nombreMeciamento=receta.nombreMedicamento + val dosisMedicamento = receta.dosis + val duracionMedicamento = receta.duracion + val fechaReceta = receta.fecha + val pesoPaciente= receta.pesoPaciente + val edadPaciente = receta.edadPaciente + val descripcionReceta ="Ejemplo de descripcion" + + if(permisosPDF()){ + //Toast.makeText(this@PacientePrincipalActivity, "PERMISOS CONCEDIDOS", Toast.LENGTH_SHORT).show() + generarPDF(nombrePaciente, descripcionReceta) + }else{ + pedirPermisosPDF() + } + } + // función que valida si los permisos ya estan garantizados por parte del usuario + fun permisosPDF() : Boolean{ + val permission1= ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) + val permission2= ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.READ_EXTERNAL_STORAGE) + + return permission1 == PackageManager.PERMISSION_GRANTED && permission2 == PackageManager.PERMISSION_GRANTED + } + fun pedirPermisosPDF(){ + ActivityCompat.requestPermissions(this, + arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.READ_EXTERNAL_STORAGE + ),200) + } + fun generarPDF(tituloReceta : String, contenidoReceta : String){ + val pdfDoducment = PdfDocument() + val tituloPDF = TextPaint() + val contenido = TextPaint() + val paginaInfo = PdfDocument.PageInfo.Builder(816,1054,1).create() + val paginaReceta = pdfDoducment.startPage(paginaInfo) + + val canvas = paginaReceta.canvas + tituloPDF.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + tituloPDF.textSize=30f + canvas.drawText(tituloReceta,10f,150f,tituloPDF) + + contenido.typeface = Typeface.defaultFromStyle(Typeface.NORMAL) + tituloPDF.textSize=20f + val arrayContenido = contenidoReceta.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + var y=200 + for(i in arrayContenido.indices){ + canvas.drawText(arrayContenido[i],20f,y.toFloat(),contenido) + y=y+15 + } + pdfDoducment.finishPage(paginaReceta) + val file=File(Environment.getExternalStorageDirectory(),"Receta.pdf") + try { + pdfDoducment.writeTo(FileOutputStream(file)) + Toast.makeText(this,"PDF CREADO CORRECTAMENTE",Toast.LENGTH_LONG).show() + }catch (e: Exception){ + Toast.makeText(this,"ERROR CREAR PDF ${e.toString()}",Toast.LENGTH_LONG).show() + + } + pdfDoducment.close() + } + + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if(requestCode==200){ + if(grantResults.isNotEmpty()){ + val writeStorage = grantResults[0]==PackageManager.PERMISSION_GRANTED + val readStorage = grantResults[1]==PackageManager.PERMISSION_GRANTED + if(writeStorage && readStorage){ + Toast.makeText(this@PacientePrincipalActivity, "PERMISOS CONCEDIDOS", Toast.LENGTH_SHORT).show() + }else{ + Toast.makeText(this@PacientePrincipalActivity, "PERMISOS DENEGADOS", Toast.LENGTH_SHORT).show() + finish() + } + } + } + } + + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_paciente_principal.xml b/app/src/main/res/layout/activity_paciente_principal.xml new file mode 100644 index 0000000..df731e6 --- /dev/null +++ b/app/src/main/res/layout/activity_paciente_principal.xml @@ -0,0 +1,20 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/rc_receta_item.xml b/app/src/main/res/layout/rc_receta_item.xml new file mode 100644 index 0000000..1c82905 --- /dev/null +++ b/app/src/main/res/layout/rc_receta_item.xml @@ -0,0 +1,70 @@ + + + + + + + + + +