correction crash car dans le thread principal

master
Bastien OLLIER 2 years ago
parent 76855f191f
commit 1f8e7f48e0

@ -11,6 +11,7 @@ import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.gson.JsonParser
@ -131,11 +132,8 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
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)
}
}
val restaurantViewModel = ViewModelProvider(this, RestaurantViewModelFactory((requireActivity().application as MappingApplication).repository)).get(RestaurantViewModel::class.java)
restaurantViewModel.insert(restaurant)
}
setSelectedIcon(it)
@ -150,9 +148,6 @@ class FirstFragment: Fragment(R.layout.fragment_map_page) {
}
}
private val restaurantViewModel: RestaurantViewModel by viewModels {
RestaurantViewModelFactory((requireActivity().application as MappingApplication).repository)
}
private fun toggleLayout() {
mapLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f)

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

Loading…
Cancel
Save