Created
September 17, 2020 16:19
-
-
Save hantrungkien/2c0cc256376ea0a606fdb61e1ba7ffeb to your computer and use it in GitHub Desktop.
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
@Module | |
@InstallIn(ActivityComponent::class) | |
object ActivityViewModelModule { | |
@Provides | |
fun provideSavedStateViewModelFactory( | |
application: Application, | |
activity: Activity, | |
viewModelFactories: @JvmSuppressWildcards Map<String, Provider<ViewModelAssistedFactory<out ViewModel>>>, | |
): DFMSavedStateViewModelFactory { | |
val owner = activity as SavedStateRegistryOwner | |
val defaultArgs = activity.intent?.extras | |
val delegate = SavedStateViewModelFactory(application, owner, defaultArgs) | |
return DFMSavedStateViewModelFactory(owner, defaultArgs, delegate, viewModelFactories) | |
} | |
} |
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
@EntryPoint | |
@InstallIn(ApplicationComponent::class) | |
interface CoreModuleDependencies { | |
fun exposeApplication(): Application | |
@UserModelSingletonQualifier | |
fun exposeUserManager(): UserModel | |
} |
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
class DFMSavedStateViewModelFactory( | |
owner: SavedStateRegistryOwner, | |
defaultArgs: Bundle?, | |
private val delegateFactory: SavedStateViewModelFactory, | |
private val viewModelFactories: @JvmSuppressWildcards Map<String, Provider<ViewModelAssistedFactory<out ViewModel>>>, | |
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) { | |
@SuppressLint("RestrictedApi") | |
override fun <T : ViewModel?> create( | |
key: String, | |
modelClass: Class<T>, | |
handle: SavedStateHandle | |
): T { | |
val factoryProvider = viewModelFactories[modelClass.name] | |
?: return delegateFactory.create("$KEY_PREFIX:$key", modelClass) | |
@Suppress("UNCHECKED_CAST") | |
return factoryProvider.get().create(handle) as T | |
} | |
companion object { | |
private const val KEY_PREFIX = "androidx.hilt.lifecycle.HiltViewModelFactory" | |
} | |
} |
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
@Module | |
@InstallIn(FragmentComponent::class) | |
object FragmentViewModelModule { | |
@Provides | |
fun provideSavedStateViewModelFactory( | |
application: Application, | |
fragment: Fragment, | |
viewModelFactories: @JvmSuppressWildcards Map<String, Provider<ViewModelAssistedFactory<out ViewModel>>>, | |
): DFMSavedStateViewModelFactory { | |
val defaultArgs = fragment.arguments | |
val delegate = SavedStateViewModelFactory(application, fragment, defaultArgs) | |
return DFMSavedStateViewModelFactory(fragment, defaultArgs, delegate, viewModelFactories) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment