aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java15
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java18
3 files changed, 17 insertions, 20 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
index 27c01fff..9ee9b399 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
@@ -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;
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index c4e10e45..fd7f3407 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -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() {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
index 4906bbad..e25e841f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
@@ -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 {