From d56ecf52a5128268d0e54e2a456efab172603a2e Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 26 Jul 2026 20:43:51 -0400 Subject: [PATCH] Fix issues in saving comments using CommentMoreBottomSheetFragment in ViewPostDetailFragmentNew. --- .../activities/ViewPostDetailActivity.java | 9 ++- .../adapters/CommentsRecyclerViewAdapter.java | 4 +- .../CommentsRecyclerViewAdapterNew.java | 7 -- .../CommentMoreBottomSheetFragment.java | 38 +++++----- .../fragments/ViewPostDetailFragmentNew.java | 6 +- .../moderation/CommentModerationEvent.kt | 6 ++ .../ViewPostDetailFragmentViewModelNew.kt | 76 +++++++++++++++++++ 7 files changed, 110 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index a668fc30..d5a5833b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -70,7 +70,6 @@ import ml.docilealligator.infinityforreddit.post.PostType; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPostType; import ml.docilealligator.infinityforreddit.readpost.ReadPostsListInterface; -import ml.docilealligator.infinityforreddit.thing.SaveThing; import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.thing.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.user.UserProfileImagesBatchLoader; @@ -531,7 +530,11 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } public void saveComment(@NonNull Comment comment, int position) { - if (comment.isSaved()) { + ViewPostDetailFragmentNew fragment = mSectionsPagerAdapter.getCurrentFragment(); + if (fragment != null) { + fragment.toggleSaveComment(comment, position); + } + /*if (comment.isSaved()) { comment.setSaved(false); SaveThing.unsaveThing(mOauthRetrofit, accessToken, comment.getFullName(), new SaveThing.SaveThingListener() { @Override @@ -573,7 +576,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele Toast.makeText(ViewPostDetailActivity.this, R.string.comment_saved_failed, Toast.LENGTH_SHORT).show(); } }); - } + }*/ } public boolean toggleSearchPanelVisibility() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 58e73442..ff7af64b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -1518,9 +1518,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter= mDepthThreshold) { - bundle.putBoolean(CommentMoreBottomSheetFragment.EXTRA_SHOW_REPLY_AND_SAVE_OPTION, true); - } + bundle.putBoolean(CommentMoreBottomSheetFragment.EXTRA_SHOW_REPLY_OPTION, true); CommentMoreBottomSheetFragment commentMoreBottomSheetFragment = new CommentMoreBottomSheetFragment(); commentMoreBottomSheetFragment.setArguments(bundle); commentMoreBottomSheetFragment.show(mFragment.getChildFragmentManager(), commentMoreBottomSheetFragment.getTag()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java index 51f954bb..772a8d40 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapterNew.java @@ -745,13 +745,6 @@ public class CommentsRecyclerViewAdapterNew extends ListAdapter { @@ -146,23 +145,24 @@ public class CommentMoreBottomSheetFragment extends LandscapeExpandedRoundedBott dismiss(); }); } - binding.saveTextViewCommentMoreBottomSheetFragment.setVisibility(View.VISIBLE); - if (comment.isSaved()) { - binding.saveTextViewCommentMoreBottomSheetFragment.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(activity, R.drawable.ic_bookmark_day_night_24dp), null, null, null); - binding.saveTextViewCommentMoreBottomSheetFragment.setText(R.string.unsave_comment); - } else { - binding.saveTextViewCommentMoreBottomSheetFragment.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(activity, R.drawable.ic_bookmark_border_day_night_24dp), null, null, null); - binding.saveTextViewCommentMoreBottomSheetFragment.setText(R.string.save_comment); - } - - binding.saveTextViewCommentMoreBottomSheetFragment.setOnClickListener(view -> { - if (activity instanceof ViewPostDetailActivity) { - ((ViewPostDetailActivity) activity).saveComment(comment, bundle.getInt(EXTRA_POSITION)); - } - dismiss(); - }); } + binding.saveTextViewCommentMoreBottomSheetFragment.setVisibility(View.VISIBLE); + if (comment.isSaved()) { + binding.saveTextViewCommentMoreBottomSheetFragment.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(activity, R.drawable.ic_bookmark_day_night_24dp), null, null, null); + binding.saveTextViewCommentMoreBottomSheetFragment.setText(R.string.unsave_comment); + } else { + binding.saveTextViewCommentMoreBottomSheetFragment.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(activity, R.drawable.ic_bookmark_border_day_night_24dp), null, null, null); + binding.saveTextViewCommentMoreBottomSheetFragment.setText(R.string.save_comment); + } + + binding.saveTextViewCommentMoreBottomSheetFragment.setOnClickListener(view -> { + if (activity instanceof ViewPostDetailActivity) { + ((ViewPostDetailActivity) activity).saveComment(comment, bundle.getInt(EXTRA_POSITION)); + } + dismiss(); + }); + binding.shareTextViewCommentMoreBottomSheetFragment.setOnClickListener(view -> { dismiss(); try { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java index 260cc70e..676d1f95 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragmentNew.java @@ -865,10 +865,8 @@ public class ViewPostDetailFragmentNew extends Fragment implements FragmentCommu } } - public void saveComment(int position, boolean isSaved) { - if (mCommentsAdapter != null) { - mCommentsAdapter.setSaveComment(position, isSaved); - } + public void toggleSaveComment(@NonNull Comment comment, int position) { + viewPostDetailFragmentViewModel.toggleSaveComment(comment, position); } public void searchComment(String query, boolean searchNextComment) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/moderation/CommentModerationEvent.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/moderation/CommentModerationEvent.kt index a205b210..450f4e7e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/moderation/CommentModerationEvent.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/moderation/CommentModerationEvent.kt @@ -28,4 +28,10 @@ sealed class CommentModerationEvent(open val comment: Comment?, open val positio data class Deleted(override val comment: Comment, override val position: Int) : CommentModerationEvent(comment, position, R.string.delete_post_success) data class DeleteFailed(override val comment: Comment?, override val position: Int) : CommentModerationEvent(comment, position, R.string.delete_post_failed) + + data class Saved(override val comment: Comment, override val position: Int) : CommentModerationEvent(comment, position, R.string.comment_saved_success) + data class SaveFailed(override val comment: Comment, override val position: Int) : CommentModerationEvent(comment, position, R.string.comment_saved_failed) + + data class Unsaved(override val comment: Comment, override val position: Int) : CommentModerationEvent(comment, position, R.string.comment_unsaved_success) + data class UnsaveFailed(override val comment: Comment, override val position: Int) : CommentModerationEvent(comment, position, R.string.comment_unsaved_failed) } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt index 603eb5bc..90bacf0d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewPostDetailFragmentViewModelNew.kt @@ -1479,6 +1479,82 @@ class ViewPostDetailFragmentViewModelNew( } } + fun toggleSaveComment(comment: Comment, position: Int) { + viewModelScope.launch { + _dataState.value.comments?.let { + if (accessToken != null) { + val updatedComments = ArrayList(it) + + var oldComment: Comment? = it.getOrNull(position) + if (!oldComment?.id.equals(comment.id)) { + val currentPosition = findCommentPosition(comment.fullName, position) + if (currentPosition >= 0 && currentPosition < it.size) { + oldComment = it[currentPosition] + } + } + + oldComment?.let { + if (oldComment.isSaved) { + if (unsaveThing( + oauthRetrofit, accessToken, oldComment.fullName + )) { + val updatedComment = Comment(oldComment) + updatedComment.isSaved = !oldComment.isSaved + + updatedComments[position] = updatedComment + + _dataState.value = _dataState.value.copy( + comments = updatedComments + ) + + commentModerationEventLiveData.postValue( + CommentModerationEvent.Unsaved( + oldComment, + position + ) + ) + } else { + commentModerationEventLiveData.postValue( + CommentModerationEvent.UnsaveFailed( + oldComment, + position + ) + ) + } + } else { + if (saveThing( + oauthRetrofit, accessToken, oldComment.fullName + )) { + val updatedComment = Comment(oldComment) + updatedComment.isSaved = !oldComment.isSaved + + updatedComments[position] = updatedComment + + _dataState.value = _dataState.value.copy( + comments = updatedComments + ) + + commentModerationEventLiveData.postValue( + CommentModerationEvent.Saved( + oldComment, + position + ) + ) + } else { + commentModerationEventLiveData.postValue( + CommentModerationEvent.SaveFailed( + oldComment, + position + ) + ) + } + } + } + } + } + } + } + fun editComment(comment: Comment, position: Int) { _dataState.value.comments?.let { val updatedComments = ArrayList(it)