-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STEP 8 : use view models and live data
- Loading branch information
Showing
7 changed files
with
116 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/devmind/devoxx/SpeakerListViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters