Ajout de bouttons pour créer des éléments dans les listes Menu et Session

This commit is contained in:
biloute02 2024-01-06 11:25:31 +01:00
parent bff62daaec
commit a79b591e90
10 changed files with 76 additions and 63 deletions

View file

@ -15,19 +15,6 @@ import com.example.palto.domain.Session
class MenuAdapter : class MenuAdapter :
ListAdapter<Session, MenuAdapter.ViewHolder>(SessionDiffCallback) { ListAdapter<Session, MenuAdapter.ViewHolder>(SessionDiffCallback) {
/*
class ViewHolder(binding: FragmentSessionItemBinding) :
RecyclerView.ViewHolder(binding.root) {
val idView: TextView = binding.itemNumber
val contentView: TextView = binding.content
override fun toString(): String {
return super.toString() + " '" + contentView.text + "'"
}
}
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder( return ViewHolder(
FragmentMenuItemBinding.inflate( FragmentMenuItemBinding.inflate(
@ -40,22 +27,20 @@ class MenuAdapter :
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position) val item = getItem(position)
holder.idView.text = position.toString() holder.sessionId.text = item.id
holder.contentView.text = item.id
} }
//override fun getItemCount(): Int = values.size
inner class ViewHolder( inner class ViewHolder(
binding: FragmentMenuItemBinding binding: FragmentMenuItemBinding
) : RecyclerView.ViewHolder(binding.root) { ) : RecyclerView.ViewHolder(binding.root) {
val idView: TextView = binding.itemNumber val sessionId: TextView = binding.sessionId
val contentView: TextView = binding.content
override fun toString(): String { override fun toString(): String {
return super.toString() + " '" + idView.text + contentView.text + "'" return super.toString() + " '" + sessionId.text + "'"
} }
} }
//override fun getItemCount(): Int = values.size
} }
object SessionDiffCallback : DiffUtil.ItemCallback<Session>() { object SessionDiffCallback : DiffUtil.ItemCallback<Session>() {

View file

@ -1,21 +1,22 @@
package com.example.palto.ui.menu package com.example.palto.ui.menu
import android.os.Bundle import android.os.Bundle
import android.util.Log
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.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import com.example.palto.R
import com.example.palto.databinding.FragmentMenuListBinding import com.example.palto.databinding.FragmentMenuListBinding
import com.example.palto.databinding.FragmentSessionListBinding
/** /**
* A fragment representing a list of Sessions. * A fragment representing a list of Sessions.
*/ */
class MenuFragment : Fragment() { class MenuFragment : Fragment() {
private val menuViewModel: MenuViewModel by viewModels() private val menuViewModel: MenuViewModel by
navGraphViewModels(R.id.nav_graph)
private var _binding: FragmentMenuListBinding? = null private var _binding: FragmentMenuListBinding? = null
// This property is only valid between onCreateView and onDestroyView // This property is only valid between onCreateView and onDestroyView
@ -29,17 +30,16 @@ class MenuFragment : Fragment() {
_binding = FragmentMenuListBinding.inflate(inflater, container, false) _binding = FragmentMenuListBinding.inflate(inflater, container, false)
val adapter = MenuAdapter() val adapter = MenuAdapter()
binding.menuList.adapter = MenuAdapter() binding.menuList.adapter = adapter
menuViewModel.sessions.observe(viewLifecycleOwner) { menuViewModel.sessions.observe(viewLifecycleOwner) {
Log.d("PALTO", "A session has been created")
adapter.submitList(it) adapter.submitList(it)
} }
binding.createSession.setOnClickListener {
menuViewModel.createSession()
findNavController().navigate(R.id.action_menuFragment_to_sessionFragment)
}
return binding.root return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
menuViewModel.createSession()
}
} }

View file

@ -4,18 +4,18 @@ 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 com.example.palto.data.repository.LoginRepository
import com.example.palto.domain.Session import com.example.palto.domain.Session
class MenuViewModel() : ViewModel() { class MenuViewModel() : ViewModel() {
private var _sessions = MutableLiveData<List<Session>>(emptyList()) private var _sessions = MutableLiveData<List<Session>>(emptyList())
val sessions: LiveData<List<Session>> = _sessions val sessions = _sessions as LiveData<List<Session>>
fun createSession() { fun createSession() {
val session = Session( val session = Session(
id = "aahh" id = "aahh"
) )
_sessions.value = (_sessions.value ?: emptyList()) + session _sessions.value = (_sessions.value ?: emptyList()) + session
Log.d("PALTO", "MenuViewModel: a session has been added into the list.")
} }
} }

View file

@ -29,7 +29,6 @@ class SessionAdapter :
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position) val item = getItem(position)
holder.cardId.text = item.uid.toHexString() holder.cardId.text = item.uid.toHexString()
//holder.contentView.text = item.content
} }
inner class ViewHolder( inner class ViewHolder(

View file

@ -41,16 +41,19 @@ class SessionFragment : Fragment() {
val adapter = SessionAdapter() val adapter = SessionAdapter()
binding.sessionList.adapter = adapter binding.sessionList.adapter = adapter
sessionViewModel.cards.observe(viewLifecycleOwner) { sessionViewModel.cards.observe(viewLifecycleOwner) {
Log.d("NFC", "A card has been had to the list")
adapter.submitList(it) adapter.submitList(it)
} }
// Set the listener for a new NFC tag. // Set the listener for a new NFC tag.
nfcViewModel.tag.observe(viewLifecycleOwner) { nfcViewModel.tag.observe(viewLifecycleOwner) {
Log.d("NFC", "tag observer has been notified") Log.d("NFC", "The tag observers has been notified.")
sessionViewModel.insertCard(it) sessionViewModel.insertCard(it)
} }
binding.createCard.setOnClickListener {
//sessionViewModel.
}
return binding.root return binding.root
} }
} }

