@@ -6,13 +6,15 @@ import androidx.lifecycle.ViewModel
6
6
import androidx.lifecycle.viewModelScope
7
7
import com.arctouch.codechallenge.App
8
8
import com.arctouch.codechallenge.data.Cache
9
+ import com.arctouch.codechallenge.di.TmbRepository
9
10
import com.arctouch.codechallenge.model.Movie
10
11
import com.arctouch.codechallenge.model.UpcomingMoviesResponse
11
12
import kotlinx.coroutines.launch
13
+ import org.koin.core.KoinComponent
14
+ import org.koin.core.inject
12
15
13
- class HomeViewModel : ViewModel () {
16
+ class HomeViewModel ( private val repository : TmbRepository ) : ViewModel(), KoinComponent {
14
17
15
- private val api = App .api
16
18
private val _upcomingMovies = MutableLiveData <MutableList <Movie >>()
17
19
val upcomingMovies: LiveData <MutableList <Movie >> = _upcomingMovies
18
20
private val _searchLiveData = MutableLiveData <MutableList <Movie >>()
@@ -28,7 +30,7 @@ class HomeViewModel : ViewModel() {
28
30
fun getUpComingMovies () {
29
31
page = page.plus(1 )
30
32
viewModelScope.launch {
31
- val upcomingMoviesResponse = api .upcomingMovies(page = page)
33
+ val upcomingMoviesResponse = repository .upcomingMovies(page = page)
32
34
val moviesWithGenres = addGenres(upcomingMoviesResponse)
33
35
val list = _upcomingMovies .value
34
36
list?.addAll(moviesWithGenres)
@@ -38,7 +40,7 @@ class HomeViewModel : ViewModel() {
38
40
39
41
fun searchMovies (query : String ) {
40
42
viewModelScope.launch {
41
- val upcomingMoviesResponse = api .getMoviesByName(query)
43
+ val upcomingMoviesResponse = repository .getMoviesByName(query)
42
44
val moviesWithGenres = addGenres(upcomingMoviesResponse)
43
45
_searchLiveData .postValue(moviesWithGenres.toMutableList())
44
46
}
0 commit comments