Fix problems when fetching more history posts in ViewPostDetailActivity.

This commit is contained in:
Docile-Alligator 2022-11-06 00:01:18 +11:00
parent fa472a3ad8
commit cf3595f22d
4 changed files with 69 additions and 55 deletions

View File

@ -0,0 +1,16 @@
package ml.docilealligator.infinityforreddit;
import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@IntDef({LoadingMorePostsStatus.LOADING, LoadingMorePostsStatus.FAILED, LoadingMorePostsStatus.NO_MORE_POSTS,
LoadingMorePostsStatus.NOT_LOADING})
@Retention(RetentionPolicy.SOURCE)
public @interface LoadingMorePostsStatus {
int LOADING = 0;
int FAILED = 1;
int NO_MORE_POSTS = 2;
int NOT_LOADING = 3;
}

View File

@ -61,6 +61,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.ActivityToolbarInterface;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.LoadingMorePostsStatus;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SaveThing;
@ -171,11 +172,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
@State
Post post;
@State
boolean isFetchingMorePosts;
@State
boolean noMorePosts;
@State
boolean fetchMorePostsFailed;
@LoadingMorePostsStatus
int loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING;
public Map<String, String> authorIcons = new HashMap<>();
private FragmentManager fragmentManager;
private SlidrInterface mSlidrInterface;
@ -511,12 +509,11 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
}
public void fetchMorePosts() {
if (isFetchingMorePosts || noMorePosts) {
if (loadingMorePostsStatus == LoadingMorePostsStatus.LOADING || loadingMorePostsStatus == LoadingMorePostsStatus.NO_MORE_POSTS) {
return;
}
isFetchingMorePosts = true;
fetchMorePostsFailed = false;
loadingMorePostsStatus = LoadingMorePostsStatus.LOADING;
Handler handler = new Handler(Looper.getMainLooper());
@ -642,10 +639,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, readPostList);
if (newPosts == null) {
handler.post(() -> {
noMorePosts = true;
loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS);
}
});
} else {
@ -654,45 +651,51 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
postLinkedHashSet.addAll(newPosts);
if (currentPostsSize == postLinkedHashSet.size()) {
handler.post(() -> {
noMorePosts = true;
loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS);
}
});
} else {
posts = new ArrayList<>(postLinkedHashSet);
handler.post(() -> sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize));
handler.post(() -> {
sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize);
loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(LoadingMorePostsStatus.NOT_LOADING);
}
});
}
}
} else {
handler.post(() -> {
fetchMorePostsFailed = true;
loadingMorePostsStatus = LoadingMorePostsStatus.FAILED;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.FAILED);
fragment.setStatus(LoadingMorePostsStatus.FAILED);
}
});
}
} catch (IOException e) {
e.printStackTrace();
handler.post(() -> {
fetchMorePostsFailed = true;
loadingMorePostsStatus = LoadingMorePostsStatus.FAILED;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.FAILED);
fragment.setStatus(LoadingMorePostsStatus.FAILED);
}
});
}
handler.post(() -> {
isFetchingMorePosts = false;
});
});
} else {
mExecutor.execute((Runnable) () -> {
long lastItem = posts.isEmpty() ? 0 : posts.get(posts.size() - 1).getPostTimeMillis();
List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(username, lastItem);
long lastItem = 0;
if (!posts.isEmpty()) {
lastItem = mRedditDataRoomDatabase.readPostDao().getReadPost(posts.get(posts.size() - 1).getId()).getTime();
}
List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(mAccountName, lastItem);
StringBuilder ids = new StringBuilder();
for (ReadPost readPost : readPosts) {
ids.append("t3_").append(readPost.getId()).append(",");
@ -715,10 +718,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, null);
if (newPosts == null || newPosts.isEmpty()) {
handler.post(() -> {
noMorePosts = true;
loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS);
}
});
} else {
@ -727,40 +730,43 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
postLinkedHashSet.addAll(newPosts);
if (currentPostsSize == postLinkedHashSet.size()) {
handler.post(() -> {
noMorePosts = true;
loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS);
}
});
} else {
posts = new ArrayList<>(postLinkedHashSet);
handler.post(() -> sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize));
handler.post(() -> {
sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize);
loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(LoadingMorePostsStatus.NOT_LOADING);
}
});
}
}
} else {
handler.post(() -> {
fetchMorePostsFailed = true;
loadingMorePostsStatus = LoadingMorePostsStatus.FAILED;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.FAILED);
}
});
}
} catch (IOException e) {
e.printStackTrace();
handler.post(() -> {
fetchMorePostsFailed = true;
loadingMorePostsStatus = LoadingMorePostsStatus.FAILED;
MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment();
if (fragment != null) {
fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS);
fragment.setStatus(LoadingMorePostsStatus.FAILED);
}
});
}
handler.post(() -> {
isFetchingMorePosts = false;
});
});
}
}
@ -936,7 +942,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
if (position >= posts.size()) {
MorePostsInfoFragment morePostsInfoFragment = new MorePostsInfoFragment();
Bundle moreBundle = new Bundle();
moreBundle.putInt(MorePostsInfoFragment.EXTRA_STATUS, MorePostsInfoFragment.Status.LOADING);
moreBundle.putInt(MorePostsInfoFragment.EXTRA_STATUS, loadingMorePostsStatus);
morePostsInfoFragment.setArguments(moreBundle);
return morePostsInfoFragment;
}

View File

@ -6,16 +6,13 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.LoadingMorePostsStatus;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -29,7 +26,7 @@ public class MorePostsInfoFragment extends Fragment {
CustomThemeWrapper mCustomThemeWrapper;
private FragmentMorePostsInfoBinding binding;
private BaseActivity mActivity;
@Status
@LoadingMorePostsStatus
int status;
public MorePostsInfoFragment() {
@ -45,21 +42,21 @@ public class MorePostsInfoFragment extends Fragment {
applyTheme();
setStatus(getArguments().getInt(EXTRA_STATUS, Status.LOADING));
setStatus(getArguments().getInt(EXTRA_STATUS, LoadingMorePostsStatus.LOADING));
return binding.getRoot();
}
public void setStatus(@Status int status) {
public void setStatus(@LoadingMorePostsStatus int status) {
this.status = status;
switch (status) {
case Status.LOADING:
case LoadingMorePostsStatus.LOADING:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.loading);
break;
case Status.FAILED:
case LoadingMorePostsStatus.FAILED:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.load_more_posts_failed);
break;
case Status.NO_MORE_POSTS:
case LoadingMorePostsStatus.NO_MORE_POSTS:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.no_more_posts);
}
}
@ -73,12 +70,4 @@ public class MorePostsInfoFragment extends Fragment {
super.onAttach(context);
mActivity = (BaseActivity) context;
}
@IntDef({Status.LOADING, Status.FAILED, Status.NO_MORE_POSTS})
@Retention(RetentionPolicy.SOURCE)
public @interface Status {
int LOADING = 0;
int FAILED = 1;
int NO_MORE_POSTS = 2;
}
}

View File

@ -23,6 +23,9 @@ public interface ReadPostDao {
@Query("SELECT * FROM read_posts WHERE username = :username")
List<ReadPost> getAllReadPosts(String username);
@Query("SELECT * FROM read_posts WHERE id = :id LIMIT 1")
ReadPost getReadPost(String id);
@Query("SELECT COUNT(id) FROM read_posts")
int getReadPostsCount();