Created
September 22, 2020 04:50
-
-
Save ch8n/1bc65b5ed753d133098e3b87656cee00 to your computer and use it in GitHub Desktop.
for medium article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Injector { | |
// step 7, okhttp and baseURL is standalone | |
private val okHttp = NetworkResolver.provideOkHttpClient() | |
private val baseURL = NetworkResolver.provideBaseURL() | |
// step 6, retrofit needs okhttp object and baseURL | |
private val retrofit = NetworkResolver.provideRetrofitClient( | |
baseURl, | |
okHttp | |
) | |
// step 5, api manager requires retrofit instance | |
private val apiManager = NetworkResolver.provideApiManager(retrofit) | |
// step 4, movie source requires apimanager | |
private val movieSource by lazy { | |
AppResolver.provideMovieDataSource(apiManager) | |
} | |
// step 3, factory requires movie repository | |
private val movieRepository by lazy { | |
AppResolver.provideMovieRepository(movieSource) | |
} | |
// step 2, Detail ViewModel requires factory | |
// things are lazy so you don't have create objects before it's getting | |
// used in the system and lazy also cache the result so acting as singleton too. | |
private val detailViewModelFactory by lazy { | |
ViewModelResolver.provideDetailViewModelFactory(movieRepository) | |
} | |
// step 1, we need to create a viewmodel which would be again | |
// and again called from viewmodel factory, hence we create it as a function | |
fun detailViewModel(activity: AppCompatActivity) : MovieDetailViewModel { | |
return ViewModelResolver.provideDetailViewModel( | |
detailViewModelFactory, activity | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment