Operator Wiki : Link
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
/* | |
* Copyright (C) 2014 Chris Banes | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
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
/* | |
* Copyright 2014 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
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
public class ViewModelProvider { | |
... | |
private final Factory mFactory; | |
private final ViewModelStore mViewModelStore; | |
// ① Use ViewModelStoreOwner | |
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) { | |
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory | |
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory() | |
: NewInstanceFactory.getInstance()); | |
} |
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 | |
@Override | |
public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { | |
... | |
mDefaultFactory = new SavedStateViewModelFactory( | |
getApplication(), | |
this, | |
getIntent() != null ? getIntent().getExtras() : null); // ◀ Activityに渡されたArgumentを代入 | |
... | |
return mDefaultFactory; |
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 | |
@Override | |
public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { | |
... | |
mDefaultFactory = new SavedStateViewModelFactory( | |
requireActivity().getApplication(), | |
this, | |
getArguments()); // ◀ Fragmentに渡されたArgumentを代入 | |
} | |
return mDefaultFactory; |
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
@MainThread | |
inline fun <reified VM : ViewModel> ComponentActivity.viewModels( | |
noinline factoryProducer: (() -> Factory)? = null | |
): Lazy<VM> { | |
val factoryPromise = factoryProducer ?: { | |
defaultViewModelProviderFactory | |
} | |
return ViewModelLazy(VM::class, { viewModelStore }, factoryPromise) | |
} |
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 | |
@Override | |
public <T extends ViewModel> T create(@NonNull String key, @NonNull Class<T> modelClass) { | |
boolean isAndroidViewModel = AndroidViewModel.class.isAssignableFrom(modelClass); | |
... | |
Constructor<T> constructor; | |
constructor = ... // Define ViewModel Constructor | |
// Case 1, SavedStateHandleが不要の場合 | |
if (constructor == null) { | |
return mFactory.create(modelClass); |
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
public final class SavedStateHandle { | |
static SavedStateHandle createHandle(@Nullable Bundle restoredState, | |
@Nullable Bundle defaultState) { | |
... | |
Map<String, Object> state = new HashMap<>(); | |
if (defaultState != null) { | |
for (String key : defaultState.keySet()) { | |
state.put(key, defaultState.get(key)); | |
} | |
} |
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
public final class SavedStateHandle { | |
final Map<String, Object> mRegular; | |
... | |
private final SavedStateProvider mSavedStateProvider = new SavedStateProvider() { | |
@NonNull | |
public Bundle saveState() { | |
Set<String> keySet = mRegular.keySet(); | |
ArrayList keys = new ArrayList(keySet.size()); | |
ArrayList value = new ArrayList(keys.size()); | |
... |
OlderNewer