streetsofboston
03/09/2018, 3:12 PMoverride val kodein: Kodein = Kodein {
streetsofboston
03/09/2018, 3:14 PMoverride val kodein: Kodein by Kodein {
, which doesn’t compilesalomonbrys
03/09/2018, 3:16 PMstreetsofboston
03/09/2018, 3:17 PMsalomonbrys
03/09/2018, 3:17 PMstreetsofboston
03/09/2018, 3:19 PMsalomonbrys
03/09/2018, 3:21 PMstreetsofboston
03/09/2018, 3:23 PMViewModel
in Android given an Activity
.
The code to create one is ViewModelProviders.of(thisActivity).get(MyViewModel::class.java)
, which I want to put into a binding.salomonbrys
03/09/2018, 3:24 PMstreetsofboston
03/09/2018, 3:24 PMstreetsofboston
03/09/2018, 3:25 PMsalomonbrys
03/09/2018, 3:33 PMbind<ViewModel>() with multiton(ref = weakReference) { activity: Activity -> ViewModelProviders.of(activity).get(MyViewModel::class.java) }
, and retrieve with val view: MyViewModel by instance<ViewModel>(arg = this) as MyViewModel
.
2. Use a context singleton : bind<ViewModel>() with singleton(scope = androidScope<Activity>()) { ViewModelProviders.of(context).get(MyViewModel::class.java }
and, in the retriever, set the context: override val kodeinContext = kcontext(this)
, and finally retrieve it with val view: MyViewModel by instance<ViewModel>() as MyViewModel
Note that, as you can see, Kodein is not meant to be a sub-class factory. That's a popular demand, so I might look into that.streetsofboston
03/09/2018, 3:34 PMhamutarto
03/11/2018, 1:23 PMhamutarto
03/11/2018, 5:27 PMhamutarto
03/11/2018, 5:27 PMhamutarto
03/11/2018, 5:27 PMleosan
03/13/2018, 12:36 PMString
tag for a job and the instance of the job
but not sure if this is a factory, a provider…
what would be similar to this using Kodein
@Singleton
class AppJobCreator
@Inject constructor(
private val jobs: @JvmSuppressWildcards Map<String, Provider<Job>>
): JobCreator {
override fun create(tag: String): Job? {
val jobProvider = jobs[tag]
return jobProvider?.get()
}
}
leosan
03/13/2018, 2:17 PM@Provides @IntoMap
@StringKey( SendBookRequestJob.JOB_TAG )
Job provideSendBookRequestJob (BookApi api, Bus bus)
{
return new SendBookRequestJob( api, bus );
}
streetsofboston
03/14/2018, 6:23 PMContext
?
E.g if I provide the returned Kodein/KodeinAware when creating a class instance that will live longer than that Context (eg a ViewModel lives longer than an Activity)salomonbrys
03/16/2018, 3:10 PMContext
is not retained by the returned Kodein object. It is only used to "walk up the context tree" to find the "closest" Kodein, or return the Application Kodein if it does not find one.streetsofboston
03/16/2018, 3:18 PMclosestKodein
in various places and started rotating my Activity a bunch of times. Using the memory-monitor, I saw there was only one instance of my Activity after all that --> no leaks 🙂apatrida
03/31/2018, 4:33 AMkodein-di-generic-jvm
apatrida
03/31/2018, 4:44 AMapatrida
03/31/2018, 4:49 AMapatrida
04/01/2018, 1:21 AManstaendig
04/04/2018, 10:17 AManstaendig
04/04/2018, 10:17 AMFailed to resolve: org.kodein.di:kodein-di-generic-jvm:5.0.0
Joe
04/04/2018, 3:06 PMgaetan
04/05/2018, 12:26 PM