cleanup and qualify view model injection

This commit is contained in:
Adrian 2020-09-13 21:35:36 +02:00
parent cbe488715a
commit d33fe6fba1
4 changed files with 58 additions and 33 deletions

View file

@ -0,0 +1,39 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dagger
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.MapKey
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Qualifier
import javax.inject.Scope
import kotlin.reflect.KClass
@Qualifier
annotation class OmnipodPluginQualifier
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
// TODO: These annotations and Factories could be used globally -> move to core or app
@Suppress("UNCHECKED_CAST")
class ViewModelFactory @Inject constructor(
private val viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
}
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope

View file

@ -15,7 +15,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.InitPodRe
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.PodInfoFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardActivity
import javax.inject.Scope
@Module
@Suppress("unused")
@ -50,15 +49,3 @@ abstract class OmnipodModule {
fun podStateManagerProvider(aapsPodStateManager: AapsPodStateManager): PodStateManager = aapsPodStateManager
}
}
// TODO: These annotations could be used globally -> move to core or app
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope

View file

@ -3,43 +3,38 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dagger
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.Binds
import dagger.MapKey
import dagger.Module
import dagger.Provides
import dagger.android.ContributesAndroidInjector
import dagger.multibindings.IntoMap
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardFragment1
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardViewModel1
import javax.inject.Inject
import javax.inject.Provider
import kotlin.reflect.KClass
@Suppress("UNCHECKED_CAST")
class ViewModelFactory @Inject constructor(
private val viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
}
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
@Module
abstract class OmnipodWizardModule {
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
companion object {
@Provides
@JvmStatic
@OmnipodPluginQualifier
fun providesViewModelFactory(@OmnipodPluginQualifier viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>): ViewModelProvider.Factory {
return ViewModelFactory(viewModels)
}
}
// VIEW MODELS
// #### VIEW MODELS ############################################################################
@Binds
@IntoMap
@OmnipodPluginQualifier
@ViewModelKey(WizardViewModel1::class)
internal abstract fun bindWizardViewModel1(viewModel: WizardViewModel1): ViewModel
// Add the rest of the view models
// FRAGMENTS
// #### FRAGMENTS ##############################################################################
@FragmentScope
@ContributesAndroidInjector
@ -47,3 +42,5 @@ abstract class OmnipodWizardModule {
// Add the rest of the fragments
}

View file

@ -10,11 +10,13 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.plugins.pump.omnipod.R
import info.nightscout.androidaps.plugins.pump.omnipod.dagger.OmnipodPluginQualifier
import javax.inject.Inject
class WizardFragment1 : DaggerFragment() {
@Inject
@OmnipodPluginQualifier
lateinit var viewModelFactory: ViewModelProvider.Factory
lateinit var viewModel: WizardViewModel1