diff --git a/app/build.gradle b/app/build.gradle index 7752ef6..e03b3d2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,6 +47,7 @@ dependencies { // Life cycle and live data implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0" + implementation "androidx.lifecycle:lifecycle-extensions:2.0.0" // Dépendances pour les tests unitaires // ============================================= diff --git a/app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt b/app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt index 3608ace..dcba535 100644 --- a/app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt +++ b/app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt @@ -2,6 +2,7 @@ package com.devmind.devoxx import android.app.Application import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.ViewModelProvider import androidx.room.Room import com.devmind.devoxx.model.DevoxxDatabase diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt b/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt index 668379a..6f22079 100644 --- a/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt +++ b/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt @@ -4,19 +4,13 @@ import android.content.Intent import android.os.Bundle import android.widget.ArrayAdapter import android.widget.Toast +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders import com.devmind.devoxx.model.Speaker import kotlinx.android.synthetic.main.activity_speaker.* -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import java.util.* -import kotlin.coroutines.CoroutineContext -class SpeakerActivity : MainActivity(), CoroutineScope { - - override val coroutineContext: CoroutineContext - get() = Dispatchers.Default +class SpeakerActivity : MainActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -30,42 +24,42 @@ class SpeakerActivity : MainActivity(), CoroutineScope { setAdapter(ArrayAdapter(baseContext, android.R.layout.simple_dropdown_item_1line, countries)) } - if(!speakerUiid.isNullOrBlank()){ - launch { - val speaker = speakerDao.readOne(speakerUiid) - withContext(Dispatchers.Main){ - speaker.apply { - speakerFirstname.setText(speaker.firstname) - speakerLastname.setText(speaker.lastname) - speakerCountry.setText(speaker.country) - } - } - } + val model = ViewModelProviders.of(this).get(SpeakerViewModel::class.java) + + model.speakerLiveData.observe(this, Observer { + speakerFirstname.setText(it.firstname) + speakerLastname.setText(it.lastname) + speakerCountry.setText(it.country) + }) + if (!speakerUiid.isNullOrBlank()) { + model.loadSpeaker(speakerUiid) } buttonSpeakerSave.setOnClickListener { if (speakerFirstname.text.isNullOrBlank() || speakerLastname.text.isNullOrBlank()) { Toast.makeText(applicationContext, R.string.speaker_error_required, Toast.LENGTH_LONG).show() } else { - launch { - if (speakerUiid.isNullOrBlank()) { - speakerDao.create(Speaker( + if (speakerUiid.isNullOrBlank()) { + model.createSpeaker( + Speaker( speakerFirstname.text.toString(), speakerLastname.text.toString(), - speakerCountry.text.toString())) + speakerCountry.text.toString() + ) + ) - } else { - speakerDao.update(Speaker( + } else { + model.updateSpeaker( + Speaker( speakerFirstname.text.toString(), speakerLastname.text.toString(), speakerCountry.text.toString(), - speakerUiid)) - } - withContext(Dispatchers.Main){ - startActivity(Intent(applicationContext, SpeakerListActivity::class.java)) - } + speakerUiid + ) + ) } + startActivity(Intent(applicationContext, SpeakerListActivity::class.java)) } } } diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt b/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt index d72196d..65755e8 100644 --- a/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt +++ b/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt @@ -2,24 +2,20 @@ package com.devmind.devoxx import android.content.Intent import android.os.Bundle +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager +import com.devmind.devoxx.model.Speaker import com.devmind.devoxx.model.SpeakerAdapter import kotlinx.android.synthetic.main.activity_speaker_list.* -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import kotlin.coroutines.CoroutineContext interface SpeakerSelectionListener{ fun onSpeakerSelect(id: String) } -class SpeakerListActivity : MainActivity(), SpeakerSelectionListener, CoroutineScope { - - override val coroutineContext: CoroutineContext - get() = Dispatchers.Default +class SpeakerListActivity : MainActivity(), SpeakerSelectionListener { override fun onSpeakerSelect(id: String) { startActivity(Intent(applicationContext, SpeakerActivity::class.java).putExtra("ID", id)) @@ -36,12 +32,11 @@ class SpeakerListActivity : MainActivity(), SpeakerSelectionListener, CoroutineS adapter = SpeakerAdapter(this@SpeakerListActivity) } - launch { - val speakers = devoxxApplication.speakerDao().readAll() - withContext(Dispatchers.Main){ - (speakerList.adapter as SpeakerAdapter).updateData(speakers) - } - } + val model = ViewModelProviders.of(this).get(SpeakerListViewModel::class.java) + + model.speakersLiveData.observe(this, Observer>{ + (speakerList.adapter as SpeakerAdapter).updateData(it) + }) buttonAddSpeaker.setOnClickListener { onSpeakerSelect("") diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerListViewModel.kt b/app/src/main/java/com/devmind/devoxx/SpeakerListViewModel.kt new file mode 100644 index 0000000..6d36796 --- /dev/null +++ b/app/src/main/java/com/devmind/devoxx/SpeakerListViewModel.kt @@ -0,0 +1,32 @@ +package com.devmind.devoxx + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.MutableLiveData +import com.devmind.devoxx.model.Speaker +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import kotlin.coroutines.CoroutineContext + + +class SpeakerListViewModel(application : Application): AndroidViewModel(application), CoroutineScope { + + override val coroutineContext: CoroutineContext + get() = Dispatchers.Default + + val speakersLiveData: MutableLiveData> by lazy { + MutableLiveData>().also { + loadSpeakers(it) + } + } + + private fun loadSpeakers(liveData: MutableLiveData>){ + launch { + val speakers = getApplication().speakerDao().readAll() + liveData.postValue(speakers) + } + + } +} diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerViewModel.kt b/app/src/main/java/com/devmind/devoxx/SpeakerViewModel.kt new file mode 100644 index 0000000..607dd36 --- /dev/null +++ b/app/src/main/java/com/devmind/devoxx/SpeakerViewModel.kt @@ -0,0 +1,42 @@ +package com.devmind.devoxx + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.MutableLiveData +import com.devmind.devoxx.model.Speaker +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlin.coroutines.CoroutineContext + +class SpeakerViewModel(application: Application) : AndroidViewModel(application), CoroutineScope { + + override val coroutineContext: CoroutineContext + get() = Dispatchers.Default + + val speakerLiveData = MutableLiveData() + + fun dao() = getApplication().speakerDao() + + fun loadSpeaker(id: String) { + if (id.isEmpty()) { + speakerLiveData.postValue(Speaker.empty()) + } else { + launch { + speakerLiveData.postValue(dao().readOne(id)) + } + } + } + + fun createSpeaker(speaker: Speaker) = + launch { + dao().create(speaker) + speakerLiveData.postValue(speaker) + } + + fun updateSpeaker(speaker: Speaker) = + launch { + dao().update(speaker) + speakerLiveData.postValue(speaker) + } +} diff --git a/app/src/main/java/com/devmind/devoxx/model/Speaker.kt b/app/src/main/java/com/devmind/devoxx/model/Speaker.kt index 4f2cf6b..fe1b56a 100644 --- a/app/src/main/java/com/devmind/devoxx/model/Speaker.kt +++ b/app/src/main/java/com/devmind/devoxx/model/Speaker.kt @@ -13,7 +13,11 @@ data class Speaker( val country: String = "France", @PrimaryKey val uuid: String = UUID.randomUUID().toString() -) +){ + companion object { + fun empty() = Speaker("", "") + } +} @Dao interface SpeakerDao {