From f529bba550b63ff1ea0b254c308257f5fd0c25ed Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 30 Dec 2018 21:54:44 +0800 Subject: [PATCH] Refreshing posts is working now. --- .idea/caches/build_file_checksums.ser | Bin 533 -> 533 bytes .../PostDataSourceFactory.java | 15 ++++++--------- .../infinityforreddit/PostFragment.java | 4 +--- .../infinityforreddit/PostViewModel.java | 18 ++++++++++-------- .../res/menu/menu_view_subreddit_detail.xml | 10 ---------- .../menu/view_subreddit_detail_activity.xml | 2 +- 6 files changed, 18 insertions(+), 31 deletions(-) delete mode 100644 app/src/main/res/menu/menu_view_subreddit_detail.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index a8589e2b9e2a66fbd98e7f6916df77dacb325504..36e49c1771b06e378932e35f7456756a4383b487 100644 GIT binary patch delta 15 XcmbQrGL>b*43-~LmkDj0Q^^PbGNT4- delta 15 XcmbQrGL>b*43_Vq*JU=&sbmBIGBXBc 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 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 paginationNetworkState; private LiveData initialLoadingState; private LiveData> 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> getPosts() { @@ -61,16 +59,20 @@ public class PostViewModel extends ViewModel { return paginationNetworkState; } - public LiveData getInitialLoadingState() { + LiveData 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 { diff --git a/app/src/main/res/menu/menu_view_subreddit_detail.xml b/app/src/main/res/menu/menu_view_subreddit_detail.xml deleted file mode 100644 index 98b7f78c..00000000 --- a/app/src/main/res/menu/menu_view_subreddit_detail.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - diff --git a/app/src/main/res/menu/view_subreddit_detail_activity.xml b/app/src/main/res/menu/view_subreddit_detail_activity.xml index aa3537f5..d9b85107 100644 --- a/app/src/main/res/menu/view_subreddit_detail_activity.xml +++ b/app/src/main/res/menu/view_subreddit_detail_activity.xml @@ -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" /> \ No newline at end of file