Moving loadFragment in a service, Creating the recyclerView for a roadtrip's places, Starting to display this recyclerView in RoadTripRecyclerView by getting the roadTrip by name

pull/6/head
Baptiiiiste 2 years ago
parent 02fd486bc1
commit fe58ff22da

@ -0,0 +1,23 @@
package uca.baptistearthur.geocaching.recyclerview
import android.annotation.SuppressLint
import android.view.LayoutInflater
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>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlacesViewHolder {
return PlacesViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cell_one_roadtrip, parent, false))
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: PlacesViewHolder, position: Int) {
holder.placeText.text = "> " + places[position].longitude + " - " + places[position].latitude
}
override fun getItemCount(): Int = places.size
}

@ -0,0 +1,14 @@
package uca.baptistearthur.geocaching.recyclerview
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import uca.baptistearthur.geocaching.R
class PlacesViewHolder(val cellule: View): ViewHolder(cellule) {
var placeDeleteButton: Button = cellule.findViewById(R.id.btnDeletePlace)
var placeText: TextView = cellule.findViewById(R.id.txtPlaceName)
}

@ -1,12 +1,27 @@
package uca.baptistearthur.geocaching.recyclerview
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.RoadTrip
import uca.baptistearthur.geocaching.services.FragmentService
import uca.baptistearthur.geocaching.ui.activity.MainWindow
import uca.baptistearthur.geocaching.ui.fragment.DetailledRoadTripFragment
class RoadTripViewHolder(val cellule: View): ViewHolder(cellule) {
var roadTripAccessButton: Button = cellule.findViewById(R.id.btnGetRoadTripsInfo)
init{
roadTripAccessButton.setOnClickListener{
val roadTripName = roadTripAccessButton.text.substring(3)
Log.d("RoadTripViewHolder", "RoadTripViewHolder clicked: ${roadTripName}")
// val roadTrip: RoadTrip = Find roadtrip by name here
// FragmentService().loadFragment(DetailledRoadTripFragment(roadTrip), (cellule.context as MainWindow).supportFragmentManager)
}
}
}

@ -0,0 +1,16 @@
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 {
fun loadFragment(fragment: Fragment, supportFragmentManager: FragmentManager){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
}

@ -2,15 +2,10 @@ package uca.baptistearthur.geocaching.ui.activity
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomnavigation.BottomNavigationView
import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.RoadTrip
import uca.baptistearthur.geocaching.recyclerview.RoadTripAdapter
import uca.baptistearthur.geocaching.services.FragmentService
import uca.baptistearthur.geocaching.ui.fragment.Map
import uca.baptistearthur.geocaching.ui.fragment.RoadTripFragment
@ -24,18 +19,20 @@ class MainWindow: AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_window)
val fragmentService = FragmentService()
// Bottom menu
loadFragment(Map())
fragmentService.loadFragment(Map(), supportFragmentManager)
val navigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
navigation.selectedItemId= R.id.map
navigation.setOnItemSelectedListener {
when (it.itemId) {
R.id.map -> {
loadFragment(map)
fragmentService.loadFragment(map, supportFragmentManager)
true
}
R.id.roadTrip -> {
loadFragment(RoadTripFragment())
fragmentService.loadFragment(RoadTripFragment(), supportFragmentManager)
true
}
else -> false
@ -43,10 +40,4 @@ class MainWindow: AppCompatActivity() {
}
}
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
}

@ -0,0 +1,68 @@
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)
}
}
}
}

@ -2,6 +2,7 @@ package uca.baptistearthur.geocaching.ui.fragment
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
@ -14,6 +15,8 @@ import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.data.Stub
import uca.baptistearthur.geocaching.model.RoadTrip
import uca.baptistearthur.geocaching.recyclerview.RoadTripAdapter
import uca.baptistearthur.geocaching.services.FragmentService
import uca.baptistearthur.geocaching.ui.activity.MainWindow
import java.util.*
// TODO: Rename parameter arguments, choose names that match
@ -44,7 +47,7 @@ class RoadTripFragment : Fragment() {
}
}
@SuppressLint("NotifyDataSetChanged")
@SuppressLint("MissingInflatedId")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_roadtrip, container, false)
@ -56,17 +59,24 @@ class RoadTripFragment : Fragment() {
buttonAddNewRoadTrip = view?.findViewById(R.id.buttonAddNewRoadTrip)
buttonAddNewRoadTrip?.setOnClickListener {
val roadTripName = editTextRoadTripName?.text.toString()
editTextRoadTripName?.text?.clear()
val roadTrip = RoadTrip(roadTripName, Date(), mutableListOf())
model.add(roadTrip)
roadTripRecyclerView?.adapter?.notifyDataSetChanged()
addRoadTrip(editTextRoadTripName!!, roadTripRecyclerView!!)
}
return view
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 {

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txtPlaceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="> TMP/ Place Name"
android:textColor="@color/black"
android:padding="5dp"
android:textSize="17sp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/btnDeletePlace"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X"
android:backgroundTint="@color/main_turquoise_200"
/>
</LinearLayout>

@ -0,0 +1,42 @@
<?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>

@ -5,6 +5,8 @@
<string name="roadtrip_title">Mes RoadTrips:</string>
<string name="add_roadtrip_button">+</string>
<string name="textaddNewRoadTrip">Entrez le nom du nouveau voyage</string>
<string name="placesList">Vos lieux à visiter:</string>
<string name="btnDeleteRoadTrip">Supprimer le voyage</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>

Loading…
Cancel
Save