Favorites function now works

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-05-28 10:10:03 +02:00
parent b7d5af23d6
commit d81b9f3799
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
5 changed files with 16 additions and 14 deletions

View File

@ -30,9 +30,6 @@ class DetailsActivity: AppCompatActivity() {
button.setOnClickListener { button.setOnClickListener {
val dao = AppDatabaseHelper.getDatabase(this) val dao = AppDatabaseHelper.getDatabase(this)
.vehicleDAO() .vehicleDAO()
val res = dao
.find(id.toLong())
println(res)
try { try {
dao.insert(Vehicle( dao.insert(Vehicle(

View File

@ -11,10 +11,11 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.ca_kotlin.adapters.CountryAdapter import com.example.ca_kotlin.adapters.VehicleAdapter
import com.example.ca_kotlin.api.ApiClient import com.example.ca_kotlin.api.ApiClient
import com.example.ca_kotlin.api.Vehicles import com.example.ca_kotlin.api.Vehicles
import com.example.ca_kotlin.dao.AppDatabaseHelper import com.example.ca_kotlin.dao.AppDatabaseHelper
import com.example.ca_kotlin.dao.Vehicle
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
@ -45,7 +46,7 @@ class MainActivity : AppCompatActivity() {
progressDialog.setCancelable(false) progressDialog.setCancelable(false)
progressDialog.show() progressDialog.show()
val adapter = CountryAdapter(data) val adapter = VehicleAdapter(data)
recycler.adapter = adapter recycler.adapter = adapter
val switch = findViewById<Switch>(R.id.switch1) val switch = findViewById<Switch>(R.id.switch1)
@ -62,20 +63,23 @@ class MainActivity : AppCompatActivity() {
global(adapter, recycler, progressDialog) global(adapter, recycler, progressDialog)
} }
fun favorites(adapter: CountryAdapter, recycler: RecyclerView, progressDialog: ProgressDialog) { private fun favorites(adapter: VehicleAdapter, recycler: RecyclerView, progressDialog: ProgressDialog) {
val vehicles = AppDatabaseHelper.getDatabase(this) val vehicles = AppDatabaseHelper.getDatabase(this)
.vehicleDAO() .vehicleDAO()
.getVehicles() .getVehicles()
val filteredData = data.filter { dVehicle -> (vehicles.find { it.vehicleId == dVehicle.vehicleId }) != null }
data.clear() data.clear()
data.addAll(vehicles as ArrayList<Vehicles>) data.addAll(filteredData)
println(vehicles) println(filteredData)
adapter.update(data.clone() as ArrayList<Vehicles>)
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
fun global(adapter: CountryAdapter, recycler: RecyclerView, progressDialog: ProgressDialog) { private fun global(adapter: VehicleAdapter, recycler: RecyclerView, progressDialog: ProgressDialog) {
ApiClient.getClient.getVehicles().enqueue(object : Callback<List<Vehicles>> { ApiClient.getClient.getVehicles().enqueue(object : Callback<List<Vehicles>> {
override fun onResponse(call: Call<List<Vehicles>>?, response: Response<List<Vehicles>>?) { override fun onResponse(call: Call<List<Vehicles>>?, response: Response<List<Vehicles>>?) {
data.clear()
data.addAll(response!!.body()!!) data.addAll(response!!.body()!!)
adapter.update(data.clone() as ArrayList<Vehicles>) adapter.update(data.clone() as ArrayList<Vehicles>)
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()

View File

@ -13,7 +13,7 @@ import com.example.ca_kotlin.api.Vehicles
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
// Adapter for the CountrySelector // Adapter for the CountrySelector
class CountryAdapter(list: ArrayList<Vehicles>) : RecyclerView.Adapter<CountryAdapter.CountryViewHolder>() { class VehicleAdapter(list: ArrayList<Vehicles>) : RecyclerView.Adapter<VehicleAdapter.CountryViewHolder>() {
private var list: ArrayList<Vehicles> = ArrayList() private var list: ArrayList<Vehicles> = ArrayList()

View File

@ -32,9 +32,10 @@
<Switch <Switch
android:id="@+id/switch1" android:id="@+id/switch1"
android:layout_width="88dp" android:layout_width="wrap_content"
android:showText="false"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Favoris"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"

View File

@ -6,7 +6,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.2.0' classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong