Add Info Page
This commit is contained in:
@ -1,5 +1,30 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db
|
||||
|
||||
import android.content.Context
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
|
||||
class MyDatabase private constructor(private val path: String) {
|
||||
|
||||
private val _profileDao: ProfileDao? = null
|
||||
private val _attestationDao: AttestationDao? = null
|
||||
|
||||
fun profileDao() : ProfileDao = _profileDao ?: ProfileDao(path)
|
||||
|
||||
fun attestationDao() : AttestationDao = _attestationDao ?: AttestationDao(path)
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: MyDatabase? = null
|
||||
private val LOCK = Any()
|
||||
|
||||
operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
|
||||
instance ?: MyDatabase(context.filesDir.path).also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
@ -33,4 +58,5 @@ abstract class MyDatabase private constructor() : RoomDatabase() {
|
||||
)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -1,20 +1,14 @@
|
||||
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
|
||||
class AttestationDao(private val path: String) {
|
||||
|
||||
@Dao
|
||||
interface AttestationDao {
|
||||
@Insert
|
||||
fun insert(attestation: Attestation)
|
||||
}
|
||||
|
||||
@Query(value = "SELECT * FROM attestation WHERE id = $CURRENT_ATTESTATION_ID")
|
||||
fun getAttestation(): LiveData<Attestation>
|
||||
}
|
||||
//@Dao
|
||||
//interface AttestationDao {
|
||||
// @Insert
|
||||
// fun insert(attestation: Attestation)
|
||||
//
|
||||
// @Query(value = "SELECT * FROM attestation WHERE id = $CURRENT_ATTESTATION_ID")
|
||||
// fun getAttestation(): LiveData<Attestation>
|
||||
//}
|
@ -1,18 +1,46 @@
|
||||
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.CURRENT_PROFILE_ID
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.google.gson.Gson
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import java.io.File
|
||||
|
||||
@Dao
|
||||
interface ProfileDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(profile: Profile)
|
||||
class ProfileDao(private val path: String) {
|
||||
|
||||
@Query(value = "SELECT * FROM profile WHERE id = $CURRENT_PROFILE_ID")
|
||||
fun getProfile(): LiveData<Profile>
|
||||
}
|
||||
private var _profile = MutableLiveData<Profile>()
|
||||
private var fileName = "profile.db"
|
||||
|
||||
fun getProfile(): LiveData<Profile> =
|
||||
_profile
|
||||
|
||||
fun updateProfile(profile: Profile) {
|
||||
_profile.value = profile
|
||||
save()
|
||||
}
|
||||
|
||||
init {
|
||||
load()
|
||||
}
|
||||
|
||||
private fun load() {
|
||||
val file = File("$path/$fileName")
|
||||
|
||||
if (file.exists()) {
|
||||
_profile.value = Gson().fromJson(file.readText(Charsets.UTF_8), Profile::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
private fun save() {
|
||||
File("$path/$fileName").writeText(Gson().toJson(this))
|
||||
}
|
||||
}
|
||||
|
||||
//@Dao
|
||||
//interface ProfileDao {
|
||||
// @Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
// fun insert(profile: Profile)
|
||||
//
|
||||
// @Query(value = "SELECT * FROM profile WHERE id = $CURRENT_PROFILE_ID")
|
||||
// fun getProfile(): LiveData<Profile>
|
||||
//}
|
||||
|
@ -5,19 +5,23 @@ 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) {
|
||||
class AttestationRepository private constructor(/*private val attestationDao: AttestationDao*/) {
|
||||
|
||||
fun getProfile() = attestationDao.getAttestation()
|
||||
// fun getAttestation() = attestationDao.getAttestation()
|
||||
|
||||
fun insertProfile(attestation: Attestation) = attestationDao.insert(attestation)
|
||||
fun createAttestation() {
|
||||
|
||||
}
|
||||
// fun insertAttestation(attestation: Attestation) = attestationDao.insert(attestation)
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: AttestationRepository? = null
|
||||
|
||||
fun getInstance(attestationDao: AttestationDao) =
|
||||
fun getInstance(/*attestationDao: AttestationDao*/) =
|
||||
instance ?: synchronized(this) {
|
||||
instance ?: AttestationRepository(attestationDao).also { instance = it }
|
||||
// instance ?: AttestationRepository(attestationDao).also { instance = it }
|
||||
instance ?: AttestationRepository().also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +1,35 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.repository
|
||||
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
class ProfileRepository private constructor(private val profileDao: ProfileDao) {
|
||||
class ProfileRepository private constructor(/*private val profileDao: ProfileDao*/) {
|
||||
|
||||
fun getProfile() = profileDao.getProfile()
|
||||
private var _profile = MutableLiveData(Profile(
|
||||
"Mathieu",
|
||||
"Sanchez",
|
||||
"19/11/1997",
|
||||
"Perpignan",
|
||||
"33 Rue Salvador Dali",
|
||||
"Canohes",
|
||||
"66680"
|
||||
))
|
||||
|
||||
fun insertProfile(profile: Profile) = profileDao.insert(profile)
|
||||
fun getProfile(): LiveData<Profile> = _profile
|
||||
|
||||
// fun insertProfile(profile: Profile) = profileDao.insert(profile)
|
||||
fun insertProfile(profile: Profile) {
|
||||
_profile.value = profile
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: ProfileRepository? = null
|
||||
|
||||
fun getInstance(profileDao: ProfileDao) =
|
||||
fun getInstance(/*profileDao: ProfileDao*/) =
|
||||
instance ?: synchronized(this) {
|
||||
instance ?: ProfileRepository(profileDao).also { instance = it }
|
||||
instance ?: ProfileRepository(/*profileDao*/).also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,15 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.databinding.FragmentAddAttestationBinding
|
||||
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
|
||||
@ -31,14 +34,26 @@ class AddFragment : Fragment() {
|
||||
this.viewModel = addViewModel
|
||||
}
|
||||
|
||||
bindMessage(binding.context)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun bindMessage(context: View) {
|
||||
addViewModel.errorMessage.observe(viewLifecycleOwner, Observer {
|
||||
it.getContentIfNotHandled()?.let { stringId ->
|
||||
Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT ).show()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
addViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AddViewModel::class.java)
|
||||
}!!
|
||||
// val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
// addViewModel = factory?.let {
|
||||
// ViewModelProvider(this, it)
|
||||
// .get(AddViewModel::class.java)
|
||||
// }!!
|
||||
val factory = InjectorUtils.provideAddViewModelFactory()
|
||||
addViewModel = ViewModelProvider(this, factory)
|
||||
.get(AddViewModel::class.java)
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,24 @@ package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.utilities.Event
|
||||
|
||||
class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel() {
|
||||
class AddViewModel(
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _errorMessage = MutableLiveData<Event<Int>>()
|
||||
|
||||
val errorMessage: LiveData<Event<Int>>
|
||||
get() = _errorMessage
|
||||
|
||||
val firstName = MutableLiveData<String>()
|
||||
val lastName = MutableLiveData<String>()
|
||||
@ -19,12 +31,22 @@ class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel
|
||||
val exitDate = MutableLiveData<String>()
|
||||
val exitHour = MutableLiveData<String>()
|
||||
|
||||
val reason1 = MutableLiveData(false)
|
||||
val reason2 = MutableLiveData(false)
|
||||
val reason3 = MutableLiveData(false)
|
||||
val reason4 = MutableLiveData(false)
|
||||
val reason5 = MutableLiveData(false)
|
||||
val reason6 = MutableLiveData(false)
|
||||
val reason7 = MutableLiveData(false)
|
||||
|
||||
@SuppressLint("LongLogTag")
|
||||
fun onGenerateAttestationClick() {
|
||||
if (checkAllValue()) {
|
||||
profileRepository.insertProfile(getProfileFromView())
|
||||
attestationRepository.createAttestation()
|
||||
} else {
|
||||
Log.e("onGenerateAttestationClick", "Cannot add profile")
|
||||
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
||||
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,6 +86,19 @@ class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel
|
||||
&& !address.value.isNullOrEmpty()
|
||||
&& !city.value.isNullOrEmpty()
|
||||
&& !postalCode.value.isNullOrEmpty()
|
||||
&& !exitDate.value.isNullOrEmpty()
|
||||
&& !exitHour.value.isNullOrEmpty()
|
||||
&& checkAtLeastOneReasonSelected()
|
||||
}
|
||||
|
||||
private fun checkAtLeastOneReasonSelected(): Boolean {
|
||||
return reason1.value!!
|
||||
|| reason2.value!!
|
||||
|| reason3.value!!
|
||||
|| reason4.value!!
|
||||
|| reason5.value!!
|
||||
|| reason6.value!!
|
||||
|| reason7.value!!
|
||||
}
|
||||
|
||||
}
|
@ -2,13 +2,17 @@ package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModelFactory(private val profileRepository: ProfileRepository) :
|
||||
class AddViewModelFactory(
|
||||
private val profileRepository: ProfileRepository,
|
||||
private val attestationRepository: AttestationRepository
|
||||
) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(profileRepository) as T
|
||||
return AddViewModel(profileRepository, attestationRepository) as T
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ class AttestationsFragment : Fragment() {
|
||||
initializeUi()
|
||||
val binding = DataBindingUtil.inflate<FragmentAttestationsBinding>(
|
||||
inflater,
|
||||
R.layout.fragment_add_attestation,
|
||||
R.layout.fragment_attestations,
|
||||
container,
|
||||
false
|
||||
).apply {
|
||||
@ -35,10 +35,13 @@ class AttestationsFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
attestationsViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}!!
|
||||
// val factory = context?.let { InjectorUtils.provideAttestationViewModel(it) }
|
||||
// attestationsViewModel = factory?.let {
|
||||
// ViewModelProvider(this, it)
|
||||
// .get(AttestationsViewModel::class.java)
|
||||
// }!!
|
||||
val factory = InjectorUtils.provideAttestationViewModel()
|
||||
attestationsViewModel = ViewModelProvider(this, factory)
|
||||
.get(AttestationsViewModel::class.java)
|
||||
}
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
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(private val attestationRepository: AttestationRepository) : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is dashboard Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
// private val _text = MutableLiveData<String>().apply {
|
||||
// value = "This is dashboard Fragment"
|
||||
// }
|
||||
// val text: LiveData<String> = _text
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.info
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import fr.sanchezm.attestationsCovid19.BuildConfig
|
||||
|
||||
import fr.sanchezm.attestationsCovid19.R;
|
||||
|
||||
class InfoFragment : Fragment() {
|
||||
|
||||
override fun onCreateView(
|
||||
inflater:LayoutInflater,
|
||||
container:ViewGroup?,
|
||||
savedInstanceState:Bundle?
|
||||
): View? {
|
||||
val root = inflater.inflate(R.layout.fragment_info, container, false)
|
||||
|
||||
root.findViewById<TextView>(R.id.develop_by).movementMethod = LinkMovementMethod.getInstance()
|
||||
root.findViewById<TextView>(R.id.version).text = getVersionText()
|
||||
return root
|
||||
}
|
||||
|
||||
private fun getVersionText(): String {
|
||||
val versionText = getString(R.string.version_number);
|
||||
val versionName = BuildConfig.VERSION_NAME
|
||||
|
||||
return "$versionText $versionName"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
package fr.sanchezm.attestationsCovid19.utilities
|
||||
|
||||
open class Event<out T>(private val content: T) {
|
||||
var hasBeenHandled = false
|
||||
private set
|
||||
|
||||
fun getContentIfNotHandled(): T? {
|
||||
return if (hasBeenHandled) {
|
||||
null
|
||||
} else {
|
||||
hasBeenHandled = true
|
||||
content
|
||||
}
|
||||
}
|
||||
|
||||
fun peekContent(): T = content
|
||||
}
|
@ -5,20 +5,30 @@ 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
|
||||
import fr.sanchezm.attestationsCovid19.ui.attestations.AttestationsViewModelFactory
|
||||
|
||||
object InjectorUtils {
|
||||
|
||||
fun provideAddViewModelFactory(context: Context): AddViewModelFactory {
|
||||
// val profileRepository =
|
||||
// ProfileRepository.getInstance()
|
||||
// val attestationRepository =
|
||||
// AttestationRepository.getInstance()
|
||||
val profileRepository =
|
||||
ProfileRepository.getInstance(MyDatabase.invoke(context).profileDao())
|
||||
val attestationRepository =
|
||||
AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
||||
|
||||
return AddViewModelFactory(profileRepository = profileRepository)
|
||||
return AddViewModelFactory(profileRepository, attestationRepository)
|
||||
}
|
||||
|
||||
fun provideAttestationViewModel(context: Context): AttestationsViewModel {
|
||||
val attestationRepository = AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
||||
return AttestationsViewModel(attestationRepository)
|
||||
fun provideAttestationViewModel(/*context: Context*/): AttestationsViewModelFactory {
|
||||
val attestationRepository =
|
||||
AttestationRepository.getInstance()
|
||||
// val attestationRepository =
|
||||
// AttestationRepository.getInstance(MyDatabase.invoke(context).attestationDao())
|
||||
|
||||
return AttestationsViewModelFactory(attestationRepository)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user