Skip to content

Commit

Permalink
STEP 8 : use view models and live data
Browse files Browse the repository at this point in the history
  • Loading branch information
javamind committed Apr 10, 2019
1 parent 7acc908 commit a0e8295
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 47 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =============================================
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 25 additions & 31 deletions app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Speaker> {
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))
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<List<Speaker>>{
(speakerList.adapter as SpeakerAdapter).updateData(it)
})

buttonAddSpeaker.setOnClickListener {
onSpeakerSelect("")
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/devmind/devoxx/SpeakerListViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<List<Speaker>> by lazy {
MutableLiveData<List<Speaker>>().also {
loadSpeakers(it)
}
}

private fun loadSpeakers(liveData: MutableLiveData<List<Speaker>>){
launch {
val speakers = getApplication<DevoxxApplication>().speakerDao().readAll()
liveData.postValue(speakers)
}

}
}
42 changes: 42 additions & 0 deletions app/src/main/java/com/devmind/devoxx/SpeakerViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<Speaker>()

fun dao() = getApplication<DevoxxApplication>().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)
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/devmind/devoxx/model/Speaker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a0e8295

Please sign in to comment.