deleting & displaying persisted data

master
Baptiiiiste 2 years ago
parent 4942de0bd5
commit 3552beba2f

@ -1,42 +1,45 @@
package uca.baptistearthur.geocaching.ui.fragment
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.data.Stub
import uca.baptistearthur.geocaching.application.RTApplication
import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTripEntity
import uca.baptistearthur.geocaching.recyclerview.RoadTripAdapter
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModel
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModelFactory
import java.util.*
class RoadTripFragment : Fragment() {
private var roadTripRecyclerView : RecyclerView? = null
private var model = Stub().load()
// private val roadTripViewModel: RoadTripViewModel by viewModels<RoadTripViewModel> {
// RoadTripViewModelFactory((MainWindow().application as RTApplication).db.roadTripDAO()) // MainWindow().application ????? bof bof
// }
private val roadTripViewModel: RoadTripViewModel by viewModels<RoadTripViewModel> {
RoadTripViewModelFactory((requireActivity().application as RTApplication).db.roadTripDAO())
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
roadTripViewModel.insertRoadTrip(RoadTripEntity(0, "Test", Date(),
listOf(Place(0.15, 45.2)) as MutableList<Place>
))
}
@SuppressLint("MissingInflatedId")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_roadtrip, container, false)
roadTripRecyclerView = view?.findViewById(R.id.recyclerViewRoadTripList)
roadTripRecyclerView?.adapter = RoadTripAdapter(model, findNavController())
roadTripViewModel.getAllRoadTrips().observe(viewLifecycleOwner, { roadTrips ->
roadTripRecyclerView?.adapter = RoadTripAdapter(roadTrips, findNavController())
})
roadTripRecyclerView?.layoutManager = LinearLayoutManager(context)
return view

@ -8,14 +8,21 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.application.RTApplication
import uca.baptistearthur.geocaching.converters.Converters
import uca.baptistearthur.geocaching.converters.toFrenchFormat
import uca.baptistearthur.geocaching.recyclerview.PlacesAdapter
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModel
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModelFactory
import java.time.format.DateTimeFormatter
import java.util.*
@ -23,8 +30,12 @@ class RoadtripDetail : Fragment() {
private var placesRecyclerView : RecyclerView? = null
private val roadTripViewModel: RoadTripViewModel by viewModels<RoadTripViewModel> {
RoadTripViewModelFactory((requireActivity().application as RTApplication).db.roadTripDAO())
}
@SuppressLint("ResourceType")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.d("GeoMap", "MAP ON CREATE VIEW")
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.roadtrip_detail, container, false)
@ -38,6 +49,11 @@ class RoadtripDetail : Fragment() {
view?.findViewById<TextView>(R.id.roadTripDetailTitle)?.text = roadTrip.name
view?.findViewById<TextView>(R.id.roadTripDetailDate)?.text = roadTrip.date.toFrenchFormat()
view?.findViewById<Button>(R.id.btnDeleteRoadTrip)?.setOnClickListener {
roadTripViewModel.deleteRoadTrip(roadTrip)
findNavController().popBackStack()
}
return view
}
}

@ -1,6 +1,7 @@
package uca.baptistearthur.geocaching.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
//import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
@ -9,9 +10,9 @@ import uca.baptistearthur.geocaching.model.RoadTripEntity
class RoadTripViewModel(val dao: RoadTripDAO): ViewModel() {
fun getRoadTripById(id: Int) = dao.getRoadTripById(id)//.asLiveData()
fun getRoadTripById(id: Int) = dao.getRoadTripById(id).asLiveData()
fun getAllRoadTrips() = dao.getAllRoadTrips()//.asLiveData()
fun getAllRoadTrips() = dao.getAllRoadTrips().asLiveData()
fun insertRoadTrip(r: RoadTripEntity){
viewModelScope.launch {

@ -27,4 +27,13 @@
android:name="uca.baptistearthur.geocaching.ui.fragment.MyLocationMap"
android:label="fragment_map"
tools:layout="@layout/fragment_map" />
<fragment android:id="@+id/placeholder" >
<action
android:id="@+id/action_placeholder_to_map2"
app:destination="@id/map2" />
</fragment>
<fragment
android:id="@+id/map2"
android:name="uca.baptistearthur.geocaching.ui.fragment.Map"
android:label="Map" />
</navigation>
Loading…
Cancel
Save