Ajout du fragment Master

master
LouisPerret 3 years ago
parent 3d496298fb
commit b8a5e6059a

@ -13,7 +13,7 @@
android:theme="@style/Theme.Ouaff"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".vues.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

@ -0,0 +1,8 @@
package fr.iut.ouaff.data.chargeur
import fr.iut.ouaff.modele.metier.Chien
interface Chargeur {
public fun charger(nomFichier : String) : MutableList<Chien>
}

@ -0,0 +1,17 @@
package fr.iut.ouaff.data.chargeur
import fr.iut.ouaff.modele.metier.Chien
import fr.iut.ouaff.modele.metier.Genre
class Stub : Chargeur {
override fun charger(nomFichier: String): MutableList<Chien> {
var listeChien: MutableList<Chien> = ArrayList()
listeChien.add(Chien("Louis", "Golden Retriever", Genre.Male, 40, 0))
listeChien.add(Chien("Elfe", "Dalmatien", Genre.Inconnu, 30, 1))
listeChien.add(Chien("Dexter", "Pitbull allemand", Genre.Male, 50, 3))
listeChien.add(Chien("Daisy", "Chihuahua", Genre.Femele, 20, 2))
listeChien.add(Chien("Deku", "Labrador", Genre.Femele, 35,1))
return listeChien
}
}

@ -0,0 +1,21 @@
package fr.iut.ouaff.modele.metier
import fr.iut.ouaff.data.chargeur.Stub
class Chenil {
var listeChiens: MutableList<Chien> = ArrayList()
get() = listeChiens
constructor(){
listeChiens = Stub().charger("")
}
public fun ajouterChien(chien: Chien){
listeChiens.add(chien)
}
public fun ajouterChien(nom: String, race: String, genre: Genre, poids: Int, agressivite: Int){
listeChiens.add(Chien(nom, race, genre, poids, agressivite))
}
}

@ -0,0 +1,39 @@
package fr.iut.ouaff.modele.metier
class Chien {
var nom:String
public get() = field
private set
var race:String
public get() = field
private set
var genre:Genre
public get() = field
private set
var poids:Int
public get() = field
private set(value) {
if(value > 0) field = value
}
var agressivite:Int
public get() = field
private set(value){
if(value >= 0 && value <= 3){
field = value
}
}
constructor(nom: String, race: String, genre: Genre, poids: Int, agressivite: Int) {
this.nom = nom
this.race = race
this.genre = genre
this.poids = poids
this.agressivite = agressivite
}
}

@ -0,0 +1,7 @@
package fr.iut.ouaff.modele.metier
enum class Genre {
Male,
Femele,
Inconnu
}

@ -1,17 +1,24 @@
package fr.iut.ouaff
package fr.iut.ouaff.vues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import fr.iut.ouaff.fragment.FragmentDetail
import fr.iut.ouaff.R
import fr.iut.ouaff.data.chargeur.Stub
import fr.iut.ouaff.modele.metier.Chien
import fr.iut.ouaff.vues.fragment.FragmentDetail
import fr.iut.ouaff.vues.fragment.FragmentMaster
class MainActivity : AppCompatActivity() {
private var listeChiens: MutableList<Chien> = Stub().charger("")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(supportFragmentManager.findFragmentById(R.id.id_fragment) == null){
supportFragmentManager.beginTransaction()
.add(R.id.id_fragment, FragmentDetail())
.add(R.id.id_fragment, FragmentMaster(listeChiens))
.commit()
}
}

@ -0,0 +1,57 @@
package fr.iut.ouaff.vues.adapter
import android.content.res.Resources
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import fr.iut.ouaff.R
import fr.iut.ouaff.modele.metier.Chien
/**
* Gère l'affichage de la liste de mes chiens -> équivalent de la ListView en javafx
*/
class AdapterChien(private var listeChiens:MutableList<Chien>): RecyclerView.Adapter<ViewHolder>() {
private lateinit var ressources: Resources;
/**
* Créer la ViewHolder qui va contenir un chien
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
ressources = parent.resources // pour pouvoir accéder au R. plus tard
var view = LayoutInflater.from(parent.context).inflate(R.layout.cellule_chien, parent, false) // je charge la vue xml qui contiendra mon chien
return ViewHolderChien(view) // je passe cette vue à ma ViewHolderChien
}
/**
* On ajoute les données du chien à afficher dans la ViewHolder créée
* holder -> représente ma ViewHolderChien
* position -> position du chien à afficher dans ma listeChien
*/
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val chienAAfficher = listeChiens.get(position) // on récupère le chien à afficher
if(holder is ViewHolderChien){ // si ma ViewHolder est bien une instance de ViewHolderChien
/// Je set le text de mes propriétés pour afficher ce que je veux afficher
holder.textNomChien.setText(chienAAfficher.nom) // afficher le nom
holder.textRaceChien.setText(chienAAfficher.race)
/// setter la couleur à afficher suivant son aggressivité
// when équivalent du switch case en Java ou en C#
var color = R.color.colorNice
when(chienAAfficher.agressivite){
1 -> color = R.color.colorNormal
2 -> color = R.color.colorBad
3 -> color = R.color.colorAggressive
}
holder.cardViewChien.setCardBackgroundColor(ressources.getColor(color))
}
}
/**
* Renvoie le nombre total d'élément de la liste à afficher
*/
override fun getItemCount(): Int = listeChiens.size
}

@ -0,0 +1,32 @@
package fr.iut.ouaff.vues.adapter
import android.view.View
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import fr.iut.ouaff.R
/**
* Affichera un de mes chiens -> équivalent des cellules en javafx
*/
class ViewHolderChien(itemView: View) : ViewHolder(itemView) {
var textNomChien: TextView
get() = field
private set
var textRaceChien: TextView
get() = field
private set
var cardViewChien: CardView
get()= field
private set
/// Initialise mes propriétés
init {
textNomChien = itemView.findViewById(R.id.nomChien)
textRaceChien = itemView.findViewById(R.id.raceChien)
cardViewChien = itemView.findViewById(R.id.cardViewChien)
}
}

@ -1,9 +1,10 @@
package fr.iut.ouaff.fragment
package fr.iut.ouaff.vues.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toolbar
import androidx.fragment.app.Fragment
import fr.iut.ouaff.R

@ -0,0 +1,28 @@
package fr.iut.ouaff.vues.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.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import fr.iut.ouaff.R
import fr.iut.ouaff.modele.metier.Chien
import fr.iut.ouaff.vues.adapter.AdapterChien
class FragmentMaster(private var listeChiens: MutableList<Chien>) : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_master, container);
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerViewChien);
//recyclerView.layoutManager = GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
recyclerView.adapter = AdapterChien(listeChiens)
return super.onCreateView(inflater, container, savedInstanceState)
}
}

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardViewChien">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nomChien"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/raceChien"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
android:orientation="vertical"
android:id="@+id/recyclerViewChien"/>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -21,3 +21,6 @@ kotlin.code.style=official
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
org.gradle.daemon=true
org.gradle.parallel=true
Loading…
Cancel
Save