Refreshing, changing sort type and post filter are now working in PostFragment.

This commit is contained in:
Alex Ning 2021-09-06 21:02:06 +08:00
parent ba2c12d45d
commit 09e1711e78
4 changed files with 108 additions and 34 deletions

View File

@ -1329,18 +1329,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void refresh() { public void refresh() {
if (mPostViewModel != null) { mAdapter.removeFooter();
mAdapter.removeFooter(); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.GONE); hasPost = false;
hasPost = false; if (isInLazyMode) {
if (isInLazyMode) { stopLazyMode();
stopLazyMode();
}
saveCache();
mPostViewModel.refresh();
} else {
mSwipeRefreshLayout.setRefreshing(false);
} }
saveCache();
mAdapter.refresh();
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {

View File

@ -49,10 +49,11 @@ public class NewPostViewModel extends ViewModel {
Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce);
posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> {
paging3PagingSource.changeSortTypeAndPostFilter( repository.changeSortTypeAndPostFilter(
sortTypeLiveData.getValue(), postFilterLiveData.getValue()); sortTypeLiveData.getValue(), postFilterLiveData.getValue());
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); 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, public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName,
@ -72,10 +73,11 @@ public class NewPostViewModel extends ViewModel {
Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce);
posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> {
paging3PagingSource.changeSortTypeAndPostFilter( repository.changeSortTypeAndPostFilter(
sortTypeLiveData.getValue(), postFilterLiveData.getValue()); sortTypeLiveData.getValue(), postFilterLiveData.getValue());
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); 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, public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName,
@ -95,10 +97,11 @@ public class NewPostViewModel extends ViewModel {
Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce);
posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> {
paging3PagingSource.changeSortTypeAndPostFilter( repository.changeSortTypeAndPostFilter(
sortTypeLiveData.getValue(), postFilterLiveData.getValue()); sortTypeLiveData.getValue(), postFilterLiveData.getValue());
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); 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, public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName,
@ -119,10 +122,11 @@ public class NewPostViewModel extends ViewModel {
Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); Pager<String, Post> pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce);
posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> {
paging3PagingSource.changeSortTypeAndPostFilter( repository.changeSortTypeAndPostFilter(
sortTypeLiveData.getValue(), postFilterLiveData.getValue()); sortTypeLiveData.getValue(), postFilterLiveData.getValue());
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this));
}); });
//posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this));
} }
public LiveData<PagingData<Post>> getPosts() { public LiveData<PagingData<Post>> getPosts() {

View File

@ -157,6 +157,7 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource<String
@Nullable @Nullable
@Override @Override
public String getRefreshKey(@NonNull PagingState<String, Post> pagingState) { public String getRefreshKey(@NonNull PagingState<String, Post> pagingState) {
Log.i("asdfad", "asdf " + sortType.getType().value);
return null; return null;
} }
@ -385,10 +386,4 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource<String
return Futures.catching(partialLoadResultFuture, return Futures.catching(partialLoadResultFuture,
IOException.class, LoadResult.Error::new, executor); IOException.class, LoadResult.Error::new, executor);
} }
void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) {
Log.i("asdasfd", "s6 " + (postFilter == null));
this.sortType = sortType;
this.postFilter = postFilter;
}
} }

View File

@ -11,46 +11,125 @@ import ml.docilealligator.infinityforreddit.readpost.ReadPost;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class PostPaging3Repository { public class PostPaging3Repository {
Executor executor;
Retrofit retrofit;
String accessToken;
String accountName;
SharedPreferences sharedPreferences;
SharedPreferences postFeedScrolledPositionSharedPreferences;
int postType;
SortType sortType;
PostFilter postFilter;
List<ReadPost> readPostList;
String name;
String userWhere;
String query;
String trendingSource;
private PostPaging3PagingSource paging3PagingSource; private PostPaging3PagingSource paging3PagingSource;
private int type;
PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName,
SharedPreferences sharedPreferences, SharedPreferences sharedPreferences,
SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType,
SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) {
paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, type = 1;
sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, this.executor = executor;
postFilter, readPostList); 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, PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName,
SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences,
String name, int postType, SortType sortType, PostFilter postFilter, String name, int postType, SortType sortType, PostFilter postFilter,
List<ReadPost> readPostList) { List<ReadPost> readPostList) {
paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, type = 2;
sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, this.executor = executor;
sortType, postFilter, readPostList); 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, PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName,
SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences,
String name, int postType, SortType sortType, PostFilter postFilter, String name, int postType, SortType sortType, PostFilter postFilter,
String userWhere, List<ReadPost> readPostList) { String userWhere, List<ReadPost> readPostList) {
type = 3;
paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, this.executor = executor;
sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, this.retrofit = retrofit;
sortType, postFilter, userWhere, readPostList); 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, PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName,
SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences,
String name, String query, String trendingSource, int postType, SortType sortType, String name, String query, String trendingSource, int postType, SortType sortType,
PostFilter postFilter, List<ReadPost> readPostList) { PostFilter postFilter, List<ReadPost> readPostList) {
paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, type = 4;
sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, this.executor = executor;
postType, sortType, postFilter, readPostList); 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() { 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; return paging3PagingSource;
} }
void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) {
this.sortType = sortType;
this.postFilter = postFilter;
}
} }