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

View File

@ -6,16 +6,13 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject; import javax.inject.Inject;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.LoadingMorePostsStatus;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -29,7 +26,7 @@ public class MorePostsInfoFragment extends Fragment {
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private FragmentMorePostsInfoBinding binding; private FragmentMorePostsInfoBinding binding;
private BaseActivity mActivity; private BaseActivity mActivity;
@Status @LoadingMorePostsStatus
int status; int status;
public MorePostsInfoFragment() { public MorePostsInfoFragment() {
@ -45,21 +42,21 @@ public class MorePostsInfoFragment extends Fragment {
applyTheme(); applyTheme();
setStatus(getArguments().getInt(EXTRA_STATUS, Status.LOADING)); setStatus(getArguments().getInt(EXTRA_STATUS, LoadingMorePostsStatus.LOADING));
return binding.getRoot(); return binding.getRoot();
} }
public void setStatus(@Status int status) { public void setStatus(@LoadingMorePostsStatus int status) {
this.status = status; this.status = status;
switch (status) { switch (status) {
case Status.LOADING: case LoadingMorePostsStatus.LOADING:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.loading); binding.infoTextViewMorePostsInfoFragment.setText(R.string.loading);
break; break;
case Status.FAILED: case LoadingMorePostsStatus.FAILED:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.load_more_posts_failed); binding.infoTextViewMorePostsInfoFragment.setText(R.string.load_more_posts_failed);
break; break;
case Status.NO_MORE_POSTS: case LoadingMorePostsStatus.NO_MORE_POSTS:
binding.infoTextViewMorePostsInfoFragment.setText(R.string.no_more_posts); binding.infoTextViewMorePostsInfoFragment.setText(R.string.no_more_posts);
} }
} }
@ -73,12 +70,4 @@ public class MorePostsInfoFragment extends Fragment {
super.onAttach(context); super.onAttach(context);
mActivity = (BaseActivity) 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") @Query("SELECT * FROM read_posts WHERE username = :username")
List<ReadPost> getAllReadPosts(String 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") @Query("SELECT COUNT(id) FROM read_posts")
int getReadPostsCount(); int getReadPostsCount();