@@ -3,10 +3,11 @@ package com.giraffe.presentation.home.screen.home
33import androidx.lifecycle.viewModelScope
44import com.giraffe.media.collections.entity.Collection
55import com.giraffe.media.collections.usecase.GetCollectionsUseCase
6+ import com.giraffe.media.entity.Genre
67import com.giraffe.media.exception.NoInternetException
78import com.giraffe.media.movie.entity.Movie
89import com.giraffe.media.movie.usecase.ObservePopularMoviesUseCase
9- import com.giraffe.media.movie.usecase.genre.GetMoviesGenresByIdsUseCase
10+ import com.giraffe.media.movie.usecase.genre.ObserveMoviesGenresUseCase
1011import com.giraffe.media.movie.usecase.matchesYourVibe.ObserveMatchesYourVibeMoviesUseCase
1112import com.giraffe.media.movie.usecase.recentlyReleased.ObserveRecentlyReleasedMoviesUseCase
1213import com.giraffe.media.movie.usecase.recentlyViewed.ObserveRecentlyViewedMoviesUseCase
@@ -41,18 +42,19 @@ class HomeViewModel @Inject constructor(
4142 private val observeTopRatedSeriesUseCase : ObserveTopRatedSeriesUseCase ,
4243 private val observeUpcomingMoviesUseCase : ObserveUpcomingMoviesUseCase ,
4344 private val getSeriesGenresByIdsUseCase : GetSeriesGenresByIdsUseCase ,
44- private val getMoviesGenresByIdsUseCase : GetMoviesGenresByIdsUseCase ,
4545 private val observeRecentlyViewedMoviesUseCase : ObserveRecentlyViewedMoviesUseCase ,
4646 private val observeRecentlyViewedSeriesUseCase : ObserveRecentlyViewedSeriesUseCase ,
4747 private val observeMatchesYourVibeMoviesUseCase : ObserveMatchesYourVibeMoviesUseCase ,
4848 private val observeMatchesYourVibeSeriesUseCase : ObserveMatchesYourVibeSeriesUseCase ,
4949 private val getCollectionsUseCase : GetCollectionsUseCase ,
5050 private val getUserNameUseCase : GetUserNameUseCase ,
51- private val isLoggedInByAccountUseCase : IsLoggedInByAccountUseCase
51+ private val isLoggedInByAccountUseCase : IsLoggedInByAccountUseCase ,
52+ private val observeMoviesGenresUseCase : ObserveMoviesGenresUseCase
5253) : BaseViewModel<HomeScreenState, HomeEffect>(initialState = HomeScreenState ()),
5354 HomeInteractionListener {
5455
5556 init {
57+
5658 loadHomeScreenData()
5759 }
5860
@@ -69,14 +71,30 @@ class HomeViewModel @Inject constructor(
6971 )
7072 }
7173 isLoggedIn()
72- getPopularity()
74+ getUserName()
75+ getYourCollections()
76+
77+ getMoviesGenres()
78+
7379 getRecentlyReleased()
7480 getUpcomingMovies()
7581 getMatchesYourVibe()
7682 getTopRatedSeries()
7783 getRecentViewed()
78- getUserName()
79- getYourCollections()
84+
85+ }
86+
87+ private fun getMoviesGenres () {
88+ safeCollect(
89+ onEmitNewValue = ::onGetMoviesGenresSuccess,
90+ onError = ::onError.also { getPopularity() },
91+ block = observeMoviesGenresUseCase::invoke
92+ )
93+ }
94+
95+ private fun onGetMoviesGenresSuccess (genres : List <Genre >) {
96+ updateState { it.copy(moviesGenres = genres) }
97+ getPopularity()
8098 }
8199
82100 private fun isLoggedIn () {
@@ -121,32 +139,25 @@ class HomeViewModel @Inject constructor(
121139
122140
123141 private fun getPopularity () {
124- viewModelScope.launch(Dispatchers .IO ) {
125- safeCollect(
126- onEmitNewValue = ::onGetPopularityMoviesSuccess,
127- onError = ::onError,
128- block = observePopularMoviesUseCase::invoke
129- )
130- safeCollect(
131- onEmitNewValue = ::onGetPopularitySeriesSuccess,
132- onError = ::onError,
133- block = observePopularSeriesUseCase::invoke
134- )
135- }
142+ safeCollect(
143+ onEmitNewValue = ::onGetPopularityMoviesSuccess,
144+ onError = ::onError,
145+ block = observePopularMoviesUseCase::invoke
146+ )
147+ safeCollect(
148+ onEmitNewValue = ::onGetPopularitySeriesSuccess,
149+ onError = ::onError,
150+ block = observePopularSeriesUseCase::invoke
151+ )
136152 }
137153
138154 private fun onGetPopularityMoviesSuccess (movies : List <Movie >) {
139- safeExecute {
140- val popularMoviesUi = movies.map { movie ->
141- val genres = getMoviesGenresByIdsUseCase(movie.genresID).map { it.title }
142- movie.toPopularMediaUi(genres)
143- }
144- updateState { currentState ->
145- currentState.copy(
146- popularity = (popularMoviesUi + currentState.popularity).distinctBy { it.id },
147- isLoadingPopularity = false
148- )
149- }
155+ updateState {
156+ val newMovies = movies.map { movie -> movie.toPopularMediaUi(it.moviesGenres) }
157+ it.copy(
158+ popularity = (newMovies + it.popularity).distinctBy { movie -> movie.id },
159+ isLoadingPopularity = false
160+ )
150161 }
151162 }
152163
0 commit comments