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.pages.PodInfoFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardActivity import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardActivity
import javax.inject.Scope
@Module @Module
@Suppress("unused") @Suppress("unused")
@ -50,15 +49,3 @@ abstract class OmnipodModule {
fun podStateManagerProvider(aapsPodStateManager: AapsPodStateManager): PodStateManager = aapsPodStateManager 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,47 +3,44 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dagger
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import dagger.Binds import dagger.Binds
import dagger.MapKey
import dagger.Module import dagger.Module
import dagger.Provides
import dagger.android.ContributesAndroidInjector import dagger.android.ContributesAndroidInjector
import dagger.multibindings.IntoMap import dagger.multibindings.IntoMap
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardFragment1 import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardFragment1
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardViewModel1 import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardViewModel1
import javax.inject.Inject
import javax.inject.Provider 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 @Module
abstract class OmnipodWizardModule { abstract class OmnipodWizardModule {
@Binds companion object {
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory @Provides
@JvmStatic
@OmnipodPluginQualifier
fun providesViewModelFactory(@OmnipodPluginQualifier viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>): ViewModelProvider.Factory {
return ViewModelFactory(viewModels)
}
}
// VIEW MODELS // #### VIEW MODELS ############################################################################
@Binds @Binds
@IntoMap @IntoMap
@OmnipodPluginQualifier
@ViewModelKey(WizardViewModel1::class) @ViewModelKey(WizardViewModel1::class)
internal abstract fun bindWizardViewModel1(viewModel: WizardViewModel1): ViewModel internal abstract fun bindWizardViewModel1(viewModel: WizardViewModel1): ViewModel
// Add the rest of the view models // Add the rest of the view models
// FRAGMENTS
// #### FRAGMENTS ##############################################################################
@FragmentScope @FragmentScope
@ContributesAndroidInjector @ContributesAndroidInjector
internal abstract fun contributesWizardFragment1(): WizardFragment1 internal abstract fun contributesWizardFragment1(): WizardFragment1
// Add the rest of the fragments // Add the rest of the fragments
} }

View file

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