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

View File

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

View File

@ -13,13 +13,13 @@ import java.util.Locale;
import retrofit2.Retrofit;
public class PostViewModel extends ViewModel {
private PostDataSource postDataSource;
private PostDataSourceFactory postDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<Post>> posts;
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(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@ -32,11 +32,10 @@ public class PostViewModel extends ViewModel {
.build();
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
postDataSource = postDataSourceFactory.getPostDataSource();
}
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(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@ -50,7 +49,6 @@ public class PostViewModel extends ViewModel {
.build();
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
postDataSource = postDataSourceFactory.getPostDataSource();
}
LiveData<PagedList<Post>> getPosts() {
@ -61,16 +59,20 @@ public class PostViewModel extends ViewModel {
return paginationNetworkState;
}
public LiveData<NetworkState> getInitialLoadingState() {
LiveData<NetworkState> getInitialLoadingState() {
return initialLoadingState;
}
void refresh() {
postDataSourceFactory.getPostDataSource().invalidate();
}
void retry() {
postDataSource.retry();
postDataSourceFactory.getPostDataSource().retry();
}
void retryLoadingMore() {
postDataSource.retryLoadingMore();
postDataSourceFactory.getPostDataSource().retryLoadingMore();
}
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:title="@string/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="always" />
app:showAsAction="never" />
</menu>