How can I change in ViewModel source of my LiveData from Room Dao. In WordDao I have two queries:
@Dao
public interface WordDao {
@Query("SELECT * FROM " + Word.TABLE_NAME + " ORDER BY text ASC")
LiveData<List<Word>> getWordsByAsc();
@Query("SELECT * FROM " + Word.TABLE_NAME + " ORDER BY text DESC")
LiveData<List<Word>> getWordsByDesc();
}
I have also Repository class:
public class WordRepository {
public LiveData<List<Word>> getWordsByAsc() {
return wordDao.getWordsByAsc();
}
public LiveData<List<Word>> getWordsByDesc() {
return wordDao.getWordsByDesc();
}
}
and my ViewModel class:
public class WordViewModel extends AndroidViewModel {
private boolean isSortAsc = true;
private LiveData<Word> words;
private WordRepository wordRepository;
public WordViewModel(@NonNull Application application) {
super(application);
wordRepository = new WordRepository(application);
words = wordRepository.getWordsByAsc();
}
public LiveData<List<Word>> getWords() {
return words;
}
public void sortButtonClicked() {
isSortAsc = !isSortAsc;
//TODO how change here source of data depending on the isSortAsc
//It works only after change configuration of screen, not insta
words = isSortAsc ? wordRepository.getWordsByAsc() :
wordRepository.getWordsByDesc()
}
}
In my Activity I have added observer:
wordViewModel.getWords().observe(this, new Observer<List<Word>>() {
@Override
public void onChanged(@Nullable List<Word> words) {
adapter.setWords(words);
}
});
In setWords(words) method I call also "notifyDataSetChanged()".
How change in the viewModel class source of LiveData depending on the "isSortAsc" parameter (words = isSortAsc ? wordRepository.getWordsByAsc() : wordRepository.getWordsByDesc()
It works only after change configuration of screen, not after insta change source for LiveData words