Fix force stop when navigating top-level comments. Fix invisible retry loading more posts button in dark theme. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-11-26 22:01:34 +08:00
parent a7edd207ad
commit b96552a5ca
7 changed files with 69 additions and 67 deletions

View File

@ -6,8 +6,8 @@ android {
applicationId "ml.docilealligator.infinityforreddit" applicationId "ml.docilealligator.infinityforreddit"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 29 targetSdkVersion 29
versionCode 22 versionCode 23
versionName "1.6.0" versionName "1.6.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {

View File

@ -258,7 +258,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override @Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == VIEW_TYPE_POST_DETAIL) { if (holder instanceof PostDetailViewHolder) {
((PostDetailViewHolder) holder).mTitleTextView.setText(mPost.getTitle()); ((PostDetailViewHolder) holder).mTitleTextView.setText(mPost.getTitle());
if (mPost.getSubredditNamePrefixed().startsWith("u/")) { if (mPost.getSubredditNamePrefixed().startsWith("u/")) {
if (mPost.getAuthorIconUrl() == null) { if (mPost.getAuthorIconUrl() == null) {
@ -583,7 +583,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
}); });
} }
}); });
} else if (holder.getItemViewType() == VIEW_TYPE_COMMENT) { } else if (holder instanceof CommentViewHolder) {
CommentData comment; CommentData comment;
if (mIsSingleCommentThreadMode) { if (mIsSingleCommentThreadMode) {
comment = mVisibleComments.get(holder.getAdapterPosition() - 2); comment = mVisibleComments.get(holder.getAdapterPosition() - 2);
@ -763,54 +763,48 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
return; return;
} }
int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1; int previousVoteType = comment.getVoteType();
int previousVoteType = mVisibleComments.get(commentPosition).getVoteType();
String newVoteType; String newVoteType;
((CommentViewHolder) holder).downVoteButton.clearColorFilter(); ((CommentViewHolder) holder).downVoteButton.clearColorFilter();
if (previousVoteType != CommentData.VOTE_TYPE_UPVOTE) { if (previousVoteType != CommentData.VOTE_TYPE_UPVOTE) {
//Not upvoted before //Not upvoted before
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_UPVOTE); comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE);
newVoteType = RedditUtils.DIR_UPVOTE; newVoteType = RedditUtils.DIR_UPVOTE;
((CommentViewHolder) holder).upVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).upVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.upvoted)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.upvoted));
} else { } else {
//Upvoted before //Upvoted before
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
newVoteType = RedditUtils.DIR_UNVOTE; newVoteType = RedditUtils.DIR_UNVOTE;
((CommentViewHolder) holder).upVoteButton.clearColorFilter(); ((CommentViewHolder) holder).upVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor));
} }
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mVisibleComments.get(commentPosition).getScore() + mVisibleComments.get(commentPosition).getVoteType())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) {
if (commentPosition < mVisibleComments.size()) { comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE);
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_UPVOTE);
}
((CommentViewHolder) holder).upVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).upVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.upvoted)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.upvoted));
} else { } else {
if (commentPosition < mVisibleComments.size()) { comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
}
((CommentViewHolder) holder).upVoteButton.clearColorFilter(); ((CommentViewHolder) holder).upVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor));
} }
((CommentViewHolder) holder).downVoteButton.clearColorFilter(); ((CommentViewHolder) holder).downVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mVisibleComments.get(commentPosition).getScore() + mVisibleComments.get(commentPosition).getVoteType())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType()));
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
} }
}, mVisibleComments.get(commentPosition).getFullName(), newVoteType, holder.getAdapterPosition()); }, comment.getFullName(), newVoteType, holder.getAdapterPosition());
}); });
((CommentViewHolder) holder).downVoteButton.setOnClickListener(view -> { ((CommentViewHolder) holder).downVoteButton.setOnClickListener(view -> {
@ -824,50 +818,48 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
return; return;
} }
int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1; int previousVoteType = comment.getVoteType();
int previousVoteType = mVisibleComments.get(commentPosition).getVoteType();
String newVoteType; String newVoteType;
((CommentViewHolder) holder).upVoteButton.clearColorFilter(); ((CommentViewHolder) holder).upVoteButton.clearColorFilter();
if (previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) { if (previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) {
//Not downvoted before //Not downvoted before
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE);
newVoteType = RedditUtils.DIR_DOWNVOTE; newVoteType = RedditUtils.DIR_DOWNVOTE;
((CommentViewHolder) holder).downVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.downvoted)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.downvoted));
} else { } else {
//Downvoted before //Downvoted before
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
newVoteType = RedditUtils.DIR_UNVOTE; newVoteType = RedditUtils.DIR_UNVOTE;
((CommentViewHolder) holder).downVoteButton.clearColorFilter(); ((CommentViewHolder) holder).downVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor));
} }
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mVisibleComments.get(commentPosition).getScore() + mVisibleComments.get(commentPosition).getVoteType())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position1) { public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) {
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE);
((CommentViewHolder) holder).downVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downVoteButton.setColorFilter(ContextCompat.getColor(mActivity, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.downvoted)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.downvoted));
} else { } else {
mVisibleComments.get(commentPosition).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE);
((CommentViewHolder) holder).downVoteButton.clearColorFilter(); ((CommentViewHolder) holder).downVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor)); ((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor));
} }
((CommentViewHolder) holder).upVoteButton.clearColorFilter(); ((CommentViewHolder) holder).upVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mVisibleComments.get(commentPosition).getScore() + mVisibleComments.get(commentPosition).getVoteType())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType()));
} }
@Override @Override
public void onVoteThingFail(int position1) { public void onVoteThingFail(int position1) {
} }
}, mVisibleComments.get(commentPosition).getFullName(), newVoteType, holder.getAdapterPosition()); }, comment.getFullName(), newVoteType, holder.getAdapterPosition());
}); });
if (comment.isSaved()) { if (comment.isSaved()) {
@ -916,11 +908,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> { ((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
if (mIsSingleCommentThreadMode) { intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getAuthor());
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(holder.getAdapterPosition() - 2).getAuthor());
} else {
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(holder.getAdapterPosition() - 1).getAuthor());
}
mActivity.startActivity(intent); mActivity.startActivity(intent);
}); });
@ -928,8 +916,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
try { try {
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain"); intent.setType("text/plain");
String extraText = mIsSingleCommentThreadMode ? mVisibleComments.get(holder.getAdapterPosition() - 2).getPermalink() String extraText = comment.getPermalink();
: mVisibleComments.get(holder.getAdapterPosition() - 1).getPermalink();
intent.putExtra(Intent.EXTRA_TEXT, extraText); intent.putExtra(Intent.EXTRA_TEXT, extraText);
mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share))); mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share)));
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
@ -940,7 +927,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((CommentViewHolder) holder).expandButton.setOnClickListener(view -> { ((CommentViewHolder) holder).expandButton.setOnClickListener(view -> {
if (((CommentViewHolder) holder).expandButton.getVisibility() == View.VISIBLE) { if (((CommentViewHolder) holder).expandButton.getVisibility() == View.VISIBLE) {
int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1; int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1;
if(commentPosition < mVisibleComments.size()) { if(commentPosition >= 0 && commentPosition < mVisibleComments.size()) {
if (mVisibleComments.get(commentPosition).isExpanded()) { if (mVisibleComments.get(commentPosition).isExpanded()) {
collapseChildren(commentPosition); collapseChildren(commentPosition);
((CommentViewHolder) holder).expandButton.setImageResource(R.drawable.ic_expand_more_grey_24dp); ((CommentViewHolder) holder).expandButton.setImageResource(R.drawable.ic_expand_more_grey_24dp);
@ -1419,20 +1406,22 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} }
public void deleteComment(int position) { public void deleteComment(int position) {
if (mVisibleComments.get(position).hasReply()) { if (mVisibleComments != null && position >= 0 && position < mVisibleComments.size()) {
mVisibleComments.get(position).setAuthor("[deleted]"); if (mVisibleComments.get(position).hasReply()) {
mVisibleComments.get(position).setCommentContent("[deleted]"); mVisibleComments.get(position).setAuthor("[deleted]");
if (mIsSingleCommentThreadMode) { mVisibleComments.get(position).setCommentContent("[deleted]");
notifyItemChanged(position + 2); if (mIsSingleCommentThreadMode) {
notifyItemChanged(position + 2);
} else {
notifyItemChanged(position + 1);
}
} else { } else {
notifyItemChanged(position + 1); mVisibleComments.remove(position);
} if (mIsSingleCommentThreadMode) {
} else { notifyItemRemoved(position + 2);
mVisibleComments.remove(position); } else {
if (mIsSingleCommentThreadMode) { notifyItemRemoved(position + 1);
notifyItemRemoved(position + 2); }
} else {
notifyItemRemoved(position + 1);
} }
} }
} }
@ -1448,7 +1437,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
public int getNextParentCommentPosition(int currentPosition) { public int getNextParentCommentPosition(int currentPosition) {
if (mVisibleComments != null && !mVisibleComments.isEmpty()) { if (mVisibleComments != null && !mVisibleComments.isEmpty()) {
if (mIsSingleCommentThreadMode) { if (mIsSingleCommentThreadMode) {
for (int i = currentPosition + 1; i - 2 < mVisibleComments.size(); i++) { for (int i = currentPosition + 1; i - 2 < mVisibleComments.size() && i - 2 >= 0; i++) {
if (mVisibleComments.get(i - 2).getDepth() == 0) { if (mVisibleComments.get(i - 2).getDepth() == 0) {
return i; return i;
} }

View File

@ -1372,7 +1372,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
boolean newExtraRow = hasExtraRow(); boolean newExtraRow = hasExtraRow();
if (previousExtraRow != newExtraRow) { if (previousExtraRow != newExtraRow) {
if (previousExtraRow) { if (previousExtraRow) {
notifyItemRemoved(super.getItemCount()); notifyItemRemoved(getItemCount() - 1);
} else { } else {
notifyItemInserted(super.getItemCount()); notifyItemInserted(super.getItemCount());
} }
@ -1381,6 +1381,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
} }
public void removeFooter() {
if (hasExtraRow()) {
notifyItemRemoved(getItemCount() - 1);
}
networkState = null;
}
@Override @Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof PostViewHolder) { if (holder instanceof PostViewHolder) {
@ -1609,7 +1617,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
ErrorViewHolder(View itemView) { ErrorViewHolder(View itemView) {
super(itemView); super(itemView);
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
errorTextView.setText(R.string.load_posts_error); errorTextView.setText(R.string.load_more_posts_error);
retryButton.setOnClickListener(view -> mCallback.retryLoadingMore()); retryButton.setOnClickListener(view -> mCallback.retryLoadingMore());
} }
} }

View File

@ -476,9 +476,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setAdapter(mAdapter);
mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class); mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> { mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
mAdapter.submitList(posts);
});
mPostViewModel.hasPost().observe(this, hasPost -> { mPostViewModel.hasPost().observe(this, hasPost -> {
this.hasPost = hasPost; this.hasPost = hasPost;
@ -542,14 +540,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void refresh() { public void refresh() {
mPostViewModel.refresh();
if (isInLazyMode) { if (isInLazyMode) {
stopLazyMode(); stopLazyMode();
} }
mAdapter.setNetworkState(null); mAdapter.removeFooter();
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
hasPost = false; hasPost = false;
mPostViewModel.refresh();
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {

View File

@ -196,9 +196,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
ArrayList<Post> singlePostList = new ArrayList<>(); ArrayList<Post> singlePostList = new ArrayList<>();
singlePostList.add(post); singlePostList.add(post);
callback.onResult(singlePostList, null, null);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(singlePostList, null, null);
} }
@Override @Override
@ -219,16 +219,17 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if (newPosts.size() != 0) { if (newPosts.size() != 0) {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
} else if (nextPageKey != null) { } else if (nextPageKey != null) {
loadBestPostsInitial(callback, nextPageKey); loadBestPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(false); hasPostLiveData.postValue(false);
} }
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, null, nextPageKey);
} }
@Override @Override
@ -275,8 +276,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) {
loadBestPostsAfter(params, callback, lastItem); loadBestPostsAfter(params, callback, lastItem);
} else { } else {
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
} }
@ -327,9 +328,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
ArrayList<Post> singlePostList = new ArrayList<>(); ArrayList<Post> singlePostList = new ArrayList<>();
singlePostList.add(post); singlePostList.add(post);
callback.onResult(singlePostList, null, null);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(singlePostList, null, null);
} }
@Override @Override
@ -350,16 +351,17 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if (newPosts.size() != 0) { if (newPosts.size() != 0) {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
} else if (nextPageKey != null) { } else if (nextPageKey != null) {
loadSubredditPostsInitial(callback, nextPageKey); loadSubredditPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(false); hasPostLiveData.postValue(false);
} }
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, null, nextPageKey);
} }
@Override @Override
@ -417,8 +419,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) {
loadSubredditPostsAfter(params, callback, lastItem); loadSubredditPostsAfter(params, callback, lastItem);
} else { } else {
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
} }
@ -476,16 +478,17 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if (newPosts.size() != 0) { if (newPosts.size() != 0) {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
} else if (nextPageKey != null) { } else if (nextPageKey != null) {
loadUserPostsInitial(callback, nextPageKey); loadUserPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(false); hasPostLiveData.postValue(false);
} }
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, null, nextPageKey);
} }
@Override @Override
@ -539,8 +542,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) {
loadUserPostsAfter(params, callback, lastItem); loadUserPostsAfter(params, callback, lastItem);
} else { } else {
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
} }
@ -620,16 +623,17 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if (newPosts.size() != 0) { if (newPosts.size() != 0) {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(true); hasPostLiveData.postValue(true);
} else if (nextPageKey != null) { } else if (nextPageKey != null) {
loadSearchPostsInitial(callback, nextPageKey); loadSearchPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(false); hasPostLiveData.postValue(false);
} }
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, null, nextPageKey);
} }
@Override @Override
@ -703,8 +707,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) {
loadSearchPostsAfter(params, callback, lastItem); loadSearchPostsAfter(params, callback, lastItem);
} else { } else {
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
} }

View File

@ -22,6 +22,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:backgroundTint="@color/backgroundColorPrimary"
android:textColor="@android:color/white"
android:text="@string/retry" android:text="@string/retry"
android:textSize="?attr/font_default" /> android:textSize="?attr/font_default" />

View File

@ -51,6 +51,7 @@
<string name="tap_to_retry">Error loading image. Tap to retry.</string> <string name="tap_to_retry">Error loading image. Tap to retry.</string>
<string name="load_posts_error">Error loading posts.\nTap to retry.</string> <string name="load_posts_error">Error loading posts.\nTap to retry.</string>
<string name="load_more_posts_error">Error loading posts.</string>
<string name="load_post_error">Error loading this post.\nTap to retry.</string> <string name="load_post_error">Error loading this post.\nTap to retry.</string>
<string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string> <string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string>
<string name="search_users_error">Error searching users.\nTap to retry.</string> <string name="search_users_error">Error searching users.\nTap to retry.</string>