Add Attestations entity, Dao, repository, viewModelFactory

This commit is contained in:
Mathieu Sanchez 2020-04-14 10:28:25 +02:00
parent 6903a584fc
commit 02b3c53a0a
10 changed files with 166 additions and 38 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase 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.dao.ProfileDao
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile 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 class MyDatabase private constructor() : RoomDatabase() {
abstract fun profileDao(): ProfileDao abstract fun profileDao(): ProfileDao
abstract fun attestationDao(): AttestationDao
companion object { companion object {
@Volatile @Volatile

View File

@ -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>
}

View File

@ -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 -> ""
}
}
}

View File

@ -19,11 +19,6 @@ data class Profile(
var id: Int = CURRENT_PROFILE_ID var id: Int = CURRENT_PROFILE_ID
override fun toString(): String { override fun toString(): String {
val builder = StringBuilder() return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city"
builder.append("Nom:", lastName, "; ")
builder.append("Prenom:", firstName, "; ")
builder.append("Naissance:", birthday, " a ", birthPlace, "; ")
builder.append("Adresse:", address, " ", postalCode, " ", city, "; ")
return builder.toString()
} }
} }

View File

@ -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 }
}
}
}

View File

@ -4,28 +4,41 @@ 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 android.widget.TextView import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import fr.sanchezm.attestationsCovid19.R import fr.sanchezm.attestationsCovid19.R
import fr.sanchezm.attestationsCovid19.databinding.FragmentAttestationsBinding
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
class AttestationsFragment : Fragment() { class AttestationsFragment : Fragment() {
private lateinit var dashboardViewModel: AttestationsViewModel private lateinit var attestationsViewModel: AttestationsViewModel
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
dashboardViewModel = initializeUi()
ViewModelProviders.of(this).get(AttestationsViewModel::class.java) val binding = DataBindingUtil.inflate<FragmentAttestationsBinding>(
val root = inflater.inflate(R.layout.fragment_attestations, container, false) inflater,
val textView: TextView = root.findViewById(R.id.text_dashboard) R.layout.fragment_add_attestation,
dashboardViewModel.text.observe(viewLifecycleOwner, Observer { container,
textView.text = it false
}) ).apply {
return root 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)
}!!
}
}

View File

@ -3,8 +3,9 @@ package fr.sanchezm.attestationsCovid19.ui.attestations
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 fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
class AttestationsViewModel : ViewModel() { class AttestationsViewModel(private val attestationRepository: AttestationRepository) : ViewModel() {
private val _text = MutableLiveData<String>().apply { private val _text = MutableLiveData<String>().apply {
value = "This is dashboard Fragment" value = "This is dashboard Fragment"

View File

@ -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
}
}

View File

@ -2,8 +2,10 @@ package fr.sanchezm.attestationsCovid19.utilities
import android.content.Context import android.content.Context
import fr.sanchezm.attestationsCovid19.data.db.MyDatabase 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.data.repository.ProfileRepository
import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory
import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModel
object InjectorUtils { object InjectorUtils {
@ -13,4 +15,10 @@ object InjectorUtils {
return AddViewModelFactory(profileRepository = profileRepository) return AddViewModelFactory(profileRepository = profileRepository)
} }
fun provideAttestationViewModel(context: Context): AttestationsViewModel {
val attestationRepository = AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
return AttestationsViewModel(attestationRepository)
}
} }

View File

@ -1,22 +1,33 @@
<?xml version="1.0" encoding="utf-8"?> <?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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools">
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.attestations.AttestationsFragment">
<TextView <data>
android:id="@+id/text_dashboard"
<variable
name="viewModel"
type="fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginStart="8dp" tools:context=".ui.attestations.AttestationsFragment">
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" <TextView
android:textAlignment="center" android:id="@+id/text_dashboard"
android:textSize="20sp" android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" /> android:layout_marginEnd="8dp"
</androidx.constraintlayout.widget.ConstraintLayout> android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>