|
18 | 18 | package com.example.android.devbyteviewer.viewmodels
|
19 | 19 |
|
20 | 20 | 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 |
25 | 26 | import kotlinx.coroutines.CoroutineScope
|
26 | 27 | import kotlinx.coroutines.Dispatchers
|
27 | 28 | import kotlinx.coroutines.SupervisorJob
|
28 | 29 | import kotlinx.coroutines.launch
|
29 |
| -import java.io.IOException |
30 | 30 |
|
31 | 31 | /**
|
32 | 32 | * 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)
|
55 | 55 | */
|
56 | 56 | private val viewModelScope = CoroutineScope(viewModelJob + Dispatchers.Main)
|
57 | 57 |
|
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) |
80 | 60 |
|
81 | 61 | /**
|
82 | 62 | * init{} is called immediately when this ViewModel is created.
|
83 | 63 | */
|
84 | 64 | 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() |
99 | 67 | }
|
100 | 68 | }
|
101 | 69 |
|
| 70 | + val playlist = videosRepository.videos |
| 71 | + |
102 | 72 | /**
|
103 | 73 | * Cancel all coroutines when the ViewModel is cleared
|
104 | 74 | */
|
|
0 commit comments