add button like mais crash car dans le thread principal

master
Bastien OLLIER 2 years ago
parent b01e3afc52
commit 76855f191f

@ -5,10 +5,13 @@ import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast import android.widget.Toast
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.gson.JsonParser import com.google.gson.JsonParser
import com.mapbox.mapboxsdk.Mapbox import com.mapbox.mapboxsdk.Mapbox
@ -22,6 +25,10 @@ import com.mapbox.mapboxsdk.utils.BitmapUtils
import fr.iut.mapping.Model.RestaurantData import fr.iut.mapping.Model.RestaurantData
import fr.iut.mapping.API.getRestaurants import fr.iut.mapping.API.getRestaurants
import kotlinx.android.synthetic.main.fragment_map_page.* import kotlinx.android.synthetic.main.fragment_map_page.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class FirstFragment: Fragment(R.layout.fragment_map_page) { class FirstFragment: Fragment(R.layout.fragment_map_page) {
@ -37,6 +44,8 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
private lateinit var locationListener: fr.iut.mapping.LocationListener private lateinit var locationListener: fr.iut.mapping.LocationListener
private lateinit var listRestaurant: List<RestaurantData> private lateinit var listRestaurant: List<RestaurantData>
private lateinit var currentRestaurant: RestaurantData
private fun makeStyleUrl(): String { private fun makeStyleUrl(): String {
return getString(R.string.mapbox_style_url); return getString(R.string.mapbox_style_url);
} }
@ -52,7 +61,7 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
val button: FloatingActionButton = rootView.findViewById(R.id.findRestaurant) val button: FloatingActionButton = rootView.findViewById(R.id.findRestaurant)
button.setOnClickListener { button.setOnClickListener {
findAndPutRestaurant() findAndPutRestaurant(rootView)
} }
@ -68,7 +77,7 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
return rootView return rootView
} }
private fun findAndPutRestaurant(){ private fun findAndPutRestaurant(rootView: View){
if(locationListener.asPermission()){ if(locationListener.asPermission()){
Log.e("Debug","${locationListener.latitude},${locationListener.longitude}") Log.e("Debug","${locationListener.latitude},${locationListener.longitude}")
listRestaurant = emptyList() listRestaurant = emptyList()
@ -96,13 +105,38 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
this.symbolManager?.iconIgnorePlacement = true this.symbolManager?.iconIgnorePlacement = true
listRestaurant.forEach { restaurant -> listRestaurant.forEach { restaurant ->
val description = "${restaurant.adress}\n${restaurant.phone ?: "Pas de numéro disponible"}\n${restaurant.lat},${restaurant.lon}" restaurant.adress?.let {
insertIconOnMap(LatLng(restaurant.lat, restaurant.lon), restaurant.name, description) insertIconOnMap(LatLng(restaurant.lat, restaurant.lon), restaurant.name,
it,restaurant.phone ?: "Pas de numéro disponible")
}
} }
this.symbolManager?.addClickListener { this.symbolManager?.addClickListener {
titleView.text = it.data?.asJsonObject?.get("title")?.asString val clickedData = it.data?.asJsonObject
descriptionView.text = it.data?.asJsonObject?.get("description")?.asString titleView.text = clickedData?.get("title")?.asString
addressRestaurant.text = clickedData?.get("address")?.asString
telephoneRestaurant.text = clickedData?.get("phone")?.asString
GPSRestaurant.text = clickedData?.get("GPS")?.asString
val button: Button = rootView.findViewById(R.id.addToFav)
button.setOnClickListener {
Log.d("debug button","click")
Log.d("debug button",clickedData.toString())
val gps = (clickedData?.get("GPS")?.asString ?: "").split(",")
val lat = gps[0]
val lon = gps[1]
val name = clickedData?.get("title")?.asString ?: ""
val address = clickedData?.get("address")?.asString ?: ""
val phone = clickedData?.get("phone")?.asString ?: ""
val restaurant = RestaurantData(lat.toDouble(), lon.toDouble(), name,address,phone)
viewLifecycleOwner.lifecycleScope.launch {
withContext(Dispatchers.IO) {
restaurantViewModel.insert(restaurant)
}
}
}
setSelectedIcon(it) setSelectedIcon(it)
toggleLayout() toggleLayout()
@ -116,6 +150,10 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
} }
} }
private val restaurantViewModel: RestaurantViewModel by viewModels {
RestaurantViewModelFactory((requireActivity().application as MappingApplication).repository)
}
private fun toggleLayout() { private fun toggleLayout() {
mapLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f) mapLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f)
descriptionLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f) descriptionLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f)
@ -138,11 +176,16 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
this.lastSymbol = symbol this.lastSymbol = symbol
} }
private fun insertIconOnMap(point: LatLng, title: String, description: String) { private fun insertIconOnMap(point: LatLng, title: String, address: String,phone: String) {
val lat = point.latitude
val lon = point.longitude
val jsonData = """ val jsonData = """
{ {
"title" : "$title", "title" : "$title",
"description" : "$description" "address" : "$address",
"phone" : "$phone",
"GPS": "$lat,$lon"
} }
""" """

@ -1,6 +1,7 @@
package fr.iut.mapping package fr.iut.mapping
import androidx.lifecycle.* import androidx.lifecycle.*
import fr.iut.mapping.Model.RestaurantData
import fr.iut.mapping.database.Entity.RestaurantEntity import fr.iut.mapping.database.Entity.RestaurantEntity
import fr.iut.mapping.database.RestaurantRepository import fr.iut.mapping.database.RestaurantRepository
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -8,8 +9,9 @@ import kotlinx.coroutines.launch
class RestaurantViewModel(private val repository: RestaurantRepository): ViewModel() { class RestaurantViewModel(private val repository: RestaurantRepository): ViewModel() {
val restaurantLikes: LiveData<List<RestaurantEntity>> = repository.restaurantLikes.asLiveData() val restaurantLikes: LiveData<List<RestaurantEntity>> = repository.restaurantLikes.asLiveData()
fun insert(restaurant: RestaurantEntity) = viewModelScope.launch { fun insert(restaurant: RestaurantData) = viewModelScope.launch {
repository.insert(restaurant) val resto = RestaurantEntity(0,restaurant.lat,restaurant.lon,restaurant.name,restaurant.phone,restaurant.adress)
repository.insert(resto)
} }
} }

@ -55,31 +55,62 @@
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="0" android:layout_weight="0"
android:orientation="vertical" android:orientation="vertical"
android:weightSum="3"> android:weightSum="5">
<TextView <TextView
android:id="@+id/titleView" android:id="@+id/titleView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:gravity="center" android:gravity="center"
android:padding="10dp" android:padding="0dp"
android:text=" " android:text=" "
android:textAlignment="center" android:textAlignment="center"
android:textSize="30dp" /> android:textSize="30dp" />
<TextView <TextView
android:id="@+id/descriptionView" android:id="@+id/addressRestaurant"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="wrap_content"
android:layout_weight="2" android:layout_weight="1"
android:gravity="center"
android:padding="0dp"
android:text=" "
android:textAlignment="center"
android:textSize="15dp" />
<TextView
android:id="@+id/telephoneRestaurant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" android:gravity="center"
android:padding="10dp" android:padding="0dp"
android:text=" " android:text=" "
android:textAlignment="center" android:textAlignment="center"
android:textSize="15dp" /> android:textSize="15dp" />
<TextView
android:id="@+id/GPSRestaurant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="0dp"
android:text=" "
android:textAlignment="center"
android:textSize="15dp" />
<Button
android:id="@+id/addToFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="like"
android:layout_gravity="center_horizontal" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

Loading…
Cancel
Save