View file

@ -16,7 +16,7 @@ class SessionViewModel(
private val attendanceRepository: AttendanceRepository private val attendanceRepository: AttendanceRepository
) : ViewModel() { ) : ViewModel() {
private val _cards: MutableLiveData<List<Card>> = MutableLiveData(emptyList()) private val _cards = MutableLiveData<List<Card>>(emptyList())
val cards = _cards as LiveData<List<Card>> val cards = _cards as LiveData<List<Card>>
fun insertCard(tag: Tag) { fun insertCard(tag: Tag) {
@ -27,7 +27,7 @@ class SessionViewModel(
"tmp" "tmp"
) )
_cards.value = (_cards.value ?: emptyList()) + card _cards.value = (_cards.value ?: emptyList()) + card
Log.d("NFC", "view model: A card has been had to the list") Log.d("PALTO", "SessionViewModel: a card has been added into the list.")
} }
/** /**

View file

@ -5,16 +5,10 @@
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/item_number" 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/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout> </LinearLayout>

View file

@ -1,13 +1,28 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/menu_list"
android:name="com.example.palto.ui.menu.menuFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" <androidx.recyclerview.widget.RecyclerView
app:layoutManager="LinearLayoutManager" android:id="@+id/menu_list"
tools:context=".ui.menu.MenuFragment" android:name="com.example.palto.ui.menu.menuFragment"
tools:listitem="@layout/fragment_menu_item" /> android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.menu.MenuFragment"
tools:listitem="@layout/fragment_menu_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/create_session"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:clickable="true"
android:contentDescription="@string/create_session"
app:srcCompat="@android:drawable/ic_input_add" />
</FrameLayout>

View file

@ -1,13 +1,28 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/session_list"
android:name="com.example.palto.ui.session.sessionFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" <androidx.recyclerview.widget.RecyclerView
app:layoutManager="LinearLayoutManager" android:id="@+id/session_list"
tools:context=".ui.session.SessionFragment" android:name="com.example.palto.ui.session.sessionFragment"
tools:listitem="@layout/fragment_session_item" /> android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.session.SessionFragment"
tools:listitem="@layout/fragment_session_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/create_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:clickable="true"
android:contentDescription="@string/create_card"
app:srcCompat="@android:drawable/ic_input_add" />
</FrameLayout>

View file

@ -12,4 +12,6 @@
<string name="invalid_password">Mot de passe invalide</string> <string name="invalid_password">Mot de passe invalide</string>
<string name="login_failed">"Erreur de connexion !"</string> <string name="login_failed">"Erreur de connexion !"</string>
<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_card">Créer une présence</string>
</resources> </resources>