baptiste2 #7

Merged
baptiste.bonneau merged 4 commits from baptiste2 into master 2 years ago

@ -14,7 +14,6 @@ android {
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
@ -33,7 +32,17 @@ android {
}
}
apply plugin: 'kotlin-kapt'
dependencies {
kapt "androidx.room:room-compiler:2.5.1"
annotationProcessor "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-runtime:2.5.1"
implementation "androidx.room:room-ktx:2.5.1"
// implementation 'androidx.lifecycle:lifecycle-livedata:2.6.1'
// implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
// implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
@ -47,5 +56,6 @@ dependencies {
implementation "androidx.fragment:fragment-ktx:1.5.5"
implementation 'org.osmdroid:osmdroid-android:6.1.14'
implementation 'com.github.MKergall:osmbonuspack:6.9.0'
implementation 'com.android.support:multidex:1.0.3'
}

@ -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,28 +1,32 @@
package uca.baptistearthur.geocaching.data
import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTrip
import uca.baptistearthur.geocaching.model.RoadTripEntity
import java.util.Date
class Stub {
fun load(): MutableList<RoadTrip> {
fun load(): MutableList<RoadTripEntity> {
val list = listOf(
RoadTrip(
RoadTripEntity(
1,
"France",
Date(),
listOf(Place(49.3, 49.3)).toMutableList()
),
RoadTrip(
RoadTripEntity(
2,
"Italie",
Date(),
listOf(Place(48.866667, 2.333333), Place(48.866667, 2.333333)).toMutableList()
),
RoadTrip(
RoadTripEntity(
3,
"Danemark",
Date(),
listOf(Place(48.866667, 2.333333), Place(48.866667, 2.333333)).toMutableList()
),
RoadTrip(
RoadTripEntity(
4,
"Islande",
Date(),
listOf(Place(48.866667, 2.333333), Place(48.866667, 2.333333), Place(48.866667, 2.333333)).toMutableList()

@ -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))
}

@ -6,7 +6,6 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTrip
class PlacesAdapter (val places: List<Place>) : RecyclerView.Adapter<PlacesViewHolder>(){

@ -6,10 +6,10 @@ import android.view.ViewGroup
import androidx.navigation.NavController
import androidx.recyclerview.widget.RecyclerView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.RoadTrip
import uca.baptistearthur.geocaching.model.RoadTripEntity
class RoadTripAdapter(val voyages: List<RoadTrip>, val navController: NavController) : RecyclerView.Adapter<RoadTripViewHolder>(){
class RoadTripAdapter(val voyages: List<RoadTripEntity>, val navController: NavController) : RecyclerView.Adapter<RoadTripViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RoadTripViewHolder {
return RoadTripViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cell_one_roadtrip, parent, false), navController)

@ -3,7 +3,6 @@ package uca.baptistearthur.geocaching.services
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.ui.fragment.DetailledRoadTripFragment
class FragmentService {

@ -2,22 +2,28 @@ package uca.baptistearthur.geocaching.ui.activity
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.services.FragmentService
import uca.baptistearthur.geocaching.application.RTApplication
import uca.baptistearthur.geocaching.ui.fragment.Map
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModel
import uca.baptistearthur.geocaching.viewModels.RoadTripViewModelFactory
class MainWindow: AppCompatActivity() {
// private val roadTripViewModel: RoadTripViewModel by viewModels<RoadTripViewModel> {
// RoadTripViewModelFactory((this.application as RTApplication).db.roadTripDAO())
// }
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
val map = Map()
super.onCreate(savedInstanceState)
setContentView(R.layout.main_window)
@ -34,7 +40,6 @@ class MainWindow: AppCompatActivity() {
true
}
}
}

@ -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)
}
}
}
}

@ -13,7 +13,7 @@ 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.model.RoadTrip
import uca.baptistearthur.geocaching.model.RoadTripEntity
import uca.baptistearthur.geocaching.recyclerview.RoadTripAdapter
import java.util.*
@ -24,7 +24,7 @@ private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [RoadTrip.newInstance] factory method to
* Use the [RoadTripEntity.newInstance] factory method to
* create an instance of this fragment.
*/
class RoadTripFragment : Fragment() {
@ -33,8 +33,6 @@ class RoadTripFragment : Fragment() {
private var param2: String? = null
private var model = Stub().load()
private var roadTripRecyclerView : RecyclerView? = null
private var editTextRoadTripName: EditText? = null
private var buttonAddNewRoadTrip: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -49,36 +47,13 @@ class RoadTripFragment : Fragment() {
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())
roadTripRecyclerView?.layoutManager = LinearLayoutManager(context)
editTextRoadTripName = view?.findViewById(R.id.editTextRoadTripName)
buttonAddNewRoadTrip = view?.findViewById(R.id.buttonAddNewRoadTrip)
buttonAddNewRoadTrip?.setOnClickListener {
addRoadTrip(editTextRoadTripName!!, roadTripRecyclerView!!)
}
return view
}
@SuppressLint("NotifyDataSetChanged")
fun addRoadTrip(editText: EditText, recyclerView: RecyclerView) {
val roadTripName = editText.text.toString().trim()
if(roadTripName.isNotEmpty() && roadTripName.length <= 20){
editText.text?.clear()
val roadTrip = RoadTrip(roadTripName, Date(), mutableListOf())
model.add(roadTrip)
recyclerView.adapter?.notifyDataSetChanged()
}else{
editText.error = "Le nom du voyage doit être compris entre 1 et 20 caractères."
}
}
companion object {
/**
* Use this factory method to create a new instance of

@ -19,6 +19,8 @@ import android.location.LocationListener
import android.util.Log
import android.widget.ProgressBar
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.osmdroid.bonuspack.routing.OSRMRoadManager
import org.osmdroid.bonuspack.routing.RoadManager
import org.osmdroid.config.IConfigurationProvider
@ -30,19 +32,26 @@ import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.recyclerview.PlacesAdapter
import uca.baptistearthur.geocaching.ui.overlay.AddMarkerOverlay
import uca.baptistearthur.geocaching.ui.overlay.RecenterOverlay
class RoadtripDetail : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
private var placesRecyclerView : RecyclerView? = null
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)
// val roadTrip = // récupérer le roadtrip cliqué
// placesRecyclerView = view?.findViewById(R.id.recyclerViewPlacesList)
// placesRecyclerView?.adapter = PlacesAdapter(roadTrip.places)
// placesRecyclerView?.layoutManager = LinearLayoutManager(context)
return view
}
}

@ -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>

@ -15,30 +15,6 @@
android:padding="10dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/editTextRoadTripName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/textaddNewRoadTrip"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonAddNewRoadTrip"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="@string/add_roadtrip_button"
android:layout_gravity="center"
android:backgroundTint="@color/main_turquoise_200"
/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewRoadTripList"
android:layout_width="match_parent"

@ -1,15 +1,63 @@
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.RoadtripDetail">
tools:context=".ui.fragment.RoadtripDetail"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:id="@+id/roadTripDetailTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="DETAIL"/>
android:text="TMP/ Roadtrip Name"
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…
Cancel
Save