Newer
Older
DevAndroid / app / src / main / java / keijumt / devandroid / di / module / ViewModelModule.kt
package keijumt.devandroid.di.module

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import dagger.Binds
import dagger.MapKey
import dagger.Module
import dagger.multibindings.IntoMap
import keijumt.devandroid.viewmodel.ApiViewModel
import keijumt.devandroid.viewmodel.AsyncViewModel
import keijumt.devandroid.viewmodel.MainViewModel
import keijumt.devandroid.viewmodel.ViewModelFactory
import kotlin.reflect.KClass

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)

/**
 * Module: ViewModel 定義
 */
@Module
internal abstract class ViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    abstract fun bindMainViewModel(viewModel: MainViewModel): ViewModel

    @Binds
    @IntoMap
    @ViewModelKey(ApiViewModel::class)
    abstract fun bindApiViewModel(viewModel: ApiViewModel): ViewModel

    @Binds
    @IntoMap
    @ViewModelKey(AsyncViewModel::class)
    abstract fun bindAsyncViewModel(viewModel: AsyncViewModel): ViewModel

    @Binds
    abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory

}