-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMoviesViewModel.kt
44 lines (37 loc) · 1.5 KB
/
MoviesViewModel.kt
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
package com.khoshnaw.viewmodel.movies
import com.khoshnaw.entity.Movie
import com.khoshnaw.usecase.movie.loadMovieList.LoadMovieListInputPort
import com.khoshnaw.usecase.movie.loadMovieList.LoadMovieListOutputPort
import com.khoshnaw.viewmodel.mapper.toUIDTO
import com.khoshnaw.viewmodel.standard.StandardViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
@HiltViewModel
class MoviesViewModel @Inject constructor(
private val movieInputPort: LoadMovieListInputPort
) : StandardViewModel<MoviesState, MoviesIntent>(),
LoadMovieListOutputPort {
override suspend fun handleIntent(intent: MoviesIntent) = when (intent) {
is MoviesIntent.RefreshMovies -> handleRefreshMovies()
is MoviesIntent.OnMovieClicked -> handleMovieClicked(intent)
}
private suspend fun handleRefreshMovies() {
movieInputPort.startUpdatingMovieList()
}
private fun handleMovieClicked(intent: MoviesIntent.OnMovieClicked) {
state.value?.movies?.getOrNull(intent.position)?.takeIf { it.id == intent.id }?.let {
println("showing movie : $it")
}
}
override suspend fun startObserveMovies(flow: Flow<List<Movie>>) {
flow.collectResult {
updateState(MoviesState.MovieList(it.toUIDTO()))
}
}
override suspend fun showLoading(loading: Boolean) {
state.value?.movies?.let { movies ->
updateState(MoviesState.MovieList(movies, loading))
}
}
}