Refreshing posts is working now.

This commit is contained in:
Alex Ning 2018-12-30 21:54:44 +08:00
parent b7e1d92c7e
commit f529bba550
6 changed files with 18 additions and 31 deletions

View File

@ -12,6 +12,7 @@ class PostDataSourceFactory extends DataSource.Factory {
private String accessToken; private String accessToken;
private Locale locale; private Locale locale;
private String subredditName; private String subredditName;
private boolean isBestPost;
private PostDataSource postDataSource; private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData; private MutableLiveData<PostDataSource> postDataSourceLiveData;
@ -21,12 +22,7 @@ class PostDataSourceFactory extends DataSource.Factory {
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.isBestPost = isBestPost;
if(isBestPost) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost);
} else {
postDataSource = new PostDataSource(retrofit, locale, isBestPost, subredditName);
}
} }
PostDataSourceFactory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) { PostDataSourceFactory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
@ -34,16 +30,17 @@ class PostDataSourceFactory extends DataSource.Factory {
this.locale = locale; this.locale = locale;
this.subredditName = subredditName; this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.isBestPost = isBestPost;
}
@Override
public DataSource create() {
if(isBestPost) { if(isBestPost) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost); postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost);
} else { } else {
postDataSource = new PostDataSource(retrofit, locale, isBestPost, subredditName); postDataSource = new PostDataSource(retrofit, locale, isBestPost, subredditName);
} }
}
@Override
public DataSource create() {
postDataSourceLiveData.postValue(postDataSource); postDataSourceLiveData.postValue(postDataSource);
return postDataSource; return postDataSource;
} }

View File

@ -11,7 +11,6 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -137,7 +136,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
mPostViewModel.getPaginationNetworkState().observe(this, networkState -> { mPostViewModel.getPaginationNetworkState().observe(this, networkState -> {
Log.i("networkstate", networkState.getStatus().toString());
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });
@ -146,7 +144,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void refresh() { public void refresh() {
mPostViewModel.refresh();
} }
private void showErrorView() { private void showErrorView() {

View File

@ -13,13 +13,13 @@ import java.util.Locale;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class PostViewModel extends ViewModel { public class PostViewModel extends ViewModel {
private PostDataSource postDataSource; private PostDataSourceFactory postDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState; private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState; private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<Post>> posts; private LiveData<PagedList<Post>> posts;
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) { public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, isBestPost); postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, isBestPost);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData()); dataSource -> dataSource.getInitialLoadStateLiveData());
@ -32,11 +32,10 @@ public class PostViewModel extends ViewModel {
.build(); .build();
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
postDataSource = postDataSourceFactory.getPostDataSource();
} }
public PostViewModel(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) { public PostViewModel(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, isBestPost, subredditName); postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, isBestPost, subredditName);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData()); dataSource -> dataSource.getInitialLoadStateLiveData());
@ -50,7 +49,6 @@ public class PostViewModel extends ViewModel {
.build(); .build();
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
postDataSource = postDataSourceFactory.getPostDataSource();
} }
LiveData<PagedList<Post>> getPosts() { LiveData<PagedList<Post>> getPosts() {
@ -61,16 +59,20 @@ public class PostViewModel extends ViewModel {
return paginationNetworkState; return paginationNetworkState;
} }
public LiveData<NetworkState> getInitialLoadingState() { LiveData<NetworkState> getInitialLoadingState() {
return initialLoadingState; return initialLoadingState;
} }
void refresh() {
postDataSourceFactory.getPostDataSource().invalidate();
}
void retry() { void retry() {
postDataSource.retry(); postDataSourceFactory.getPostDataSource().retry();
} }
void retryLoadingMore() { void retryLoadingMore() {
postDataSource.retryLoadingMore(); postDataSourceFactory.getPostDataSource().retryLoadingMore();
} }
public static class Factory extends ViewModelProvider.NewInstanceFactory { public static class Factory extends ViewModelProvider.NewInstanceFactory {

View File

@ -1,10 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ml.docilealligator.infinityforreddit.ViewSubredditDetailActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

View File

@ -8,5 +8,5 @@
android:orderInCategory="1" android:orderInCategory="1"
android:title="@string/action_refresh" android:title="@string/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp" android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="always" /> app:showAsAction="never" />
</menu> </menu>