content: /*
   * Copyright 2017, The Android Open Source Project
   *
   * 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
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.android.persistence.viewmodel;
  
  import android.app.Application;
  import androidx.lifecycle.AndroidViewModel;
  import androidx.lifecycle.LiveData;
  import androidx.lifecycle.ViewModel;
  import androidx.lifecycle.ViewModelProvider;
  import androidx.annotation.NonNull;
  
  import com.example.android.persistence.BasicApp;
  import com.example.android.persistence.DataRepository;
  import com.example.android.persistence.db.entity.CommentEntity;
  import com.example.android.persistence.db.entity.ProductEntity;
  
  import java.util.List;
  
  public class ProductViewModel extends AndroidViewModel {
  
      private final LiveData<ProductEntity> mObservableProduct;
  
      private final int mProductId;
  
      private final LiveData<List<CommentEntity>> mObservableComments;
  
      public ProductViewModel(@NonNull Application application, DataRepository repository,
              final int productId) {
          super(application);
          mProductId = productId;
  
          mObservableComments = repository.loadComments(mProductId);
          mObservableProduct = repository.loadProduct(mProductId);
      }
  
      /**
       * Expose the LiveData Comments query so the UI can observe it.
       */
      public LiveData<List<CommentEntity>> getComments() {
          return mObservableComments;
      }
  
      public LiveData<ProductEntity> getProduct() {
          return mObservableProduct;
      }
  
      /**
       * A creator is used to inject the product ID into the ViewModel
       * <p>
       * This creator is to showcase how to inject dependencies into ViewModels. It's not
       * actually necessary in this case, as the product ID can be passed in a public method.
       */
      public static class Factory extends ViewModelProvider.NewInstanceFactory {
  
          @NonNull
          private final Application mApplication;
  
          private final int mProductId;
  
          private final DataRepository mRepository;
  
          public Factory(@NonNull Application application, int productId) {
              mApplication = application;
              mProductId = productId;
              mRepository = ((BasicApp) application).getRepository();
          }
  
          @SuppressWarnings("unchecked")
          @Override
          @NonNull
          public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
              return (T) new ProductViewModel(mApplication, mRepository, mProductId);
          }
      }
  }
  