Created
November 7, 2017 04:59
-
-
Save yshrsmz/7f33332caa71224f532592fa5b8dc339 to your computer and use it in GitHub Desktop.
ViewModelFactory and Dagger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.yslibrary.omnitweety.base | |
import android.arch.lifecycle.ViewModel | |
import android.arch.lifecycle.ViewModelProvider | |
import javax.inject.Inject | |
import javax.inject.Provider | |
import javax.inject.Singleton | |
/** | |
* Created by yshrsmz on 2017/07/22. | |
*/ | |
@Singleton | |
class ViewModelFactory @Inject constructor( | |
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>> | |
) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
val creator = creators[modelClass] | |
?: creators.entries.firstOrNull { (key) -> modelClass.isAssignableFrom(key) }?.value | |
?: throw IllegalArgumentException("Unknown ViewModel Class: $modelClass") | |
@Suppress("UNCHECKED_CAST") | |
return try { | |
creator.get() as T | |
} catch (e: Exception) { | |
throw RuntimeException(e) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.yslibrary.omnitweety.base | |
import android.arch.lifecycle.ViewModel | |
import android.arch.lifecycle.ViewModelProvider | |
import dagger.Binds | |
import dagger.MapKey | |
import dagger.Module | |
import dagger.multibindings.IntoMap | |
import net.yslibrary.omnitweety.compose.TweetComposeViewModel | |
import net.yslibrary.omnitweety.entry.EntryViewModel | |
import net.yslibrary.omnitweety.history.HistoryViewModel | |
import net.yslibrary.omnitweety.license.LicenseViewModel | |
import net.yslibrary.omnitweety.setting.SettingsViewModel | |
import net.yslibrary.omnitweety.settingadvanced.AdvancedSettingsViewModel | |
import kotlin.reflect.KClass | |
/** | |
* Created by yshrsmz on 2017/07/22. | |
*/ | |
@Module | |
abstract class ViewModelModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey(EntryViewModel::class) | |
abstract fun bindEntryViewModel(viewModel: EntryViewModel): ViewModel | |
@Binds | |
@IntoMap | |
@ViewModelKey(HistoryViewModel::class) | |
abstract fun bindHistoryViewModel(viewModel: HistoryViewModel): ViewModel | |
@Binds | |
@IntoMap | |
@ViewModelKey(LicenseViewModel::class) | |
abstract fun bindLicenseViewModel(viewModel: LicenseViewModel): ViewModel | |
@Binds | |
@IntoMap | |
@ViewModelKey(SettingsViewModel::class) | |
abstract fun bindSettingsViewModel(viewModel: SettingsViewModel): ViewModel | |
@Binds | |
@IntoMap | |
@ViewModelKey(AdvancedSettingsViewModel::class) | |
abstract fun bindAdvancedSettingsViewModel(viewModel: AdvancedSettingsViewModel): ViewModel | |
@Binds | |
@IntoMap | |
@ViewModelKey(TweetComposeViewModel::class) | |
abstract fun bindTweetComposeViewModel(viewModel: TweetComposeViewModel): ViewModel | |
@Binds | |
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory | |
} | |
@MustBeDocumented | |
@Target(AnnotationTarget.FUNCTION) | |
@Retention(AnnotationRetention.RUNTIME) | |
@MapKey | |
annotation class ViewModelKey(val value: KClass<out ViewModel>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment