From 7f81bfb402e221fa6ce712ea38783dd46a3656ed Mon Sep 17 00:00:00 2001 From: Yannine Alvarez Date: Thu, 14 May 2020 15:11:44 -0400 Subject: [PATCH] Agregar funciĆ³n room. --- .idea/vcs.xml | 6 ++++++ app/build.gradle | 6 ++++++ app/src/main/java/com/example/ayudapy/Dao.kt | 11 ++++++++++- app/src/main/java/com/example/ayudapy/DataBaseRoom.kt | 26 ++++++++++---------------- app/src/main/java/com/example/ayudapy/MapsActivity.kt | 41 ++++++++++++++++++++++++++++++++++++----- app/src/main/java/com/example/ayudapy/Repository.kt | 4 ++++ app/src/main/res/drawable-xxhdpi/icon_pendientes_checked.png | Bin 0 -> 3290 bytes 7 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/example/ayudapy/Repository.kt create mode 100644 app/src/main/res/drawable-xxhdpi/icon_pendientes_checked.png diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 2e78b11..24e3ff0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +//apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' @@ -63,6 +64,7 @@ dependencies { //implementation 'com.squareup.okio:okio:2.4.3' //noinspection GradleCompatible + implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'androidx.cardview:cardview:1.0.0' @@ -73,8 +75,12 @@ dependencies { implementation 'android.arch.persistence.room:runtime:1.1.1' kapt 'android.arch.persistence.room:compiler:1.1.1' + implementation 'org.jetbrains.anko:anko-common:0.9' + //implementation "android.arch.lifecycle:extensions:1.1.1" //implementation "android.arch.persistence.room:runtime:1.0.0-rc1" //kapt "android.arch.persistence.room:compiler:1.0.0-rc1" + + } diff --git a/app/src/main/java/com/example/ayudapy/Dao.kt b/app/src/main/java/com/example/ayudapy/Dao.kt index ec0020b..e6e409d 100644 --- a/app/src/main/java/com/example/ayudapy/Dao.kt +++ b/app/src/main/java/com/example/ayudapy/Dao.kt @@ -9,12 +9,21 @@ interface DaoPedido { @Query("SELECT * from pedido_save") fun getAll(): List - @Insert(onConflict = REPLACE) + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(pedido: PedidoSave) @Delete fun delete(pedido: PedidoSave) } +/* +* @Dao +interface studentDao{ + @Query("SELECT * FROM studentTbl") + fun allStudents():List + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(student:Student) +}*/ /* @Query("UPDATE pedido_save SET name =:studentName, nim =:studentNim, gender =:studentGen WHERE id =:studentId") fun update(studentId: Long, studentName:String, studentNim:String, studentGen:String)*/ diff --git a/app/src/main/java/com/example/ayudapy/DataBaseRoom.kt b/app/src/main/java/com/example/ayudapy/DataBaseRoom.kt index 1f58781..a223646 100644 --- a/app/src/main/java/com/example/ayudapy/DataBaseRoom.kt +++ b/app/src/main/java/com/example/ayudapy/DataBaseRoom.kt @@ -4,27 +4,21 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase - @Database(entities = arrayOf(PedidoSave::class), version = 1) -abstract class PedidoDataBase : RoomDatabase() { +abstract class pedidoDataBase : RoomDatabase() { + abstract fun daoPedido():DaoPedido - abstract fun daoPedido(): DaoPedido companion object { - private var INSTANCE: PedidoDataBase? = null - - fun getInstance(context: Context): PedidoDataBase? { + private var INSTANCE: pedidoDataBase? = null + fun getInstance(context: Context): pedidoDataBase { if (INSTANCE == null) { - synchronized(PedidoDataBase::class) { - INSTANCE = Room.databaseBuilder(context.getApplicationContext(), - PedidoDataBase::class.java, "pedidodata.db") - .build() - } + INSTANCE = Room.databaseBuilder( + context, + pedidoDataBase::class.java, + "roomdb") + .build() } - return INSTANCE - } - - fun destroyInstance() { - INSTANCE = null + return INSTANCE as pedidoDataBase } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/ayudapy/MapsActivity.kt b/app/src/main/java/com/example/ayudapy/MapsActivity.kt index b4d3efb..9bc1c8d 100644 --- a/app/src/main/java/com/example/ayudapy/MapsActivity.kt +++ b/app/src/main/java/com/example/ayudapy/MapsActivity.kt @@ -30,13 +30,18 @@ import com.google.android.gms.maps.model.Marker import com.google.android.gms.maps.model.MarkerOptions import com.google.android.material.bottomsheet.BottomSheetDialog import kotlinx.android.synthetic.main.alert_dialog_contacto.view.* +import kotlinx.android.synthetic.main.fragment_pedido.* import kotlinx.android.synthetic.main.fragment_pedido.view.* +import kotlinx.android.synthetic.main.fragment_pedido.view.pedido_id +import org.jetbrains.anko.doAsync import retrofit2.Call import retrofit2.Callback import retrofit2.Response import java.time.LocalDateTime import java.time.format.DateTimeFormatter + + class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener { private lateinit var mMap: GoogleMap @@ -45,6 +50,9 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker internal lateinit var infoButton: Button private var selectedMarker:Marker?=null + + private lateinit var mDb:pedidoDataBase + companion object{ private const val LOCATION_PERMISSION_REQUEST_CODE=1 } @@ -68,7 +76,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker return false } } - private var pedidoDatabase:PedidoDataBase? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -79,6 +87,10 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker .findFragmentById(R.id.map) as SupportMapFragment mapFragment.getMapAsync(this) + + // Initialize the room database + mDb = pedidoDataBase.getInstance(applicationContext) + fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) //intent para mandar a la siguiente pantalla el boton @@ -120,7 +132,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker Log.i("Mensaje", "Se tiene permiso!") } - pedidoDatabase = PedidoDataBase.getInstance(this) + } @@ -331,6 +343,28 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker pedidoDetalle.title,pedidoDetalle.message,pedidoDetalle.name,pedidoDetalle.phone, pedidoDetalle.address,pedidoDetalle.added))*/ + // Initialize a new student + val pedido = PedidoSave(id = pedidoDetalle!!.id, + added = pedidoDetalle!!.added, + address = pedidoDetalle!!.address, + message = pedidoDetalle!!.message, + name = pedidoDetalle!!.name, + phone = pedidoDetalle!!.phone, + title = pedidoDetalle!!.title + + ) + + + doAsync { + //Application.database.myDAO().insertUser(user) + mDb.daoPedido().insert(pedido) + + + + } + + view.pendiente_button.setBackgroundResource(R.drawable.icon_pendientes_checked) + } view.listo_button.setOnClickListener{ @@ -350,9 +384,6 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker ) } - /* fun insertToDb(pedido:PedidoSave){ - - }*/ //permiso para acceder a la ubicacion del dispositvo private fun setup(){ diff --git a/app/src/main/java/com/example/ayudapy/Repository.kt b/app/src/main/java/com/example/ayudapy/Repository.kt new file mode 100644 index 0000000..3e0db7d --- /dev/null +++ b/app/src/main/java/com/example/ayudapy/Repository.kt @@ -0,0 +1,4 @@ +package com.example.ayudapy + +class Repository { +} \ No newline at end of file diff --git a/app/src/main/res/drawable-xxhdpi/icon_pendientes_checked.png b/app/src/main/res/drawable-xxhdpi/icon_pendientes_checked.png new file mode 100644 index 0000000..ae4a9f2 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/icon_pendientes_checked.png differ -- libgit2 0.26.0