diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt index 948fdf8..cbb9303 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/MyDatabase.kt @@ -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 diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt new file mode 100644 index 0000000..969b954 --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt new file mode 100644 index 0000000..f1037e1 --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt @@ -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 +) { + @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 -> "" + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt index 76decc6..310c9da 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt @@ -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" } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt new file mode 100644 index 0000000..242debf --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt @@ -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 } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt index 1aef0f2..35d5915 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt @@ -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( + 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) + }!! + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt index f802a7f..df298aa 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt @@ -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().apply { value = "This is dashboard Fragment" diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt new file mode 100644 index 0000000..913cb1b --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModelFactory.kt @@ -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 create(modelClass: Class): T { + return AttestationsViewModel(attestationRepository) as T + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt index b4f082b..23175b9 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/InjectorUtils.kt @@ -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) + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_attestations.xml b/app/src/main/res/layout/fragment_attestations.xml index 2a6c8fd..aa6a4f4 100644 --- a/app/src/main/res/layout/fragment_attestations.xml +++ b/app/src/main/res/layout/fragment_attestations.xml @@ -1,22 +1,33 @@ - + xmlns:tools="http://schemas.android.com/tools"> - + + + + + - \ No newline at end of file + android:layout_height="match_parent" + tools:context=".ui.attestations.AttestationsFragment"> + + + + + \ No newline at end of file