Skip to content

Commit 7223c51

Browse files
author
Jill Heske
committed
Step.06-Solution-Use-the-Repository
1 parent 96135dd commit 7223c51

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

app/src/main/java/com/example/android/devbyteviewer/viewmodels/DevByteViewModel.kt

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
package com.example.android.devbyteviewer.viewmodels
1919

2020
import android.app.Application
21-
import androidx.lifecycle.*
22-
import com.example.android.devbyteviewer.domain.Video
23-
import com.example.android.devbyteviewer.network.Network
24-
import com.example.android.devbyteviewer.network.asDomainModel
21+
import androidx.lifecycle.AndroidViewModel
22+
import androidx.lifecycle.ViewModel
23+
import androidx.lifecycle.ViewModelProvider
24+
import com.example.android.devbyteviewer.database.getDatabase
25+
import com.example.android.devbyteviewer.repository.VideosRepository
2526
import kotlinx.coroutines.CoroutineScope
2627
import kotlinx.coroutines.Dispatchers
2728
import kotlinx.coroutines.SupervisorJob
2829
import kotlinx.coroutines.launch
29-
import java.io.IOException
3030

3131
/**
3232
* DevByteViewModel designed to store and manage UI-related data in a lifecycle conscious way. This
@@ -55,50 +55,20 @@ class DevByteViewModel(application: Application) : AndroidViewModel(application)
5555
*/
5656
private val viewModelScope = CoroutineScope(viewModelJob + Dispatchers.Main)
5757

58-
// TODO (01) Remove _playlist, playlist variables, the init block, and refreshDataFromNetwork() function.
59-
60-
// TODO (02) Create a database variable and assign it to getDatabase(), passing the application.
61-
62-
// TODO (03) Define a videosRepository by calling the constructor and passing in the database.
63-
64-
// TODO (04) Create an init block and launch a coroutine to call videosRepository.refreshVideos().
65-
66-
// TODO (05) Get videos from the repository and assign it to a playlist variable.
67-
68-
/**
69-
* A playlist of videos that can be shown on the screen. This is private to avoid exposing a
70-
* way to set this value to observers.
71-
*/
72-
private val _playlist = MutableLiveData<List<Video>>()
73-
74-
/**
75-
* A playlist of videos that can be shown on the screen. Views should use this to get access
76-
* to the data.
77-
*/
78-
val playlist: LiveData<List<Video>>
79-
get() = _playlist
58+
private val database = getDatabase(application)
59+
private val videosRepository = VideosRepository(database)
8060

8161
/**
8262
* init{} is called immediately when this ViewModel is created.
8363
*/
8464
init {
85-
refreshDataFromNetwork()
86-
}
87-
88-
/**
89-
* Refresh data from network and pass it via LiveData. Use a coroutine launch to get to
90-
* background thread.
91-
*/
92-
private fun refreshDataFromNetwork() = viewModelScope.launch {
93-
try {
94-
val playlist = Network.devbytes.getPlaylist().await()
95-
_playlist.postValue(playlist.asDomainModel())
96-
} catch (networkError: IOException) {
97-
// Show an infinite loading spinner if the request fails
98-
// challenge exercise: show an error to the user if the network request fails
65+
viewModelScope.launch {
66+
videosRepository.refreshVideos()
9967
}
10068
}
10169

70+
val playlist = videosRepository.videos
71+
10272
/**
10373
* Cancel all coroutines when the ViewModel is cleared
10474
*/

0 commit comments

Comments
 (0)