@ -0,0 +1,24 @@
|
|||||||
|
package uca.baptistearthur.geocaching.application
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import uca.baptistearthur.geocaching.data.Database
|
||||||
|
|
||||||
|
class RTApplication: Application() {
|
||||||
|
|
||||||
|
// val db: Database by lazy {
|
||||||
|
// Database.getInstance(this)
|
||||||
|
// }
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// lateinit var db: Database
|
||||||
|
//
|
||||||
|
// override fun onCreate() {
|
||||||
|
// super.onCreate()
|
||||||
|
//
|
||||||
|
// // Initialiser la propriété db ici
|
||||||
|
// db = Database.getInstance(this) as Database
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package uca.baptistearthur.geocaching.converters
|
||||||
|
|
||||||
|
import androidx.room.TypeConverter
|
||||||
|
import java.util.Date
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import uca.baptistearthur.geocaching.model.Place
|
||||||
|
|
||||||
|
class Converters {
|
||||||
|
@TypeConverter
|
||||||
|
fun toDate(timestamp: Long?): Date? {
|
||||||
|
return if (timestamp == null) null else Date(timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toLong(date: Date?): Long? {
|
||||||
|
return date?.time
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toString(places: List<Place>): String {
|
||||||
|
return Gson().toJson(places)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toPlaces(value: String): List<Place> {
|
||||||
|
val listType = object : TypeToken<List<Place>>() {}.type
|
||||||
|
return Gson().fromJson(value, listType)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package uca.baptistearthur.geocaching.data
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import androidx.room.TypeConverters
|
||||||
|
import uca.baptistearthur.geocaching.converters.Converters
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
@androidx.room.Database(entities = arrayOf(RoadTripEntity::class), version=1)
|
||||||
|
@TypeConverters(Converters::class)
|
||||||
|
abstract class Database : RoomDatabase(){
|
||||||
|
|
||||||
|
abstract fun roadTripDAO(): RoadTripDAO
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
private var INSTANCE: Database ?= null
|
||||||
|
|
||||||
|
fun getInstance(context: Context) =
|
||||||
|
INSTANCE ?: synchronized(this){
|
||||||
|
val db = Room.databaseBuilder(context, Database::class.java, "roadTripDB").build()
|
||||||
|
INSTANCE = db
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package uca.baptistearthur.geocaching.data
|
||||||
|
|
||||||
|
import androidx.annotation.RequiresPermission.Read
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
@androidx.room.Dao
|
||||||
|
interface RoadTripDAO {
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insertRoadTrip(r: RoadTripEntity)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun deleteRoadTrip(r: RoadTripEntity)
|
||||||
|
|
||||||
|
@Query("SELECT * FROM Roadtrip")
|
||||||
|
fun getAllRoadTrips(): Flow<MutableList<RoadTripEntity>>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM Roadtrip WHERE id = :id")
|
||||||
|
fun getRoadTripById(id: Int): Flow<RoadTripEntity>
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
package uca.baptistearthur.geocaching.model
|
|
||||||
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
class RoadTrip(
|
|
||||||
val name: String,
|
|
||||||
val date: Date,
|
|
||||||
val places: MutableList<Place>
|
|
||||||
){
|
|
||||||
|
|
||||||
fun addPlaceToRoadTripList(place: Place) = places.add(place)
|
|
||||||
fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(Place(latitude, longitude))
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package uca.baptistearthur.geocaching.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
@Entity(tableName = "Roadtrip")
|
||||||
|
class RoadTripEntity(
|
||||||
|
@PrimaryKey(autoGenerate = true) val id: Int,
|
||||||
|
@ColumnInfo(name="name") val name: String,
|
||||||
|
@ColumnInfo(name="date") val date: Date,
|
||||||
|
@ColumnInfo(name="places") val places: MutableList<Place>
|
||||||
|
){
|
||||||
|
|
||||||
|
fun addPlaceToRoadTripList(place: Place) = places.add(place)
|
||||||
|
fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(Place(latitude, longitude))
|
||||||
|
|
||||||
|
}
|
@ -1,68 +0,0 @@
|
|||||||
package uca.baptistearthur.geocaching.ui.fragment
|
|
||||||
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import uca.baptistearthur.geocaching.R
|
|
||||||
import uca.baptistearthur.geocaching.model.RoadTrip
|
|
||||||
import uca.baptistearthur.geocaching.recyclerview.PlacesAdapter
|
|
||||||
import uca.baptistearthur.geocaching.recyclerview.RoadTripAdapter
|
|
||||||
|
|
||||||
// TODO: Rename parameter arguments, choose names that match
|
|
||||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
||||||
private const val ARG_PARAM1 = "param1"
|
|
||||||
private const val ARG_PARAM2 = "param2"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple [Fragment] subclass.
|
|
||||||
* Use the [RoadTrip.newInstance] factory method to
|
|
||||||
* create an instance of this fragment.
|
|
||||||
*/
|
|
||||||
class DetailledRoadTripFragment(val roadTrip: RoadTrip): Fragment() {
|
|
||||||
// TODO: Rename and change types of parameters
|
|
||||||
private var param1: String? = null
|
|
||||||
private var param2: String? = null
|
|
||||||
private var placesRecyclerView : RecyclerView? = null
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
arguments?.let {
|
|
||||||
param1 = it.getString(ARG_PARAM1)
|
|
||||||
param2 = it.getString(ARG_PARAM2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
||||||
val view = inflater.inflate(R.layout.fragment_detailled_roadtrip, container, false)
|
|
||||||
|
|
||||||
placesRecyclerView = view?.findViewById(R.id.recyclerViewPlacesList)
|
|
||||||
placesRecyclerView?.adapter = PlacesAdapter(roadTrip.places)
|
|
||||||
placesRecyclerView?.layoutManager = LinearLayoutManager(context)
|
|
||||||
|
|
||||||
return view
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
/**
|
|
||||||
* Use this factory method to create a new instance of
|
|
||||||
* this fragment using the provided parameters.
|
|
||||||
*
|
|
||||||
* @param param1 Parameter 1.
|
|
||||||
* @param param2 Parameter 2.
|
|
||||||
* @return A new instance of fragment List.
|
|
||||||
*/
|
|
||||||
// TODO: Rename and change types and number of parameters
|
|
||||||
@JvmStatic
|
|
||||||
fun newInstance(param1: String, param2: String) =
|
|
||||||
RoadTripFragment().apply {
|
|
||||||
arguments = Bundle().apply {
|
|
||||||
putString(ARG_PARAM1, param1)
|
|
||||||
putString(ARG_PARAM2, param2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,27 @@
|
|||||||
|
package uca.baptistearthur.geocaching.viewModels
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import uca.baptistearthur.geocaching.data.RoadTripDAO
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
class RoadTripViewModel(val dao: RoadTripDAO): ViewModel() {
|
||||||
|
|
||||||
|
fun getRoadTripById(id: Int) = dao.getRoadTripById(id) // .asLiveData() // ne marche pas -> impossible d'importer
|
||||||
|
|
||||||
|
fun getAllRoadTrips() = dao.getAllRoadTrips() // .asLiveData()
|
||||||
|
|
||||||
|
fun insertRoadTrip(r: RoadTripEntity){
|
||||||
|
viewModelScope.launch {
|
||||||
|
dao.insertRoadTrip(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteRoadTrip(r: RoadTripEntity){
|
||||||
|
viewModelScope.launch {
|
||||||
|
dao.deleteRoadTrip(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package uca.baptistearthur.geocaching.viewModels
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import uca.baptistearthur.geocaching.data.RoadTripDAO
|
||||||
|
|
||||||
|
class RoadTripViewModelFactory(private val dao: RoadTripDAO): ViewModelProvider.Factory{
|
||||||
|
|
||||||
|
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||||
|
if (modelClass.isAssignableFrom(RoadTripViewModel::class.java)){
|
||||||
|
return RoadTripViewModel(dao) as T
|
||||||
|
}
|
||||||
|
throw IllegalArgumentException("Unknown ViewModel class")
|
||||||
|
}
|
||||||
|
}
|
@ -1,42 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
tools:context=".ui.fragment.DetailledRoadTripFragment"
|
|
||||||
android:orientation="vertical">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="TMP/ Roadtrip Name"
|
|
||||||
android:background="@color/main_turquoise_200"
|
|
||||||
android:textColor="@color/main_turquoise_50"
|
|
||||||
android:padding="10dp"
|
|
||||||
android:textSize="20sp"/>
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="@string/placesList"
|
|
||||||
android:textColor="@color/main_turquoise_200"
|
|
||||||
android:padding="5dp"
|
|
||||||
android:textSize="17sp"/>
|
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
|
||||||
android:id="@+id/recyclerViewPlacesList"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_weight="1"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/btnDeleteRoadTrip"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="@string/btnDeleteRoadTrip"
|
|
||||||
android:backgroundTint="@color/main_turquoise_500"
|
|
||||||
android:layout_margin="10dp"/>
|
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
@ -1,15 +1,63 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".ui.fragment.RoadtripDetail">
|
tools:context=".ui.fragment.RoadtripDetail"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:id="@+id/roadTripDetailTitle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center"
|
android:text="TMP/ Roadtrip Name"
|
||||||
android:text="DETAIL"/>
|
android:background="@color/main_turquoise_200"
|
||||||
|
android:textColor="@color/main_turquoise_50"
|
||||||
|
android:paddingTop="10dp"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:textSize="20sp"/>
|
||||||
|
|
||||||
</FrameLayout>
|
<TextView
|
||||||
|
android:id="@+id/roadTripDetailDate"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="TMP/ Date"
|
||||||
|
android:background="@color/main_turquoise_200"
|
||||||
|
android:textColor="@color/main_turquoise_50"
|
||||||
|
android:textSize="15sp"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:paddingBottom="5dp"/>
|
||||||
|
|
||||||
|
<org.osmdroid.views.MapView
|
||||||
|
android:id="@+id/roadTripDetailMapView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true"
|
||||||
|
android:layout_weight="1"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/placesList"
|
||||||
|
android:textColor="@color/main_turquoise_200"
|
||||||
|
android:padding="5dp"
|
||||||
|
android:textSize="17sp"/>
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/recyclerViewPlacesList"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnDeleteRoadTrip"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/btnDeleteRoadTrip"
|
||||||
|
android:backgroundTint="@color/main_turquoise_500"
|
||||||
|
android:layout_margin="10dp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in new issue