Colton Idle
09/22/2023, 9:41 PM@InstallIn(SingletonComponent::class)
@Module
class AppModule {
@Singleton
@Provides
fun provideApiService(): ApiService {
return ApiServiceBuilder()
.build(//baseUrl Will GO here)
}
val appModule = module {
single { ApiServiceBuilder().build() }
}
?Javier
09/22/2023, 9:55 PMColton Idle
09/22/2023, 9:56 PM@HiltViewModel
class MyScreenViewModel
@Inject
constructor(
val apiService: ApiService,
val savedStateHandle: SavedStateHandle
) : ViewModel() {
Javier
09/22/2023, 9:57 PMColton Idle
09/22/2023, 9:58 PMJavier
09/22/2023, 9:58 PMColton Idle
09/22/2023, 10:00 PMclass MyApplication : Application(){
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApplication)
modules(appModule)
}
}
}
val appModule = module {
single { buildApiService("<https://myapiUrl>") }
factory { CloseableCoroutineScope() }
viewModel { MyViewModel(get(), get()) }
}
class MyViewModel (
private val apiService: ApiService,
private val closeableCoroutineScope: CloseableCoroutineScope
) : ViewModel(closeableCoroutineScope) {
and
@Composable
fun MyScreen() {
val myViewModel = koinInject<MyViewModel>()
The only thing I'm sorta worried about is whether or not my ViewModel will be properly scoped to the nav graph like how hilt + compose + AAC nav works.ascii
09/23/2023, 5:52 AMVlad
09/23/2023, 10:40 AMColton Idle
09/23/2023, 1:45 PMMatt Rea
09/23/2023, 5:17 PMkoin-androidx-compose-navigation
artifact that exposes koinNavViewModel()
, which uses LocalViewModelStoreOwner
.
However I don’t think this is set automatically by androidx navigation? You could always inspect your app at runtime and see if that CompositionLocal is populated. But you may need to set it yourself
https://stackoverflow.com/questions/69002018/why-a-new-viewmodel-is-created-in-each-compose-navigation-routeget()
at all. For most objects, you just declare your dependency once and never have to update the declaration again
https://insert-koin.io/docs/reference/koin-android/dsl-update
But there’s also the KSP codegen stuff, which works pretty well. But I like a little more control. Sometimes I want to put a small bit of code in my DI - like instantiating OkHttpClient.Builder() for example
https://insert-koin.io/docs/reference/koin-annotations/startarnaud.giuliani
09/26/2023, 3:43 PMThe way I see it, most people want to migrate to Hilt because it's AndroidX and integrates better with pretty much any other AndroidX lib. Plus, Hilt does its magic compile-time, while Koin is fully-runtime I think? Meaning Hilt just offers better performance.@ascii Koin is using the same Android API, and save up to 75% of your compilation time. It's also far more easy to lear and use, and doesn't constraint you in terms of development paradigm