parent
2aa3d6c49c
commit
b01e3afc52
@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut.mapping
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Entity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.viewModels
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import fr.iut.mapping.Model.RestaurantData
|
||||||
|
import fr.iut.mapping.database.DAO.RestaurantDAO
|
||||||
|
import fr.iut.mapping.database.Entity.RestaurantEntity
|
||||||
|
|
||||||
|
class SecondFragment: Fragment(R.layout.fragment_likes_page) {
|
||||||
|
|
||||||
|
private val restaurantViewModel: RestaurantViewModel by viewModels {
|
||||||
|
RestaurantViewModelFactory((requireActivity().application as MappingApplication).repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||||
|
val rootView = inflater.inflate(R.layout.fragment_likes_page, container, false)
|
||||||
|
|
||||||
|
val recyclerView = rootView.findViewById<RecyclerView>(R.id.recyclerview)
|
||||||
|
val adapter = RestaurantAdapter()
|
||||||
|
recyclerView.adapter = adapter
|
||||||
|
recyclerView.layoutManager = LinearLayoutManager(requireContext())
|
||||||
|
|
||||||
|
|
||||||
|
restaurantViewModel.restaurantLikes.observe(viewLifecycleOwner) { restaurantEntities ->
|
||||||
|
val restaurantDataList: MutableList<RestaurantData?> =
|
||||||
|
restaurantEntities.map { entity ->
|
||||||
|
RestaurantData(entity.lat, entity.lon, entity.name, entity.phone, entity.adress)
|
||||||
|
}.toMutableList()
|
||||||
|
Log.d("debug1", restaurantDataList.toString())
|
||||||
|
adapter.submitList(restaurantDataList)
|
||||||
|
Log.d("debug2", restaurantDataList.toString())
|
||||||
|
}
|
||||||
|
return rootView
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +0,0 @@
|
|||||||
package fr.iut.mapping
|
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
|
|
||||||
class SecondFragment:Fragment(R.layout.fragment_likes_page) {
|
|
||||||
}
|
|
@ -0,0 +1,16 @@
|
|||||||
|
package fr.iut.mapping
|
||||||
|
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import fr.iut.mapping.database.RestaurantDatabase
|
||||||
|
import fr.iut.mapping.database.RestaurantRepository
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
|
||||||
|
class MappingApplication: Application() {
|
||||||
|
val applicationScope = CoroutineScope(SupervisorJob())
|
||||||
|
|
||||||
|
val database by lazy { RestaurantDatabase.getDatabase(this,applicationScope) }
|
||||||
|
val repository by lazy { RestaurantRepository(database.restaurantDAO()) }
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package fr.iut.mapping
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import androidx.recyclerview.widget.ListAdapter
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import fr.iut.mapping.Model.RestaurantData
|
||||||
|
|
||||||
|
class RestaurantAdapter : ListAdapter<RestaurantData, RestaurantAdapter.RestaurantViewHolder>(RESTAURANT_COMPARATOR) {
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RestaurantViewHolder {
|
||||||
|
Log.d("debug","onCreateViewHolder")
|
||||||
|
return RestaurantViewHolder.create(parent)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: RestaurantViewHolder, position: Int) {
|
||||||
|
Log.d("debug","onBindViewHolder")
|
||||||
|
val current = getItem(position)
|
||||||
|
holder.bind(current.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int {
|
||||||
|
Log.d("debug","getItemCount")
|
||||||
|
Log.d("debug",currentList.toString())
|
||||||
|
|
||||||
|
return currentList.size
|
||||||
|
}
|
||||||
|
|
||||||
|
class RestaurantViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||||
|
private val restaurantItemView: TextView = itemView.findViewById(R.id.nameRestaurant)
|
||||||
|
|
||||||
|
fun bind(text: String?) {
|
||||||
|
Log.d("debug","bind")
|
||||||
|
|
||||||
|
restaurantItemView.text = text
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(parent: ViewGroup): RestaurantViewHolder {
|
||||||
|
Log.d("debug","create")
|
||||||
|
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.recyclerviewitem, parent, false)
|
||||||
|
return RestaurantViewHolder(view)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val RESTAURANT_COMPARATOR = object : DiffUtil.ItemCallback<RestaurantData>() {
|
||||||
|
override fun areItemsTheSame(oldItem: RestaurantData, newItem: RestaurantData): Boolean {
|
||||||
|
Log.d("debug","areItemsTheSame")
|
||||||
|
|
||||||
|
return oldItem === newItem
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: RestaurantData, newItem: RestaurantData): Boolean {
|
||||||
|
Log.d("debug","areContentsTheSame")
|
||||||
|
|
||||||
|
return oldItem.name == newItem.name &&
|
||||||
|
oldItem.lat == newItem.lat &&
|
||||||
|
oldItem.lon == newItem.lon &&
|
||||||
|
oldItem.adress == newItem.adress &&
|
||||||
|
oldItem.phone == newItem.phone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/nameRestaurant"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@android:color/holo_orange_light" />
|
||||||
|
</LinearLayout>
|
Loading…
Reference in new issue