Skip to content

Instantly share code, notes, and snippets.

@rogergcc
Last active August 25, 2023 00:28
Show Gist options
  • Save rogergcc/b516ef9019d38da4be5ddc1e2e4f7b44 to your computer and use it in GitHub Desktop.
Save rogergcc/b516ef9019d38da4be5ddc1e2e4f7b44 to your computer and use it in GitHub Desktop.
Manual Inyection VM
private val viewModel by viewModels<MovieViewModel> {
MovieViewModelFactory(
IMovieRepositoryImpl(
RemoteMovieDataSource(RetrofitClient.webservice),
LocalMovieDataSource(AppDatabase.getDatabase(requireContext()).movieDao())
)
)
}
//2 not arguments
//private val viewModel by viewModels<MovieViewModel>()
class MovieFragment : Fragment(R.layout.fragment_movie)
// ,MoviesAdapter.OnMovieClickListener {
override fun onMovieClick(movie: MovieResponse) {
val action = MovieFragmentDirections.actionMovieFragmentToMovieDetailFragment(
movie.poster_path,
movie.backdrop_path!!,
movie.vote_average.toFloat(),
movie.vote_count,
movie.overview,
movie.title,
movie.original_language,
movie.release_date
)
findNavController().navigate(action)
}
///////////////////////////////////
class MoviesAdapter(
// private val itemClickListener: OnMovieClickListener,
class MovieViewModelFactory(private val repo: IMovieRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return modelClass.getConstructor(IMovieRepository::class.java).newInstance(repo)
}
}
object RetrofitClient {
val webservice by lazy {
Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build().create(ApiService::class.java)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment