Created
February 8, 2018 01:32
-
-
Save realdm/fa00abc98307f2af0d4136ac2f2a6b35 to your computer and use it in GitHub Desktop.
This file contains 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
@NonNull | |
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) { | |
String canonicalName = modelClass.getCanonicalName(); | |
if (canonicalName == null) { | |
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels"); | |
} | |
return get(DEFAULT_KEY + ":" + canonicalName, modelClass); | |
} | |
@MainThread | |
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) { | |
ViewModel viewModel = mViewModelStore.get(key); | |
if (modelClass.isInstance(viewModel)) { | |
//noinspection unchecked | |
return (T) viewModel; | |
} else { | |
//noinspection StatementWithEmptyBody | |
if (viewModel != null) { | |
// TODO: log a warning. | |
} | |
} | |
viewModel = mFactory.create(modelClass); | |
mViewModelStore.put(key, viewModel); | |
//noinspection unchecked | |
return (T) viewModel; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment