private void initViewModels(final String productId) { productViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() { @SuppressWarnings("unchecked") @Override public <T extends ViewModel> T create(final Class<T> modelClass) { if (modelClass.equals(RealProductViewModel.class)) { return (T) new RealProductViewModel(productId); } else { return null; } } }).get(RealProductViewModel.class); productViewModel.productLiveData().observe(this, this::renderProduct); productViewModel.progressLiveData().observe(this, progress -> { if (progress != null) { swipeRefreshLayoutView.setRefreshing(progress.show); } }); productViewModel.errorErrorCallback().observe(this.getLifecycle(), error -> { if (error != null) { showDefaultErrorMessage(); } }); }
public static <T extends ViewModel> ViewModelProvider.Factory createFor(T model) { return new ViewModelProvider.Factory() { @Override public <T extends ViewModel> T create(Class<T> modelClass) { if (modelClass.isAssignableFrom(model.getClass())) { return (T) model; } throw new IllegalArgumentException("unexpected model class " + modelClass); } }; }
@NonNull private CountriesViewModel getViewModel() { CountriesService service = ApiClient.getCountriesService(); CountryDao moviesDao = CountriesDatabase.getInstance(this).countryDao(); ViewModelProvider.Factory factory = new CountriesViewModelProviderFactory(service, moviesDao); return ViewModelProviders.of(this, factory).get(CountriesViewModel.class); }
@PerFragment @Provides public LiveDataRowPresenter provideLiveDataRowPresenter(ViewModelProvider.Factory factory, DataLoadedListener activity) { LiveDataRowPresenter rowPresenter = new LiveDataRowPresenter(factory); rowPresenter.registerDataLoadedListener(activity); return rowPresenter; }
private void initViewModels(final String collectionId) { ProductListViewModel listViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() { @SuppressWarnings("unchecked") @Override public <T extends ViewModel> T create(final Class<T> modelClass) { if (modelClass.equals(ProductListViewModel.class)) { return (T) new ProductListViewModel(collectionId); } else { return null; } } }).get(ProductListViewModel.class); productListView.bindViewModel(listViewModel); }
@Override public void initViewModel(ViewModelProvider.Factory factory) { mViewModel = ViewModelProviders.of(this,factory).get(SoccerSeasonFragmentModel.class); }
@Override public void initViewModel(ViewModelProvider.Factory factory) { mViewModel = ViewModelProviders.of(this,factory).get(TeamFragmentModel.class); }
@Binds abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
@Binds abstract ViewModelProvider.Factory bindViewModelFactory(ArchCompViewModelFactory factory);
@Provides @Singleton ViewModelProvider.Factory provideViewModelFactory(ListItemRepository repository){ return new CustomViewModelFactory(repository); }
@Binds abstract ViewModelProvider.Factory bindViewModelFactory(GithubViewModelFactory factory);
@Provides @Singleton ViewModelProvider.Factory provideViewModelFactory() { return new ViewModelProvider.NewInstanceFactory(); }
@Provides @Singleton ViewModelProvider.Factory provideViewModelFactory(ViewModelSubComponent.Builder builder){ return new PhotoViewModelFactory(builder.build()); }
@Provides ViewModelProvider.Factory mainViewModelProvider(MainViewModel mainViewModel) { return new ViewModelProviderFactory<>(mainViewModel); }
@Provides ViewModelProvider.Factory provideBlogViewModel(BlogViewModel blogViewModel) { return new ViewModelProviderFactory<>(blogViewModel); }
@Provides ViewModelProvider.Factory provideOpenSourceViewModel(OpenSourceViewModel openSourceViewModel) { return new ViewModelProviderFactory<>(openSourceViewModel); }
@Singleton @Provides ViewModelProvider.Factory provideViewModelFactory(ViewModelSubComponent.Builder viewModelBuilder){ return new ViewModelFactory(viewModelBuilder.build()); }
@Binds @Singleton abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
public LiveDataRowPresenter(ViewModelProvider.Factory factory) { super(); mDataLoadedListeners = new ArrayList<>(); viewModelFactory = factory; }
private void initViewModels() { ShopSettings shopSettings = ((BaseApplication) getApplication()).shopSettings().getValue(); RealCartViewModel cartViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() { @Override public <T extends ViewModel> T create(final Class<T> modelClass) { if (modelClass.equals(RealCartViewModel.class)) { //noinspection unchecked return (T) new RealCartViewModel(shopSettings); } else { return null; } } }).get(RealCartViewModel.class); cartHeaderViewModel = cartViewModel; cartDetailsViewModel = cartViewModel; cartDetailsViewModel.webCheckoutCallback().observe(this.getLifecycle(), checkout -> { if (checkout != null) { onWebCheckoutConfirmation(checkout); } }); cartDetailsViewModel.androidPayStartCheckoutCallback().observe(this.getLifecycle(), payCart -> { if (cartHeaderViewModel.googleApiClientConnectionData().getValue() == Boolean.TRUE && payCart != null) { PayHelper.requestMaskedWallet(googleApiClient, payCart, BuildConfig.ANDROID_PAY_PUBLIC_KEY); } }); cartDetailsViewModel.androidPayCheckoutCallback().observe(this.getLifecycle(), confirmation -> { if (confirmation != null) { ScreenRouter.route(this, new AndroidPayConfirmationClickActionEvent(confirmation.checkoutId, confirmation.payCart, confirmation.maskedWallet)); } }); cartDetailsViewModel.progressLiveData().observe(this, progress -> { if (progress != null) { if (progress.show) { showProgress(progress.requestId); } else { hideProgress(progress.requestId); } } }); cartDetailsViewModel.errorErrorCallback().observe(this.getLifecycle(), error -> { if (error != null) { showError(error.requestId, error.t, error.message); } }); cartHeaderView.bindViewModel(cartHeaderViewModel); CartListViewModel cartListViewModel = ViewModelProviders.of(this).get(CartListViewModel.class); cartListView.bindViewModel(cartListViewModel); }
private void initViewModels() { RealCheckoutViewModel realCheckoutViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() { @SuppressWarnings("unchecked") @Override public <T extends ViewModel> T create(final Class<T> modelClass) { if (modelClass.equals(RealCheckoutViewModel.class)) { return (T) new RealCheckoutViewModel(checkoutId, payCart, maskedWallet); } else { return null; } } }).get(RealCheckoutViewModel.class); realCheckoutViewModel.progressLiveData().observe(this, progress -> { if (progress != null) { if (progress.show) { showProgress(progress.requestId); } else { hideProgress(progress.requestId); } } }); realCheckoutViewModel.errorErrorCallback().observe(this.getLifecycle(), error -> { if (error != null) { showError(error.requestId, error.t, error.message); } }); realCheckoutViewModel.payCartLiveData().observe(this, payCart -> { if (payCart == null) { totalSummaryView.render(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); } else { totalSummaryView.render(payCart.subtotal, payCart.shippingPrice != null ? payCart.shippingPrice : BigDecimal.ZERO, payCart.taxPrice != null ? payCart.taxPrice : BigDecimal.ZERO, payCart.totalPrice); } }); realCheckoutViewModel.maskedWalletLiveData().observe(this, maskedWallet -> { if (maskedWallet != null) { updateMaskedWallet(maskedWallet); } }); realCheckoutViewModel.successPaymentLiveData().observe(this, payment -> { if (payment != null) { showCheckoutSuccessMessage(); } }); checkoutViewModel = realCheckoutViewModel; shippingRatesView.bindViewModel(realCheckoutViewModel); }
public abstract void initViewModel(ViewModelProvider.Factory factory);
ViewModelProvider.Factory viewModelProviderFactory();