Android LiveData Modification prob - android

I want to show a product list in my Android Application using Android new Architecture components. I have a Pojo class named Products like below:
public class Products {
private List<ProductDetails> products = null;
private Integer onSaleCount;
private Integer total;
private Integer page;
.........................
}
The webservice returns the list with pagination:
#GET("/products/")
Call<Products> getProducts(#Query("page") int page)
So each product list call, I am getting one set products list with other data.
I have already defined method to call the same in ProductListRepository:
public class ProductListRepository {
#Inject
ProductService mProductService;
public Products getProductList(int pageNo) {
.......................
..................
}
}
But I am facing problem to set the LiveData in ProductViewModel because I want to merge the List of ProductDetails with older one and also update the value of the other variables in Products like onSaleCount, pageNo.
public class ProductListViewModel extends ViewModel {
private final ProductListRepository mProductListRepository;
private MediatorLiveData<Products> mProducts;
public void getProductList(int pageNo) {
Products products = mProductListRepository.getProductList(pageNo);
if(products==null){
mProducts.setValue(products);
}else{
// **How to add old List and new List and set it to LiveData**
}
return mProducts;
}
}
Can anyone help me?

To just modify data, mProducts can be MutableLiveData<Products>, and your code like this:
public void getProductList(int pageNo) {
Products products = mProductListRepository.getProductList(pageNo);
if (products == null) {
// Nothing to do, no new data
} else {
Products oldProducts = mProducts.getValue();
// merge oldProducts and products into newProducts, as you prefer
mProducts.setValue(newProducts);
}
}
MediatorLiveData is designed to observe multiple LiveData sources.

Related

Observed LiveData doesn't really update after first fetch

I'm building an offline-first app with the database setup as the single source of truth. I am using Room to simplify the database handling, and LiveData to simplify observable data patterns.
I am also using Retrofit to make any network calls required to populate the database with new data.
I have set up an observer in my Fragment as follows:
private void setUpObserver() {
tfViewModel = ViewModelProviders.of(getActivity()).get(TFViewModel.class);
tfViewModel.getAllPosts().observe(getActivity(),
newPosts -> {
if (newPosts != null && newPosts.size() > 0) {
lottieAnimationView.setVisibility(View.INVISIBLE);
mPostsAdapter.updateItems(newPosts);
}
});
tfViewModel.fetchNextData(currentPage);
}
When my app first starts, I'm deleberately truncating each table in my database using Room callbacks so that new data is fetched every time. (For testing. This beats the offline-first experience and must not be done in production.)
Anyway, so when it first starts, it calls the fetchNextData method of the viewmodel which in turn asks the Repository to fetch the data.
Here's my ViewModel:
public class TFViewModel extends AndroidViewModel {
private TFRepository mRepository;
private LiveData<List<Post>> mPostList;
public TFViewModel(Application application) {
super(application);
mRepository = new TFRepository(application);
mPostList = mRepository.getAllPosts();
}
public LiveData<List<Post>> getAllPosts() {
return mPostList;
}
public void fetchNextData(int page) {
mRepository.fetchNextPosts(page);
}
}
In the repository, I use my DAOs to insert posts into the database. To fetch new data, I use a Service Class to fetch new data for me. When the fetch call returns, I use an AsyncTask to insert the new posts to my database. (Details omitted for brevity):
public class TFRepository {
private PostDao postDao;
private LiveData<List<Post>> postList;
private RetrofitSingleton retrofitSingleton;
public TFRepository(Application application) {
TFRoomDatabase db = TFRoomDatabase.getDatabase(application);
postDao = db.postDao();
retrofitSingleton = RetrofitSingleton.getInstance(application.getApplicationContext());
postList = postDao.getAllPosts();
}
public LiveData<List<Post>> getAllPosts() {
return postList;
}
public void fetchNextPosts(int page) {
getPostList(page);
}
private void getPostList(int page) {
APICaller.getInstance(retrofitSingleton).getFeed(page,
new NetworkResponseListener<BaseResponse<FeedResponse>>() {
#Override
public void onResponseReceived(BaseResponse<FeedResponse> feedResponseBaseResponse) {
if (feedResponseBaseResponse == null) return;
List<Post> posts = feedResponseBaseResponse.getData().getPosts();
new insertAllPostsAsyncTask(postDao).execute(posts);
}
#Override
public void onError(String errorMessage) {
}
});
}
}
The OBSERVER I had setup in my fragment gets an empty list the first time around. The API call returns with the first page of posts and it receives 10 posts the second time. The view is popualted. Everything is good.
Problem: As the user scrolls down, the Fragment asks the ViewModel to fetch more data. The ViewModel asks the Repository to fetch new data. The Retrofit call goes and comes back with the new data. It is inserted in the database. BUT THE OBSERVER IS NOT NOTIFIED. What am I missing?
NOTE: I do not want to use a MutableLiveData as I want to maintain the DB as the single source of truth. Also, as the docs state that LiveData is notified whenever the underlying DB changes, my implementation should work with LiveData.

Get data from firebase with Result of LiveData in viewmodel

I want to get 2 objects from firebase and I want to display combine results on the Recyclerview using viewmodel. I have created a viewmodel that gets the one object from firebase and based on id from this object I want to get the other object and update the ui.
some code of view model class
private static final DatabaseReference POST_REF =
FirebaseDatabase.getInstance().getReference("/Post");
private final FirebaseQueryLiveData liveData = new
FirebaseQueryLiveData(POST_REF);
#NonNull
public LiveData<DataSnapshot> getDataSnapshotLiveData() {
return liveData;
}
FirebaseQueryLiveData class
private final MyValueEventListener listener = new MyValueEventListener();
public FirebaseQueryLiveData(DatabaseReference ref) {
this.query = ref;
}
#Override
protected void onActive() {
Log.d(LOG_TAG, "onActive");
query.addValueEventListener(listener);
}
#Override
protected void onInactive() {
Log.d(LOG_TAG, "onInactive");
query.removeEventListener(listener);
}
How to get the second object from firebase using the result of first and notify the livedata about it
First object is post of users and second object is userinfo . I want to display combine result on Ui.
DataBase Structure I Want to get post and based on ids in post I want to get userdetails
I think step# 5 in this tutorial is the answer to your problem.
Here you can update UI once data in firebase changes.

How to make use of the new system architecture ViewModel/LiveData with cursorAdapter?

While learning the new android Architecture component’s ViewModel and LiveData, having a little confusion when observe the LiveData changing from database source change, and how this would work with Cursor adapter.
in https://developer.android.com/reference/android/widget/CursorAdapter.html, it says
int FLAG_REGISTER_CONTENT_OBSERVER
If set the adapter will register a content observer on the cursor
and will call onContentChanged() when a notification comes in. Be
careful when using this flag: you will need to unset the current
Cursor from the adapter to avoid leaks due to its registered
observers. This flag is not needed when using a CursorAdapter
with a CursorLoader.
so the with cursorAdaptor it has a way to get the ‘live update’ when the database data is updated.
is there a way to use the LiveData (to observe the database data update) with the cursorAdaptor?
trying to show the question of where to use the liveData updating the cursor in snippet below:
(with the sample of https://codelabs.developers.google.com/codelabs/android-persistence)
the Book:
#Entity
public class Book {
public #PrimaryKey String id;
public String title;
}
The ViewModel:
public class BooksBorrowedByUserViewModel extends AndroidViewModel {
public final LiveData<List<Book>> books;
private AppDatabase mDb;
public BooksBorrowedByUserViewModel(Application application) {
super(application);
createDb();
// Books is a LiveData object so updates are observed.
books = mDb.bookModel().findBooksBorrowedByName("Mike"); //<=== this ViewModel specific to one type query statement
}
public void createDb() {
mDb = AppDatabase.getInMemoryDatabase(this.getApplication());
// Populate it with initial data
DatabaseInitializer.populateAsync(mDb);
}
}
is this the way to use LiveData observer to force reload cursor?
private CursorAdapter listAdapter;
private BooksBorrowedByUserViewModel mViewModel;
private void subscribeUiBooks() {
mViewModel.books.observe(this, new Observer<List<Book>>() {
#Override
public void onChanged(#NonNull final List<Book> books) {
showBooksInUi(books, mBooksTextView); //<== the sample’s code
// if would like to update the cursorAdaptor
//
// ??? to requery the database and swap cursor here?
// Cursor data = queryData(buildSqlStatement()); // build the same sql statement as used in the BooksBorrowedByUserViewModel
// listAdapter.swapCursor(data)
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//having a list using CursorAdaptor
ListView list = getListView();
listAdapter = new CursorAdapter(getActivity(), null, 0)
list.setAdapter(listAdapter);
// Get a reference to the ViewModel for this screen.
mViewModel = ViewModelProviders.of(this).get(BooksBorrowedByUserViewModel.class);
subscribeUiBooks();
}
CursorAdapter is an old stuff, you should use Room + LiveData + RecyclerView.
Your data layer:
public LiveData<List<UserEntity>> getUsers() {
return userDao.getUsers();
}
Your activity:
viewModel.getUsers().observe(this, new Observer<List<UserEntity>>() {
#Override
public void onChanged(#Nullable List<UserEntity> users) {
if (users != null) {
adapter.setUsers(users);
}
}
});
In adapter:
private List<UserEntity> users = new ArrayList<>();
public void setUsers(List<UserEntity> users) {
this.users.clear();
this.users.addAll(users);
notifyDataSetChanged();
}
So when your activity lunch you should get live data from Room and subscribe to it. After that when you add something to that table, room automatically update observers, so you should just setup new data to the adapter and notify it.
Guide to App Architecture
LiveData
Room

Query nested Realm objects encapsulated in RealmList into RealmResults

I have the following RealmObject:
public class City extends RealmObject {
private String cityId;
private RealmList<Street> streets;
public String getId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public RealmList<Street> getStreets() {
return streets;
}
public void setStreets(RealmList<Street> streets) {
this.streets = streets;
}
}
Now having a cityId I need to query streets of particular city. How to do that? What I did try was:
Realm.getInstance(context).where(City.class).equalTo("cityId", someCityId, false)
.findFirst().getStreets().where().findAll()
But this leads into an Exception. I need to display streets in a ListView implementing filtering so I need streets to be RealmResults to use RealmBaseAdapter<Street>.
The proper way would be to have an open Realm instance either opened in your Activity in onCreate() and closed in onDestroy(), or in your custom application class.
Then you can use this realm instance to query the realm
City city = realm.where(City.class).equalTo("cityId", cityId).findFirst();
Then you can access the RealmList<T> like any other list
RealmList<Street> streets = city.getStreets();
Then you can use a recyclerview to get the view for a given index within your streets list.

Using a ForeignCollection

My entity contains the following private ForeignCollection attribute:
#ForeignCollectionField
private ForeignCollection<Order> orderCollection;
private List<Order> orderList;
What is the best way or usual way to avoid a having a caller use a ForeignCollection? Is there any neat way to return the Collections data to a caller?
How does the following method look? It allows a caller to access the data via a List. Would you recommend doing it this way?
public List<Order> getOrders() {
if (orderList == null) {
orderList = new ArrayList<Order>();
for (Order order : orderCollection) {
orderList.add(order);
}
}
return orderList;
}
If it's ok to change the signature to Collection rather than List, you could try using Collections.unmodifiableCollection().
public Collection<Order> getOrders()
{
return Collections.unmodifiableCollection(orderCollection);
}
Otherwise, your approach of using a lazy member variable is fine (provided you don't need synchronization). Also, note that you can just use the constructor of ArrayList to copy the values from the source collection:
orderList = new ArrayList<Order>(orderCollection);

Categories

Resources