I'm having the following ViewModel:
public class FeedViewModel extends ViewModel {
private final FeedRepository repository;
private LiveData<Resource<List<Photo>>> feed;
@Inject
FeedViewModel(@NonNull final FeedRepository repository) {
this.repository = repository;
}
LiveData<Resource<List<Photo>>> getUserFeed() {
if (feed == null) {
feed = new MutableLiveData<>();
feed = repository.get();
}
return feed;
}
}
I observe feed in the Fragment this way:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel.getUserFeed().observe(this, feed -> {
switch (feed.status) {
case ERROR:
processErrorState(feed.data);
break;
case LOADING:
processLoadingState(feed.data);
break;
case SUCCESS:
if (feed.data != null)
processSuccessState(feed.data);
break;
}
});
}
The question is: how can I refresh feed the right way? Let's suppose that user triggered swipeToRefresh, so that event must create a refresh task of feed. How can I implement this?