Data binding and Room for storage not working yet
This commit is contained in:
@ -11,11 +11,11 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
val navView: BottomNavigationView = findViewById(R.id.nav_view)
|
||||
|
||||
val navController = findNavController(R.id.nav_host_fragment)
|
||||
|
||||
navController
|
||||
actionBar?.hide()
|
||||
navView.setupWithNavController(navController)
|
||||
navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme))
|
||||
|
@ -0,0 +1,34 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
@Database(
|
||||
entities = [Profile::class],
|
||||
version = 1
|
||||
)
|
||||
abstract class MyDatabase private constructor() : RoomDatabase() {
|
||||
|
||||
abstract fun profileDao(): ProfileDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: MyDatabase? = null
|
||||
private val LOCK = Any()
|
||||
|
||||
operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
|
||||
instance ?: buildDatabase(context).also { instance = it }
|
||||
}
|
||||
|
||||
private fun buildDatabase(context: Context) =
|
||||
Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
MyDatabase::class.java, "data.db"
|
||||
)
|
||||
.build()
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
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 fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
@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>
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
const val CURRENT_PROFILE_ID = 0
|
||||
|
||||
@Entity(tableName = "profile")
|
||||
data class Profile(
|
||||
val firstName: String,
|
||||
val lastName: String,
|
||||
val birthday: String,
|
||||
val birthPlace: String,
|
||||
val address: String,
|
||||
val city: String,
|
||||
val postalCode: String
|
||||
) {
|
||||
@PrimaryKey
|
||||
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()
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package fr.sanchezm.attestationsCovid19.data.repository
|
||||
|
||||
import fr.sanchezm.attestationsCovid19.data.db.dao.ProfileDao
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
|
||||
class ProfileRepository private constructor(private val profileDao: ProfileDao) {
|
||||
|
||||
fun getProfile() = profileDao.getProfile()
|
||||
|
||||
fun insertProfile(profile: Profile) = profileDao.insert(profile)
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: ProfileRepository? = null
|
||||
|
||||
fun getInstance(profileDao: ProfileDao) =
|
||||
instance ?: synchronized(this) {
|
||||
instance ?: ProfileRepository(profileDao).also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
@ -4,25 +4,41 @@ import android.os.Bundle
|
||||
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.ViewModelProviders
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.R
|
||||
import fr.sanchezm.attestationsCovid19.databinding.FragmentAddAttestationBinding
|
||||
import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils
|
||||
|
||||
class AddFragment : Fragment() {
|
||||
|
||||
private lateinit var homeViewModel: AddViewModel
|
||||
private lateinit var addViewModel: AddViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
homeViewModel =
|
||||
ViewModelProviders.of(this).get(AddViewModel::class.java)
|
||||
val root = inflater.inflate(R.layout.fragment_add_attestation, container, false)
|
||||
homeViewModel.text.observe(viewLifecycleOwner, Observer {
|
||||
})
|
||||
return root
|
||||
initializeUi()
|
||||
val binding = DataBindingUtil.inflate<FragmentAddAttestationBinding>(
|
||||
inflater,
|
||||
R.layout.fragment_add_attestation,
|
||||
container,
|
||||
false
|
||||
).apply {
|
||||
this.lifecycleOwner = this@AddFragment
|
||||
this.viewModel = addViewModel
|
||||
}
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun initializeUi() {
|
||||
val factory = context?.let { InjectorUtils.provideAddViewModelFactory(it) }
|
||||
addViewModel = factory?.let {
|
||||
ViewModelProvider(this, it)
|
||||
.get(AddViewModel::class.java)
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,69 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.sanchezm.attestationsCovid19.data.db.entity.Profile
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModel : ViewModel() {
|
||||
class AddViewModel(private val profileRepository: ProfileRepository) : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "Fragment ajouter une attestation"
|
||||
val firstName = MutableLiveData<String>()
|
||||
val lastName = MutableLiveData<String>()
|
||||
val birthday = MutableLiveData<String>()
|
||||
val birthPlace = MutableLiveData<String>()
|
||||
val address = MutableLiveData<String>()
|
||||
val city = MutableLiveData<String>()
|
||||
val postalCode = MutableLiveData<String>()
|
||||
val exitDate = MutableLiveData<String>()
|
||||
val exitHour = MutableLiveData<String>()
|
||||
|
||||
@SuppressLint("LongLogTag")
|
||||
fun onGenerateAttestationClick() {
|
||||
if (checkAllValue()) {
|
||||
profileRepository.insertProfile(getProfileFromView())
|
||||
} else {
|
||||
Log.e("onGenerateAttestationClick", "Cannot add profile")
|
||||
}
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
|
||||
init {
|
||||
setProfileValue()
|
||||
}
|
||||
|
||||
private fun setProfileValue() {
|
||||
val profile = profileRepository.getProfile()
|
||||
|
||||
firstName.value = profile.value?.firstName
|
||||
lastName.value = profile.value?.lastName
|
||||
birthday.value = profile.value?.birthday
|
||||
birthPlace.value = profile.value?.birthPlace
|
||||
address.value = profile.value?.address
|
||||
city.value = profile.value?.city
|
||||
postalCode.value = profile.value?.postalCode
|
||||
}
|
||||
|
||||
private fun getProfileFromView(): Profile {
|
||||
return Profile(
|
||||
firstName.value.toString(),
|
||||
lastName.value.toString(),
|
||||
birthday.value.toString(),
|
||||
birthPlace.value.toString(),
|
||||
address.value.toString(),
|
||||
city.value.toString(),
|
||||
postalCode.value.toString()
|
||||
)
|
||||
}
|
||||
|
||||
private fun checkAllValue(): Boolean {
|
||||
return !firstName.value.isNullOrEmpty()
|
||||
&& !lastName.value.isNullOrEmpty()
|
||||
&& !birthday.value.isNullOrEmpty()
|
||||
&& !birthPlace.value.isNullOrEmpty()
|
||||
&& !address.value.isNullOrEmpty()
|
||||
&& !city.value.isNullOrEmpty()
|
||||
&& !postalCode.value.isNullOrEmpty()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package fr.sanchezm.attestationsCovid19.ui.add
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
|
||||
class AddViewModelFactory(private val profileRepository: ProfileRepository) :
|
||||
ViewModelProvider.NewInstanceFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
return AddViewModel(profileRepository) as T
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package fr.sanchezm.attestationsCovid19.utilities
|
||||
|
||||
import android.content.Context
|
||||
import fr.sanchezm.attestationsCovid19.data.db.MyDatabase
|
||||
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
|
||||
import fr.sanchezm.attestationsCovid19.ui.add.AddViewModelFactory
|
||||
|
||||
object InjectorUtils {
|
||||
|
||||
fun provideAddViewModelFactory(context: Context): AddViewModelFactory {
|
||||
val profileRepository =
|
||||
ProfileRepository.getInstance(MyDatabase.invoke(context).profileDao())
|
||||
|
||||
return AddViewModelFactory(profileRepository = profileRepository)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user