-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamesViewModel.kt
More file actions
52 lines (44 loc) · 1.65 KB
/
Copy pathGamesViewModel.kt
File metadata and controls
52 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package band.effective.compose.drawer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import band.effective.compose.drawer.AppConfiguration
import band.effective.compose.drawer.domain.Game
import band.effective.compose.drawer.domain.GamesResponse
import band.effective.compose.drawer.network.GamesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
class GamesViewModel : ViewModel() {
data class State(
val isLoading: Boolean = true,
val games: List<Game> = emptyList(),
val error: String? = ""
)
private val mutableState: MutableStateFlow<State> = MutableStateFlow(State())
val state: StateFlow<State> = mutableState.asStateFlow()
private val api: GamesApi = AppConfiguration.api
init {
loadData()
}
fun loadData() = viewModelScope.launch {
mutableState.update { it.copy(isLoading = true, error = null) }
Timber.v("Fetching games list...")
try {
val response: GamesResponse = api.getGames()
mutableState.update {
it.copy(
games = response.results,
isLoading = false,
error = null
)
}
Timber.v("Fetched games list successfully.")
} catch (e: Exception) {
Timber.e(e, "Something went wrong when fetching games list.")
mutableState.update { it.copy(error = e.localizedMessage, isLoading = false) }
}
}
}