diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java index 28a44419..d9b02aa7 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java @@ -206,6 +206,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private Call subredditAutocompleteCall; private String mAccessToken; private String mAccountName; + private String mAccountQualifiedName; private String subredditName; private String description; @@ -344,6 +345,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); if (savedInstanceState == null) { mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); @@ -1038,7 +1040,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp }); CheckIsSubscribedToSubreddit.checkIsSubscribedToSubreddit(mExecutor, new Handler(), - mRedditDataRoomDatabase, subredditName, mAccountName, + mRedditDataRoomDatabase, qualifiedName, mAccountQualifiedName, new CheckIsSubscribedToSubreddit.CheckIsSubscribedToSubredditListener() { @Override public void isSubscribed() { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsRecyclerViewAdapter.java index a9492f0f..61f41ec0 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsRecyclerViewAdapter.java @@ -33,7 +33,10 @@ import com.bumptech.glide.request.RequestOptions; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.concurrent.Executor; import butterknife.BindView; @@ -97,6 +100,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter mVisibleComments; + + private Set loadedComments; private Locale mLocale; private RequestManager mGlide; private RecyclerView.RecycledViewPool recycledViewPool; @@ -211,6 +216,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter(); + loadedComments = new HashSet<>(); mLocale = locale; mSingleCommentId = singleCommentId; mIsSingleCommentThreadMode = isSingleCommentThreadMode; @@ -632,12 +638,19 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter trulyNewComments = new ArrayList<>(); + for (int i = 0; i < expandedComments.size(); i++) { + if (loadedComments.contains(expandedComments.get(i).getId()) || expandedComments.get(i).getDepth() != parentComment.getDepth() + 1) { + continue; + } + trulyNewComments.add(expandedComments.get(i)); + loadedComments.add(expandedComments.get(i).getId()); + } + mVisibleComments.addAll(placeholderPosition, trulyNewComments); if (mIsSingleCommentThreadMode) { - notifyItemRangeInserted(placeholderPosition + 1, expandedComments.size()); + notifyItemRangeInserted(placeholderPosition + 1, trulyNewComments.size()); } else { - notifyItemRangeInserted(placeholderPosition, expandedComments.size()); + notifyItemRangeInserted(placeholderPosition, trulyNewComments.size()); } } @@ -648,8 +661,15 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter trulyNewComments = new ArrayList<>(); + for (int i = 0; i < expandedComments.size(); i++) { + if (loadedComments.contains(expandedComments.get(i).getId()) || expandedComments.get(i).getDepth() != parentComment.getDepth() + 1) { + continue; + } + trulyNewComments.add(expandedComments.get(i)); + loadedComments.add(expandedComments.get(i).getId()); + } + mVisibleComments.get(parentPosition).addChildren(trulyNewComments); if (mIsSingleCommentThreadMode) { notifyItemChanged(parentPosition + 1); } else { @@ -668,6 +688,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { if (accountName == null) { @@ -19,7 +19,7 @@ public class CheckIsSubscribedToSubreddit { redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); } } - SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName == null ? "-" : accountName); + SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubredditByQualifiedName(communityQualifiedName, accountName == null ? "-" : accountName); handler.post(() -> { if (subscribedSubredditData != null) { checkIsSubscribedToSubredditListener.isSubscribed(); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java index e71bf13c..4f151f06 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/Comment.java @@ -311,12 +311,35 @@ public class Comment implements Parcelable { children = moreChildren; } else { if (children.size() > 1 && children.get(children.size() - 1).placeholderType == PLACEHOLDER_LOAD_MORE_COMMENTS) { - children.addAll(children.size() - 2, moreChildren); + for (int i = 0; i < moreChildren.size(); i++) { + boolean found = false; + for (int j = 0; j < children.size(); j++) { + if (children.get(j).id == moreChildren.get(i).id) { + found = true; + break; + } + } + if (!found) + children.add(moreChildren.get(i)); + } + } else { - children.addAll(moreChildren); + // Add only unique children + for (int i = 0; i < moreChildren.size(); i++) { + boolean found = false; + for (int j = 0; j < children.size(); j++) { + if (children.get(j).id == moreChildren.get(i).id) { + found = true; + break; + } + } + if (!found) { + children.add(moreChildren.get(i)); + } + } } } - childCount += moreChildren == null ? 0 : moreChildren.size(); + //childCount += moreChildren == null ? 0 : moreChildren.size(); assertChildrenDepth(); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java index 4e0584f1..d6c3d42c 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java @@ -30,7 +30,7 @@ public class FetchComment { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - ParseComment.parseComments(executor, handler, response.body(), + ParseComment.parseComments(executor, handler, response.body(), commentId, expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList topLevelComments, diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java index dfa0440d..d7f9f54b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -24,7 +24,7 @@ import eu.toldi.infinityforlemmy.utils.LemmyUtils; public class ParseComment { - public static void parseComments(Executor executor, Handler handler, String response, + public static void parseComments(Executor executor, Handler handler, String response, Integer commentId, boolean expandChildren, ParseCommentListener parseCommentListener) { executor.execute(() -> { @@ -48,8 +48,8 @@ public class ParseComment { topLevelComments.add(singleComment); } } - Comment parentComment = orderedComments.get(0); - if (parentComment.getDepth() == 0) + Comment parentComment = (commentId != null) ? parsedComments.get(commentId) : null; + if (parentComment != null && parentComment.getDepth() == 0) parentComment = null; for (int i = orderedComments.size() - 1; i >= 0; i--) { @@ -71,9 +71,9 @@ public class ParseComment { expandChildren(newComments, expandedNewComments, expandChildren); if (topLevelComments.isEmpty() && !parsedComments.isEmpty() && parentComment != null) { - for (int i = orderedComments.size() - 1; i >= 0; i--) { + for (int i = 0; i < orderedComments.size(); i++) { Comment c = orderedComments.get(i); - if (c.getDepth() > parentComment.getDepth()) + if (c.getParentId() == parentComment.getId()) expandedNewComments.add(c); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java index cea93a1f..9bed73cb 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java @@ -1312,7 +1312,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic if (mRespectSubredditRecommendedSortType) { fetchCommentsRespectRecommendedSort(false); } else { - ParseComment.parseComments(mExecutor, new Handler(), response.body(), + ParseComment.parseComments(mExecutor, new Handler(), response.body(), null, mExpandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, Integer parentId, ArrayList moreChildrenIds) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java index e36e12ee..7c0a06ea 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/subscribedsubreddit/SubscribedSubredditDao.java @@ -28,6 +28,9 @@ public interface SubscribedSubredditDao { @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1") SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); + @Query("SELECT * from subscribed_subreddits WHERE qualified_name = :qualified_name COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1") + SubscribedSubredditData getSubscribedSubredditByQualifiedName(String qualified_name, String accountName); + @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE") void deleteSubscribedSubreddit(String subredditName, String accountName); }