Reputation: 771
I have this view model
public class MainViewModel extends AndroidViewModel {
// Constant for logging
private static final String TAG = MainViewModel.class.getSimpleName();
private LiveData<List<JournalEntry>> journals;
public MainViewModel(Application application) {
super(application);
AppDatabase database = AppDatabase.getInstance(this.getApplication());
Log.d(TAG, "Actively retrieving the tasks from the DataBase");
journals = database.journalDao().loadAllJournals();
}
public LiveData<List<JournalEntry>> getJournals() {
return journals;
}
}
which returns a LiveData<List<JournalEntry>>
I would like to transform the live data to return a LiveData<List<ListItem>>
where the ListItem list contains JournalEntry objects, and DateHeader Objects
I had tried to manipulate the observing list like this
private void setupViewModel() {
MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
viewModel.getJournals().observe(this, new Observer<List<JournalEntry>>() {
@Override
public void onChanged(@Nullable List<JournalEntry> journalEntries) {
Log.d(TAG, "Updating list of tasks from LiveData in ViewModel");
Map<Date, List<JournalEntry>> journals = toMap(journalEntries);
Date previousDate = null;
for (Date date : journals.keySet()) {
HeaderItem header = new HeaderItem(date);
Date currentDate = header.getDate();
if(previousDate==null || !DateUtil.formatDate(currentDate).equals(DateUtil.formatDate(previousDate))){
items.add(header);
}
for (JournalEntry journal : journals.get(date)) {
JournalItem item = new JournalItem(journal);
items.add(item);
previousDate = item.getJournalItem().getCreatedAt();
}
}
mAdapter.setItems(items);
}
});
}
But realized the view model is duplicating all the items onChange instead of just updating the ones that have changed. I am not quite sure how I can achieve this using LiveData transfromation
Thanks in Advance
Upvotes: 2
Views: 6185
Reputation: 771
I got what i wanted with MediatorLiveData, then clearing my list ItemList with each onChanged. I set MediatorLiveData value to my new list of ItemList, and observe it
private void setupViewModel() {
MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
final LiveData<List<JournalEntry>> liveItems = viewModel.getJournals();
final MediatorLiveData itemListData = new MediatorLiveData<>();
itemListData.addSource(liveItems, new Observer<List<JournalEntry>>() {
@Override public void onChanged(List<JournalEntry> journalEntries) {
Map<Date, List<JournalEntry>> journals = toMap(journalEntries);
Date previousDate = null;
items.clear();
for (Date date : journals.keySet()) {
HeaderItem header = new HeaderItem(date);
Date currentDate = header.getDate();
if (previousDate == null || !DateUtil.formatDate(currentDate).equals(DateUtil.formatDate(previousDate))) {
items.add(header);
}
for (JournalEntry journal : journals.get(date)) {
JournalItem item = new JournalItem(journal);
items.add(item);
previousDate = item.getJournalItem().getCreatedAt();
}
}
itemListData.setValue(items);
}
});
itemListData.observe(this, new Observer<List<ListItem>>() {
@Override
public void onChanged(@Nullable List<ListItem> journalEntries) {
Log.d(TAG, "Updating list of tasks from LiveData in ViewModel");
mAdapter.setItems(journalEntries);
}
});
}
Upvotes: 1