From dc67dd59df2ead432da90546d7545c26de208539 Mon Sep 17 00:00:00 2001 From: Yannine Alvarez Date: Mon, 18 May 2020 21:27:07 -0400 Subject: [PATCH] Agregar repository, model, adapter para recyclerview. --- app/build.gradle | 10 ++++++++++ app/src/main/java/com/example/ayudapy/Dao.kt | 5 +++-- app/src/main/java/com/example/ayudapy/ListaModel.kt | 15 +++++++++++++++ app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ app/src/main/java/com/example/ayudapy/MapsActivity.kt | 11 +++++++++-- app/src/main/java/com/example/ayudapy/PendienteLista.kt | 24 +++++++++++++++++++++++- app/src/main/java/com/example/ayudapy/Repository.kt | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- app/src/main/res/layout/pendientes_items.xml | 7 ++++++- app/src/main/res/values/colors.xml | 4 ++++ app/src/main/res/values/styles.xml | 24 ++++++++++++++++++++++++ 10 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/example/ayudapy/ListaModel.kt create mode 100644 app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt diff --git a/app/build.gradle b/app/build.gradle index c5ee411..4de0469 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -79,6 +79,16 @@ dependencies { implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1' + // ViewModel and LiveData + def lifecycle_version = "2.0.0" + implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" + + +// // COROUTINES +// api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines" +// api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines" + + //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 e6e409d..9e9a582 100644 --- a/app/src/main/java/com/example/ayudapy/Dao.kt +++ b/app/src/main/java/com/example/ayudapy/Dao.kt @@ -6,8 +6,9 @@ import androidx.room.OnConflictStrategy.REPLACE @Dao interface DaoPedido { - @Query("SELECT * from pedido_save") - fun getAll(): List + + @Query("SELECT * FROM pedido_save") + fun getAll(): LiveData> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(pedido: PedidoSave) diff --git a/app/src/main/java/com/example/ayudapy/ListaModel.kt b/app/src/main/java/com/example/ayudapy/ListaModel.kt new file mode 100644 index 0000000..8fd4e24 --- /dev/null +++ b/app/src/main/java/com/example/ayudapy/ListaModel.kt @@ -0,0 +1,15 @@ +package com.example.ayudapy + +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import android.app.Application + + +class PedidoViewModel(application:Application): AndroidViewModel(application){ + private val db:pedidoDataBase = pedidoDataBase.getInstance(application) + internal val allPedidos : LiveData> = db.daoPedido().getAll() + + fun insert(student:PedidoSave){ + db.daoPedido().insert(student) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt b/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt new file mode 100644 index 0000000..c55c9bb --- /dev/null +++ b/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt @@ -0,0 +1,46 @@ +package com.example.ayudapy + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.pendientes_items.view.* + +class RecyclerViewAdapter(val pedido: List) + : RecyclerView.Adapter(){ + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) + : RecyclerViewAdapter.ViewHolder { + val v: View = LayoutInflater.from(parent.context) + .inflate(R.layout.pendientes_items,parent,false) + return ViewHolder(v) + } + + override fun onBindViewHolder(holder: RecyclerViewAdapter.ViewHolder, position: Int) { + + holder.date_day.text = pedido[position].added + holder.title.text = pedido[position].title + holder.message_view.text = pedido[position].message + } + + override fun getItemCount(): Int { + return pedido.size + } + + override fun getItemId(position: Int): Long { + return super.getItemId(position) + } + + override fun getItemViewType(position: Int): Int { + return super.getItemViewType(position) + } + + class ViewHolder(itemView:View): RecyclerView.ViewHolder(itemView){ + val date_day = itemView.date_day + val title = itemView.title + val message_view = itemView.message_view + } +} + diff --git a/app/src/main/java/com/example/ayudapy/MapsActivity.kt b/app/src/main/java/com/example/ayudapy/MapsActivity.kt index 1dac0fd..75be4be 100644 --- a/app/src/main/java/com/example/ayudapy/MapsActivity.kt +++ b/app/src/main/java/com/example/ayudapy/MapsActivity.kt @@ -18,6 +18,10 @@ import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.LocationServices import com.google.android.gms.maps.CameraUpdateFactory @@ -32,6 +36,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialog import kotlinx.android.synthetic.main.alert_dialog_contacto.view.* import kotlinx.android.synthetic.main.fragment_pedido.view.* import kotlinx.android.synthetic.main.info.* +import kotlinx.android.synthetic.main.pendientes_lista.* import org.jetbrains.anko.doAsync import retrofit2.Call import retrofit2.Callback @@ -51,6 +56,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker private lateinit var mDb:pedidoDataBase + companion object{ private const val LOCATION_PERMISSION_REQUEST_CODE=1 } @@ -124,6 +130,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker + //agregar permiso para llamar val permissionCheck: Int = ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE @@ -363,12 +370,12 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarker mDb.daoPedido().insert(pedidoEntity) - mDb.daoPedido().getAll().forEach() + /* mDb.daoPedido().getAll().forEach() { Log.i("Fetch Records", "Id: ${it.id}") println("Name es de pedido: ${it.name}") - } + }*/ } pedido.start() diff --git a/app/src/main/java/com/example/ayudapy/PendienteLista.kt b/app/src/main/java/com/example/ayudapy/PendienteLista.kt index 23983b8..494fc38 100644 --- a/app/src/main/java/com/example/ayudapy/PendienteLista.kt +++ b/app/src/main/java/com/example/ayudapy/PendienteLista.kt @@ -3,12 +3,34 @@ package com.example.ayudapy import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.pendientes_lista.* class PendienteLista : AppCompatActivity() { + private lateinit var model: PedidoViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.pendientes_lista) - val intento: Intent = intent + + // Get the view model + model = ViewModelProviders.of(this).get(PedidoViewModel::class.java) + + // Specify layout for recycler view + val linearLayoutManager = LinearLayoutManager( + this, RecyclerView.VERTICAL,false) + recycler_view.layoutManager = linearLayoutManager + + + // Observe the model + model.allPedidos.observe(this, Observer{ pedido-> + // Data bind the recycler view + recycler_view.adapter = RecyclerViewAdapter(pedido) + }) + } + } diff --git a/app/src/main/java/com/example/ayudapy/Repository.kt b/app/src/main/java/com/example/ayudapy/Repository.kt index 3e0db7d..cbf9963 100644 --- a/app/src/main/java/com/example/ayudapy/Repository.kt +++ b/app/src/main/java/com/example/ayudapy/Repository.kt @@ -1,4 +1,63 @@ package com.example.ayudapy -class Repository { +import android.os.AsyncTask +import androidx.annotation.WorkerThread +import androidx.lifecycle.LiveData + +// Declares the DAO as a private property in the constructor. Pass in the DAO +// instead of the whole database, because you only need access to the DAO + +class PedidoRepository(private val pedidoDao: DaoPedido) { + + val allPedidos: LiveData> = pedidoDao.getAll() + + @WorkerThread + fun insert(word: PedidoSave) { + pedidoDao.insert(word) + } + + + /* --------------- BORRAR TODOS LOS DATOS -------------- */ + + /*fun deleteAll() { + deleteAllWordsAsyncTask(wordDao).execute() + }*/ + + /* private class deleteAllWordsAsyncTask internal constructor(private val mAsyncTaskDao: DaoPedido) : + AsyncTask() { + + override fun doInBackground(vararg voids: Void): Void? { + mAsyncTaskDao.delete() + return null + } + }*/ + + /* ---------------- BORRAR UN SOLO DATO ---------------- */ + + /* fun deleteWord(word: PedidoSave) { + deleteWordAsyncTask(wordDao).execute(word) + } + + private class deleteWordAsyncTask internal constructor(private val mAsyncTaskDao: DaoPedido) : + AsyncTask() { + + override fun doInBackground(vararg params: PedidoSave): Void? { + mAsyncTaskDao.delete(params[0]) + return null + } + }*/ + + /* -------------- ACTUALIZAR UN SOLO DATO ---------------- + + fun update(word: Word) { + updateWordAsyncTask(wordDao).execute(word) + } + + private class updateWordAsyncTask internal constructor(private val mAsyncTaskDao: WordDao) : + AsyncTask() { + override fun doInBackground(vararg params: Word?): Void? { + mAsyncTaskDao.update(params[0]!!) + return null + } + }*/ } \ No newline at end of file diff --git a/app/src/main/res/layout/pendientes_items.xml b/app/src/main/res/layout/pendientes_items.xml index de3d5d0..8ce7706 100644 --- a/app/src/main/res/layout/pendientes_items.xml +++ b/app/src/main/res/layout/pendientes_items.xml @@ -4,6 +4,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/swipe_layout" + android:background="#FFFFFF" android:layout_width="match_parent" android:layout_height="100dp" app:dragEdge="right" @@ -32,6 +33,7 @@ android:id="@+id/front_layout" android:layout_width="match_parent" android:layout_height="match_parent" + android:background="#FFFFFF" android:paddingStart="8dp" android:paddingEnd="8dp"> @@ -39,6 +41,7 @@ android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="match_parent" + android:background="#FFFFFF" android:clickable="true" android:focusable="true" android:foreground="?android:attr/selectableItemBackground" @@ -51,7 +54,8 @@ + android:layout_height="match_parent" + android:background="#FFFFFF"> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index aa076c7..5cc4186 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -5,4 +5,8 @@ #03DAC5 #33FFFFFF #CCC + #009A9B +#FD5C7F +#f44336 +#388E3C diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c89628c..82e43e4 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -13,5 +13,29 @@ 12dp + + + + + + + + + + -- libgit2 0.26.0