From fe58ff22da152227a8e7254f11a06c0ef59e5cfa Mon Sep 17 00:00:00 2001 From: Baptiiiiste Date: Mon, 27 Mar 2023 22:32:34 +0200 Subject: [PATCH] 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 --- .../geocaching/recyclerview/PlacesAdapter.kt | 23 +++++++ .../recyclerview/PlacesViewHolder.kt | 14 ++++ .../recyclerview/RoadTripViewHolder.kt | 15 ++++ .../geocaching/services/FragmentService.kt | 16 +++++ .../geocaching/ui/activity/MainWindow.kt | 21 ++---- .../ui/fragment/DetailledRoadTripFragment.kt | 68 +++++++++++++++++++ .../ui/fragment/RoadTripFragment.kt | 26 ++++--- app/src/main/res/layout/cell_place.xml | 31 +++++++++ .../layout/fragment_detailled_roadtrip.xml | 42 ++++++++++++ app/src/main/res/values/strings.xml | 2 + 10 files changed, 235 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesAdapter.kt create mode 100644 app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesViewHolder.kt create mode 100644 app/src/main/java/uca/baptistearthur/geocaching/services/FragmentService.kt create mode 100644 app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/DetailledRoadTripFragment.kt create mode 100644 app/src/main/res/layout/cell_place.xml create mode 100644 app/src/main/res/layout/fragment_detailled_roadtrip.xml diff --git a/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesAdapter.kt b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesAdapter.kt new file mode 100644 index 0000000..3e3b60f --- /dev/null +++ b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesAdapter.kt @@ -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) : RecyclerView.Adapter(){ + + 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 +} \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesViewHolder.kt b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesViewHolder.kt new file mode 100644 index 0000000..745cdb2 --- /dev/null +++ b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/PlacesViewHolder.kt @@ -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) + +} \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/RoadTripViewHolder.kt b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/RoadTripViewHolder.kt index d87236b..a6bd2b8 100644 --- a/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/RoadTripViewHolder.kt +++ b/app/src/main/java/uca/baptistearthur/geocaching/recyclerview/RoadTripViewHolder.kt @@ -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) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/services/FragmentService.kt b/app/src/main/java/uca/baptistearthur/geocaching/services/FragmentService.kt new file mode 100644 index 0000000..37359f6 --- /dev/null +++ b/app/src/main/java/uca/baptistearthur/geocaching/services/FragmentService.kt @@ -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() + } + +} \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/ui/activity/MainWindow.kt b/app/src/main/java/uca/baptistearthur/geocaching/ui/activity/MainWindow.kt index 7cc7524..b38938c 100644 --- a/app/src/main/java/uca/baptistearthur/geocaching/ui/activity/MainWindow.kt +++ b/app/src/main/java/uca/baptistearthur/geocaching/ui/activity/MainWindow.kt @@ -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(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() - } - } \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/DetailledRoadTripFragment.kt b/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/DetailledRoadTripFragment.kt new file mode 100644 index 0000000..e10fa9d --- /dev/null +++ b/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/DetailledRoadTripFragment.kt @@ -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) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/RoadTripFragment.kt b/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/RoadTripFragment.kt index 40fc0db..a3870a6 100644 --- a/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/RoadTripFragment.kt +++ b/app/src/main/java/uca/baptistearthur/geocaching/ui/fragment/RoadTripFragment.kt @@ -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 { diff --git a/app/src/main/res/layout/cell_place.xml b/app/src/main/res/layout/cell_place.xml new file mode 100644 index 0000000..b1f5495 --- /dev/null +++ b/app/src/main/res/layout/cell_place.xml @@ -0,0 +1,31 @@ + + + + + +