Add Attestations entity, Dao, repository, viewModelFactory
This commit is contained in:
parent
6903a584fc
commit
02b3c53a0a
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
@ -14,6 +15,7 @@ import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
abstract class MyDatabase private constructor() : RoomDatabase() {
|
||||
|
||||
abstract fun profileDao(): ProfileDao
|
||||
abstract fun attestationDao(): AttestationDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
|
@ -0,0 +1,20 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.dao
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.CURRENT_ATTESTATION_ID
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.CURRENT_PROFILE_ID
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
@Dao
|
||||
interface AttestationDao {
|
||||
@Insert
|
||||
fun insert(attestation: Attestation)
|
||||
|
||||
@Query(value = "SELECT * FROM attestation WHERE id = $CURRENT_ATTESTATION_ID")
|
||||
fun getAttestation(): LiveData<Attestation>
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import java.lang.StringBuilder
|
||||
|
||||
const val CURRENT_ATTESTATION_ID = 0
|
||||
|
||||
@Entity(tableName = "attestation")
|
||||
data class Attestation(
|
||||
val profile: Profile,
|
||||
val exitDate: String,
|
||||
val exitHour: String,
|
||||
val createAt: String,
|
||||
val reasons: List<Int>
|
||||
) {
|
||||
@PrimaryKey
|
||||
var id: Int = CURRENT_ATTESTATION_ID
|
||||
|
||||
override fun toString(): String {
|
||||
val motifs = StringBuilder()
|
||||
|
||||
repeat(reasons.size) { motifs.append(getMotifText(it), "-") }.also { motifs.dropLast(1) }
|
||||
return "Cree le: $createAt; $profile; Sortie: $exitDate a $exitHour; Motifs: $motifs"
|
||||
}
|
||||
|
||||
private fun getMotifText(i: Int): String {
|
||||
return when (i) {
|
||||
1 -> "travail"
|
||||
2 -> "courses"
|
||||
3 -> "sante"
|
||||
4 -> "famille"
|
||||
5 -> "sport"
|
||||
6 -> "judiciare"
|
||||
7 -> "missions"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -19,11 +19,6 @@ data class Profile(
|
||||
var id: Int = CURRENT_PROFILE_ID
|
||||
|
||||
override fun toString(): String {
|
||||
val builder = StringBuilder()
|
||||
builder.append("Nom:", lastName, "; ")
|
||||
builder.append("Prenom:", firstName, "; ")
|
||||
builder.append("Naissance:", birthday, " a ", birthPlace, "; ")
|
||||
builder.append("Adresse:", address, " ", postalCode, " ", city, "; ")
|
||||
return builder.toString()
|
||||
return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city"
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.repository
|
||||
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
class AttestationRepository private constructor(private val attestationDao: AttestationDao) {
|
||||
|
||||
fun getProfile() = attestationDao.getAttestation()
|
||||
|
||||
fun insertProfile(attestation: Attestation) = attestationDao.insert(attestation)
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: AttestationRepository? = null
|
||||
|
||||
fun getInstance(attestationDao: AttestationDao) =
|
||||
instance ?: synchronized(this) {
|
||||
instance ?: AttestationRepository(attestationDao).also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
@ -4,28 +4,41 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.databinding.FragmentAttestationsBinding
|
||||
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
|
||||
|
||||
class AttestationsFragment : Fragment() {
|
||||
|
||||
private lateinit var dashboardViewModel: AttestationsViewModel
|
||||
private lateinit var attestationsViewModel: AttestationsViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
dashboardViewModel =
|
||||
ViewModelProviders.of(this).get(AttestationsViewModel::class.java)
|
||||
val root = inflater.inflate(R.layout.fragment_attestations, container, false)
|
||||
val textView: TextView = root.findViewById(R.id.text_dashboard)
|
||||
dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
|
||||
textView.text = it
|
||||
})
|
||||
return root
|
||||
initializeUi()
|
||||
val binding = DataBindingUtil.inflate<FragmentAttestationsBinding>(
|
||||
inflater,
|
||||
R.layout.fragment_add_attestation,
|
||||
container,
|
||||
false
|
||||
).apply {
|
||||
this.lifecycleOwner = this@AttestationsFragment
|
||||
this.viewModel = attestationsViewModel
|
||||
}
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
attestationsViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
@ -3,8 +3,9 @@ package fr.sanchezm.attestationsCovid19.ui.attestations
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
|
||||
class AttestationsViewModel : ViewModel() {
|
||||
class AttestationsViewModel(private val attestationRepository: AttestationRepository) : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is dashboard Fragment"
|
||||
|
@ -0,0 +1,15 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.attestations
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AttestationsViewModelFactory(private val attestationRepository: AttestationRepository) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AttestationsViewModel(attestationRepository) as T
|
||||
}
|
||||
}
|
@ -2,8 +2,10 @@ package fr.sanchezm.attestationsCovid19.utilities
|
||||
|
||||
import android.content.Context
|
||||
import fr.sanchezm.attestationsCovid19.data.db.MyDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory
|
||||
import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModel
|
||||
|
||||
object InjectorUtils {
|
||||
|
||||
@ -13,4 +15,10 @@ object InjectorUtils {
|
||||
|
||||
return AddViewModelFactory(profileRepository = profileRepository)
|
||||
}
|
||||
|
||||
fun provideAttestationViewModel(context: Context): AttestationsViewModel {
|
||||
val attestationRepository = AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
||||
return AttestationsViewModel(attestationRepository)
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<layout 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"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="viewModel"
|
||||
type="fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModel" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.attestations.AttestationsFragment">
|
||||
@ -19,4 +28,6 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</layout>
|
Loading…
Reference in New Issue
Block a user