Skip to content

Commit

Permalink
Make some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed Jun 10, 2024
1 parent 1735251 commit d0fa51a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.gravatar.AvatarQueryOptions;
import com.gravatar.AvatarUrl;
Expand Down Expand Up @@ -138,6 +139,8 @@ public abstract class CommentDetailFragment extends ViewPagerFragment implements

@Nullable protected CommentDetailFragmentBinding mBinding = null;

private CommentDetailViewModel mViewModel;

private final OnActionClickListener mOnActionClickListener = new OnActionClickListener() {
@Override public void onEditCommentClicked() {
editComment();
Expand Down Expand Up @@ -168,7 +171,7 @@ public abstract class CommentDetailFragment extends ViewPagerFragment implements
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((WordPress) requireActivity().getApplication()).component().inject(this);

mViewModel = new ViewModelProvider(this).get(CommentDetailViewModel.class);
mCommentSource = (CommentSource) requireArguments().getSerializable(KEY_MODE);
setHasOptionsMenu(true);
}
Expand Down Expand Up @@ -687,33 +690,14 @@ protected void moderateComment(
// Fire the appropriate listener if we have one
if (note != null && mOnNoteCommentActionListener != null) {
mOnNoteCommentActionListener.onModerateCommentForNote(note, newStatus);
dispatchModerationAction(site, comment, newStatus);
mViewModel.dispatchModerationAction(site, comment, newStatus);
} else if (mOnCommentActionListener != null) {
mOnCommentActionListener.onModerateComment(comment, newStatus);
// Sad, but onModerateComment does the moderation itself (due to the undo bar), this should be refactored,
// That's why we don't call dispatchModerationAction() here.
}
}

private void dispatchModerationAction(
@NonNull SiteModel site,
@NonNull CommentModel comment,
CommentStatus newStatus
) {
if (newStatus == CommentStatus.DELETED) {
// For deletion, we need to dispatch a specific action.
mCommentsStoreAdapter.dispatch(
CommentActionBuilder.newDeleteCommentAction(new RemoteCommentPayload(site, comment))
);
} else {
// Actual moderation (push the modified comment).
comment.setStatus(newStatus.toString());
mCommentsStoreAdapter.dispatch(
CommentActionBuilder.newPushCommentAction(new RemoteCommentPayload(site, comment))
);
}
}

/*
* display the comment associated with the passed notification
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ class CommentDetailViewModel @Inject constructor(
comment.apply { this.status = status.toString() }
commentsStoreAdapter.dispatch(
if (status == CommentStatus.DELETED) {
// For deletion, we need to dispatch a specific action.
CommentActionBuilder.newDeleteCommentAction(CommentStore.RemoteCommentPayload(site, comment))
} else {
// Actual moderation (push the modified comment).
CommentActionBuilder.newPushCommentAction(CommentStore.RemoteCommentPayload(site, comment))
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ public void onModerateCommentForNote(@NonNull Note note, @NonNull CommentStatus
resultIntent.putExtra(NotificationsListFragment.NOTE_MODERATE_STATUS_EXTRA, newStatus.toString());

setResult(RESULT_OK, resultIntent);
finish();

if (newStatus == CommentStatus.DELETED) {
finish();
}
}

@SuppressWarnings("unused")
Expand Down

0 comments on commit d0fa51a

Please sign in to comment.