Java Code Examples for androidx.lifecycle.ViewModelProvider#Factory

The following examples show how to use androidx.lifecycle.ViewModelProvider#Factory . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PresenterModule.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
@PerFragment
@Provides
public LiveDataRowPresenter provideLiveDataRowPresenter(ViewModelProvider.Factory factory,
        DataLoadedListener activity) {
    LiveDataRowPresenter rowPresenter = new LiveDataRowPresenter(factory);

    rowPresenter.registerDataLoadedListener(activity);
    return rowPresenter;
}
 
Example 2
Source File: ThreadDetailFragment.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(false);

    if (LOG_DEBUG) {
        Log.d(LOG_TAG, "Entered onCreate()");
    }

    if (savedInstanceState == null) {
        extractExtras(getArguments());
    } else {
        if (savedInstanceState.containsKey(Extras.EXTRAS_THREAD_ID)
                && getArguments() != null
                && getArguments().containsKey(Extras.EXTRAS_THREAD_ID)
                && savedInstanceState.getLong(Extras.EXTRAS_THREAD_ID) == getArguments().getLong(Extras.EXTRAS_THREAD_ID)) {
            extractExtras(savedInstanceState);
        } else {
            extractExtras(getArguments());
        }

    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    prefs.edit().remove(RefreshJobService.NOTIFICATIONS_KEY_THREAD_SIZE + "." + threadId).apply();

    lastReadRunnable = new LastReadRunnable(threadId, boardName);
    threadReplyFragmentTag = REPLY_FRAGMENT_TAG + "_" + threadId;

    ViewModelProvider.Factory factory = new ViewModelProvider.Factory() {
        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) new ThreadViewModel(boardName, threadId);
        }
    };
    viewModel = ViewModelProviders
            .of(this, factory)
            .get(ThreadViewModel.class);
}
 
Example 3
Source File: ViewModelProviders.java    From deagle with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given Activity
 * is alive. More detailed explanation is in {@link ViewModel}.
 * <p>
 * It uses the given {@link ViewModelProvider.Factory} to instantiate new ViewModels.
 *
 * @param activity an activity, in whose scope ViewModels should be retained
 * @param factory  a {@code Factory} to instantiate new ViewModels
 * @return a ViewModelProvider instance
 */
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleViewModelActivity activity,
								   @Nullable ViewModelProvider.Factory factory) {
	Application application = checkApplication(activity);
	if (factory == null) {
		factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
	}
	return new ViewModelProvider(activity.getViewModelStore(), factory);
}
 
Example 4
Source File: ViewModelModule.java    From tv-samples with Apache License 2.0 4 votes vote down vote up
@Binds
@Singleton
abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
 
Example 5
Source File: LiveDataRowPresenter.java    From tv-samples with Apache License 2.0 4 votes vote down vote up
public LiveDataRowPresenter(ViewModelProvider.Factory factory) {
    super();
    mDataLoadedListeners = new ArrayList<>();
    viewModelFactory = factory;
}
 
Example 6
Source File: ViewModelModule.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Provides
@Singleton
static ViewModelProvider.Factory provideViewModelFactory(@NonNull final ViewModelSubComponent.Builder viewModelSubComponent) {
    return new ViewModelFactory(viewModelSubComponent.build());
}
 
Example 7
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
 
Example 8
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 9
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 10
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 11
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 12
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 13
Source File: ViewModelModule.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ParamsViewModelFactory factory);
 
Example 14
Source File: ViewModelProviders.java    From deagle with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given
 * {@code fragment} is alive. More detailed explanation is in {@link ViewModel}.
 * <p>
 * It uses the given {@link ViewModelProvider.Factory} to instantiate new ViewModels.
 *
 * @param fragment a fragment, in whose scope ViewModels should be retained
 * @param factory  a {@code Factory} to instantiate new ViewModels
 * @return a ViewModelProvider instance
 */
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleViewModelFragment fragment, @Nullable ViewModelProvider.Factory factory) {
	Application application = checkApplication(checkActivity(fragment));
	if (factory == null) {
		factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
	}
	return new ViewModelProvider(fragment.getViewModelStore(), factory);
}
 
Example 15
Source File: LiveDataDetailFragmentSubComponent.java    From tv-samples with Apache License 2.0 votes vote down vote up
ViewModelProvider.Factory viewModelProviderFactory();