From 09e1711e7851f86c193a7ad33d011b68490634cc Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 6 Sep 2021 21:02:06 +0800 Subject: Refreshing, changing sort type and post filter are now working in PostFragment. --- .../infinityforreddit/fragments/PostFragment.java | 18 ++-- .../infinityforreddit/post/NewPostViewModel.java | 12 ++- .../post/PostPaging3PagingSource.java | 7 +- .../post/PostPaging3Repository.java | 105 ++++++++++++++++++--- 4 files changed, 108 insertions(+), 34 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 59624b7f..f4ed088a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -1329,18 +1329,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void refresh() { - if (mPostViewModel != null) { - mAdapter.removeFooter(); - mFetchPostInfoLinearLayout.setVisibility(View.GONE); - hasPost = false; - if (isInLazyMode) { - stopLazyMode(); - } - saveCache(); - mPostViewModel.refresh(); - } else { - mSwipeRefreshLayout.setRefreshing(false); + mAdapter.removeFooter(); + mFetchPostInfoLinearLayout.setVisibility(View.GONE); + hasPost = false; + if (isInLazyMode) { + stopLazyMode(); } + saveCache(); + mAdapter.refresh(); } private void showErrorView(int stringResId) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java index 970f380c..9ef8828d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java @@ -49,10 +49,11 @@ public class NewPostViewModel extends ViewModel { Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - paging3PagingSource.changeSortTypeAndPostFilter( + repository.changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + //posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); } public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -72,10 +73,11 @@ public class NewPostViewModel extends ViewModel { Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - paging3PagingSource.changeSortTypeAndPostFilter( + repository.changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + //posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); } public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -95,10 +97,11 @@ public class NewPostViewModel extends ViewModel { Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - paging3PagingSource.changeSortTypeAndPostFilter( + repository.changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + //posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); } public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -119,10 +122,11 @@ public class NewPostViewModel extends ViewModel { Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - paging3PagingSource.changeSortTypeAndPostFilter( + repository.changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + //posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); } public LiveData> getPosts() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java index cde59067..dee6edd1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java @@ -157,6 +157,7 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource pagingState) { + Log.i("asdfad", "asdf " + sortType.getType().value); return null; } @@ -385,10 +386,4 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource readPostList; + String name; + String userWhere; + String query; + String trendingSource; + private PostPaging3PagingSource paging3PagingSource; + private int type; PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, - postFilter, readPostList); + type = 1; + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; } PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, readPostList); + type = 2; + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = name; } PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, String userWhere, List readPostList) { - - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, userWhere, readPostList); + type = 3; + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = name; + this.userWhere = userWhere; } PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, String query, String trendingSource, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, - postType, sortType, postFilter, readPostList); + type = 4; + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = name; + this.query = query; + this.trendingSource = trendingSource; } public PostPaging3PagingSource returnPagingSoruce() { + switch (type) { + case 1: + paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, + postFilter, readPostList); + break; + case 2: + paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, + sortType, postFilter, readPostList); + break; + case 3: + paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, + sortType, postFilter, userWhere, readPostList); + break; + default: + paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, + postType, sortType, postFilter, readPostList); + } return paging3PagingSource; } + + void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) { + this.sortType = sortType; + this.postFilter = postFilter; + } } -- cgit v1.2.3