Complétion du fragment menu : afficher, créer et cliquer une fiche d’émargement.

This commit is contained in:
biloute02 2024-01-12 15:07:43 +01:00
parent 652b46f4c5
commit 035030ca0f
9 changed files with 138 additions and 48 deletions

View file

@ -5,5 +5,7 @@ import java.io.Serializable
* Data class that captures tokens for logged in users retrieved from LoginRepository * Data class that captures tokens for logged in users retrieved from LoginRepository
*/ */
data class Session( data class Session(
val id: String val id: Int,
val name: String,
var attendances: List<User>
) )

View file

@ -12,35 +12,39 @@ import com.example.palto.domain.Session
/** /**
* A [ListAdapter] that can display [Session] items. * A [ListAdapter] that can display [Session] items.
*/ */
class MenuAdapter : class MenuAdapter(private val onClick: (Session) -> Unit) :
ListAdapter<Session, MenuAdapter.ViewHolder>(SessionDiffCallback) { ListAdapter<Session, MenuAdapter.ViewHolder>(SessionDiffCallback) {
inner class ViewHolder(binding: FragmentMenuItemBinding) :
RecyclerView.ViewHolder(binding.root) {
private val sessionNameText: TextView = binding.sessionName
private var currentSession: Session? = null
init {
binding.root.setOnClickListener {
currentSession?.let {
onClick(it)
}
}
}
fun bind(session: Session) {
currentSession = session
sessionNameText.text = session.name
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder( val binding = FragmentMenuItemBinding
FragmentMenuItemBinding.inflate( .inflate(LayoutInflater.from(parent.context), parent, false)
LayoutInflater.from(parent.context), return ViewHolder(binding)
parent,
false
)
)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position) val item = getItem(position)
holder.sessionId.text = item.id holder.bind(item)
} }
inner class ViewHolder( override fun getItemCount() = currentList.size
binding: FragmentMenuItemBinding
) : RecyclerView.ViewHolder(binding.root) {
val sessionId: TextView = binding.sessionId
override fun toString(): String {
return super.toString() + " '" + sessionId.text + "'"
}
}
//override fun getItemCount(): Int = values.size
} }
object SessionDiffCallback : DiffUtil.ItemCallback<Session>() { object SessionDiffCallback : DiffUtil.ItemCallback<Session>() {

View file

@ -4,11 +4,15 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels import androidx.navigation.navGraphViewModels
import com.example.palto.R import com.example.palto.R
import com.example.palto.databinding.FragmentMenuListBinding import com.example.palto.databinding.FragmentMenuListBinding
import com.example.palto.domain.Session
import com.example.palto.ui.UserViewModel
/** /**
* A fragment representing a list of Sessions. * A fragment representing a list of Sessions.
@ -16,11 +20,13 @@ import com.example.palto.databinding.FragmentMenuListBinding
class MenuFragment : Fragment() { class MenuFragment : Fragment() {
private val menuViewModel: MenuViewModel by private val menuViewModel: MenuViewModel by
navGraphViewModels(R.id.nav_graph) navGraphViewModels(R.id.nav_graph) { MenuViewModel.Factory }
private val userViewModel: UserViewModel by
activityViewModels() { UserViewModel.Factory }
private var _binding: FragmentMenuListBinding? = null
// This property is only valid between onCreateView and onDestroyView // This property is only valid between onCreateView and onDestroyView
private val binding get() = _binding!! private lateinit var binding: FragmentMenuListBinding
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -41,17 +47,25 @@ class MenuFragment : Fragment() {
} }
// Display the list of sessions. // Display the list of sessions.
val adapter = MenuAdapter()
// Create a new MenuAdapter (list) with the given function when clicking an item.
val adapter = MenuAdapter { adapterOnClick(it) }
binding.menuList.adapter = adapter binding.menuList.adapter = adapter
// Link the adapter with the session list in the menuViewMode.
menuViewModel.sessions.observe(viewLifecycleOwner) { menuViewModel.sessions.observe(viewLifecycleOwner) {
adapter.submitList(it) adapter.submitList(it)
} }
// Bind the add button.
binding.createSession.setOnClickListener { binding.createSession.setOnClickListener {
menuViewModel.createSession() navController.navigate(R.id.action_menuFragment_to_newSessionFragment)
findNavController().navigate(R.id.action_menuFragment_to_sessionFragment)
} }
return binding.root return binding.root
} }
private fun adapterOnClick(session: Session) {
val bundle = bundleOf("session" to session.id)
findNavController().navigate(R.id.action_menuFragment_to_sessionFragment, bundle)
}
} }

View file

@ -4,18 +4,34 @@ import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.palto.data.repository.TokenRepository
import com.example.palto.data.repository.UserRepository
import com.example.palto.domain.Session import com.example.palto.domain.Session
import com.example.palto.ui.UserViewModel
class MenuViewModel() : ViewModel() { class MenuViewModel() : ViewModel() {
private var _sessions = MutableLiveData<List<Session>>(emptyList()) private var _sessions = MutableLiveData<List<Session>>(emptyList())
val sessions = _sessions as LiveData<List<Session>> val sessions: LiveData<List<Session>> = _sessions
fun createSession() { fun createSession(name: String) {
val list = _sessions.value ?: emptyList()
val session = Session( val session = Session(
id = "aahh" id = list.size,
name = name,
attendances = emptyList()
) )
_sessions.value = (_sessions.value ?: emptyList()) + session _sessions.value = list + session
Log.d("PALTO", "MenuViewModel: a session has been added into the list.") Log.d("Palto", "MenuViewModel: A session has been added into the list.")
}
companion object {
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MenuViewModel() as T
}
}
} }
} }

View file

@ -4,11 +4,17 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <!--<TextView
android:id="@+id/session_id" android:id="@+id/session_id"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin" android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" /> android:textAppearance="?attr/textAppearanceListItem" />-->
<TextView
android:id="@+id/session_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textAppearance="?attr/textAppearanceListItem"/>
</LinearLayout> </LinearLayout>

View file

@ -21,6 +21,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:clickable="true" android:clickable="true"
android:contentDescription="@string/create_session" android:contentDescription="@string/create_session"
app:srcCompat="@android:drawable/ic_input_add" /> app:srcCompat="@android:drawable/ic_input_add" />

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<EditText
android:id="@+id/new_session_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/new_session_name_hint"
android:inputType="text"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/new_session_create"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/new_session_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_session_button_create"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_session_name" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -4,27 +4,37 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph" android:id="@+id/nav_graph"
app:startDestination="@id/menuFragment"> app:startDestination="@id/menuFragment">
<fragment
android:id="@+id/loginFragment"
android:name="com.example.palto.ui.login.LoginFragment"
android:label="fragment._login"
tools:layout="@layout/fragment_login" />
<fragment
android:id="@+id/sessionFragment"
android:name="com.example.palto.ui.session.SessionFragment"
android:label="fragment_session_list"
tools:layout="@layout/fragment_session_list" />
<fragment <fragment
android:id="@+id/menuFragment" android:id="@+id/menuFragment"
android:name="com.example.palto.ui.menu.MenuFragment" android:name="com.example.palto.ui.menu.MenuFragment"
android:label="fragment_menu_list" android:label="Menu Palto"
tools:layout="@layout/fragment_menu_list" > tools:layout="@layout/fragment_menu_list" >
<action
android:id="@+id/action_menuFragment_to_loginFragment"
app:destination="@id/loginFragment" />
<action <action
android:id="@+id/action_menuFragment_to_sessionFragment" android:id="@+id/action_menuFragment_to_sessionFragment"
app:destination="@id/sessionFragment" /> app:destination="@id/sessionFragment" />
<action
android:id="@+id/action_menuFragment_to_newSessionFragment"
app:destination="@id/newSessionFragment" />
</fragment> </fragment>
<fragment
android:id="@+id/loginFragment"
android:name="com.example.palto.ui.login.LoginFragment"
android:label="Connexion"
tools:layout="@layout/fragment_login" />
<fragment
android:id="@+id/newSessionFragment"
android:name="com.example.palto.ui.menu.new_session.NewSessionFragment"
android:label="Nouvelle fiche"
tools:layout="@layout/fragment_new_session"/>
<fragment
android:id="@+id/sessionFragment"
android:name="com.example.palto.ui.session.SessionFragment"
android:label="Fiche de présence"
tools:layout="@layout/fragment_session_list" />
</navigation> </navigation>

View file

@ -15,4 +15,8 @@
<string name="help_message">Identifiants Invalides</string> <string name="help_message">Identifiants Invalides</string>
<string name="create_session">Créer une session</string> <string name="create_session">Créer une session</string>
<string name="create_card">Créer une présence</string> <string name="create_card">Créer une présence</string>
<string name="nouvelle_fiche">Nouvelle Fiche</string>
<string name="new_session_name_hint">Nom de la fiche</string>
<string name="new_session_button_create">Créer</string>
<string name="menu_item_logout">Déconnexion</string>
</resources> </resources>