Verified Commit 8625785f authored by Mikolai Gütschow's avatar Mikolai Gütschow
Browse files

avoid matching instances of different classes to same key

parent a1123169
Pipeline #8278 passed with stage
in 1 minute and 40 seconds
......@@ -52,11 +52,7 @@ fun <VM : ViewModel> viewModel(
"No ViewModelProvider was provided via LocalViewModelProvider"
}
): VM {
val viewModel = if (key == null) {
viewModelProvider.get(modelClass)
} else {
viewModelProvider.get(key, modelClass)
}
val viewModel = viewModelProvider.get(modelClass, key)
DisposableEffect(key) {
viewModel.onInit()
......
......@@ -16,11 +16,9 @@ constructor(
private val viewModels = HashMap<String, ViewModel>()
fun <VM : ViewModel> get(modelClass: KClass<VM>): VM =
get(modelClass.qualifiedName!!, modelClass)
fun <VM : ViewModel> get(key: String, modelClass: KClass<VM>): VM {
val viewModel = viewModels[key]
fun <VM : ViewModel> get(modelClass: KClass<VM>, key: String? = null): VM {
val viewModelKey = "${modelClass.qualifiedName}:$key"
val viewModel = viewModels[viewModelKey]
if (modelClass.isInstance(viewModel)) {
return viewModel as VM
......@@ -28,7 +26,7 @@ constructor(
try {
val viewModel = viewModelFactory.create(modelClass)
viewModels[key] = viewModel
viewModels[viewModelKey] = viewModel
return viewModel
} catch (e: InstantiationException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment