blob: be524d516a9000be7db2242cb3b4925f8f9f778a [file] [log] [blame]
/*
* 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 android.arch.core.util.Function;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import com.example.android.persistence.db.DatabaseCreator;
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 static final MutableLiveData ABSENT = new MutableLiveData();
private final LiveData<ProductEntity> mObservableProduct;
private Observer<ProductEntity> mProductObserver;
public ObservableField<ProductEntity> product = new ObservableField<>();
{
//noinspection unchecked
ABSENT.setValue(null);
}
// Product exposed for data binding
//public final ObservableField<ProductEntity> product = new ObservableField<>();
private final int mProductId;
private final LiveData<List<CommentEntity>> mObservableComments;
public ProductViewModel(@NonNull Application application,
final int productId) {
super(application);
mProductId = productId;
final DatabaseCreator databaseCreator = DatabaseCreator.getInstance(this.getApplication());
mObservableComments = Transformations.switchMap(databaseCreator.isDatabaseCreated(), new Function<Boolean, LiveData<List<CommentEntity>>>() {
@Override
public LiveData<List<CommentEntity>> apply(Boolean isDbCreated) {
if (!isDbCreated) {
//noinspection unchecked
return ABSENT;
} else {
//noinspection ConstantConditions
return databaseCreator.getDatabase().commentDao().loadComments(mProductId);
}
}
});
mObservableProduct = Transformations.switchMap(databaseCreator.isDatabaseCreated(), new Function<Boolean, LiveData<ProductEntity>>() {
@Override
public LiveData<ProductEntity> apply(Boolean isDbCreated) {
if (!isDbCreated) {
//noinspection unchecked
return ABSENT;
} else {
//noinspection ConstantConditions
return databaseCreator.getDatabase().productDao().loadProduct(mProductId);
}
}
});
databaseCreator.createDb(this.getApplication());
}
/**
* Expose the LiveData Comments query so the UI can observe it.
*/
public LiveData<List<CommentEntity>> getComments() {
return mObservableComments;
}
public LiveData<ProductEntity> getObservableProduct() {
return mObservableProduct;
}
public void setProduct(ProductEntity product) {
this.product.set(product);
}
/**
* 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;
public Factory(@NonNull Application application, int productId) {
mApplication = application;
mProductId = productId;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
//noinspection unchecked
return (T) new ProductViewModel(mApplication, mProductId);
}
}
}