Skip to content

Commit 5637910

Browse files
Enabling or disabling reply notifications in CommentMoreBottomSheetFragment.
1 parent 707aec6 commit 5637910

14 files changed

+190
-3
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ml.docilealligator.infinityforreddit;
2+
3+
import android.os.Handler;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
11+
import ml.docilealligator.infinityforreddit.comment.Comment;
12+
import ml.docilealligator.infinityforreddit.utils.APIUtils;
13+
import retrofit2.Call;
14+
import retrofit2.Callback;
15+
import retrofit2.Response;
16+
import retrofit2.Retrofit;
17+
18+
public class ReplyNotificationsToggle {
19+
public static void toggleEnableNotification(Handler handler, Retrofit oauthRetrofit, String accessToken,
20+
Comment comment, SendNotificationListener sendNotificationListener) {
21+
Map<String, String> params = new HashMap<>();
22+
params.put(APIUtils.ID_KEY, comment.getFullName());
23+
params.put(APIUtils.STATE_KEY, String.valueOf(!comment.isSendReplies()));
24+
oauthRetrofit.create(RedditAPI.class).toggleRepliesNotification(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
25+
@Override
26+
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
27+
if (response.isSuccessful()) {
28+
handler.post(sendNotificationListener::onSuccess);
29+
} else {
30+
handler.post(sendNotificationListener::onError);
31+
}
32+
}
33+
34+
@Override
35+
public void onFailure(@NonNull Call<String> call, @NonNull Throwable throwable) {
36+
handler.post(sendNotificationListener::onError);
37+
}
38+
});
39+
}
40+
41+
public interface SendNotificationListener {
42+
void onSuccess();
43+
void onError();
44+
}
45+
}

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ public void deleteComment(String fullName, int position) {
424424
}
425425
}
426426

427+
public void toggleReplyNotifications(Comment comment, int position) {
428+
if (sectionsPagerAdapter != null) {
429+
ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment();
430+
if (fragment != null) {
431+
fragment.toggleReplyNotifications(comment, position);
432+
}
433+
}
434+
}
435+
427436
public void saveComment(@NonNull Comment comment, int position) {
428437
if (comment.isSaved()) {
429438
comment.setSaved(false);

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575
import ml.docilealligator.infinityforreddit.account.Account;
7676
import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecyclerViewAdapter;
7777
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
78+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
7879
import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit;
7980
import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUser;
80-
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
8181
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment;
8282
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
8383
import ml.docilealligator.infinityforreddit.bottomsheetfragments.KarmaInfoBottomSheetFragment;
@@ -87,6 +87,7 @@
8787
import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTimeBottomSheetFragment;
8888
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
8989
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UserThingSortTypeBottomSheetFragment;
90+
import ml.docilealligator.infinityforreddit.comment.Comment;
9091
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
9192
import ml.docilealligator.infinityforreddit.customviews.NavigationWrapper;
9293
import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr;
@@ -1110,6 +1111,10 @@ public void deleteFailed() {
11101111
.show();
11111112
}
11121113

1114+
public void toggleReplyNotifications(Comment comment, int position) {
1115+
sectionsPagerAdapter.toggleCommentReplyNotification(comment, position);
1116+
}
1117+
11131118
@Override
11141119
public boolean onCreateOptionsMenu(Menu menu) {
11151120
getMenuInflater().inflate(R.menu.view_user_detail_activity, menu);
@@ -1737,6 +1742,18 @@ void filterPosts() {
17371742
}
17381743
}
17391744
}
1745+
1746+
void toggleCommentReplyNotification(Comment comment, int position) {
1747+
if (fragmentManager != null) {
1748+
Fragment fragment = fragmentManager.findFragmentByTag("f1");
1749+
if (fragment instanceof CommentsListingFragment) {
1750+
((CommentsListingFragment) fragment).toggleReplyNotifications(comment, position);
1751+
return;
1752+
}
1753+
}
1754+
1755+
Toast.makeText(ViewUserDetailActivity.this, R.string.cannot_find_comment, Toast.LENGTH_SHORT).show();
1756+
}
17401757
}
17411758

17421759
@Override

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ public void editComment(String commentContentMarkdown, int position) {
373373
}
374374
}
375375

376+
public void toggleReplyNotifications(int position) {
377+
Comment comment = getItem(position);
378+
if (comment != null) {
379+
comment.toggleSendReplies();
380+
notifyItemChanged(position);
381+
}
382+
}
383+
376384
public void setCanStartActivity(boolean canStartActivity) {
377385
this.canStartActivity = canStartActivity;
378386
}

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,15 @@ public void deleteComment(int position) {
10881088
}
10891089
}
10901090

1091+
public void toggleReplyNotifications(String fullName, int position) {
1092+
if (mVisibleComments != null && position >= 0 && position < mVisibleComments.size()) {
1093+
if (mVisibleComments.get(position).getFullName().equals(fullName)) {
1094+
mVisibleComments.get(position).toggleSendReplies();
1095+
}
1096+
}
1097+
//TODO The comment's position may change
1098+
}
1099+
10911100
public int getNextParentCommentPosition(int currentPosition) {
10921101
if (mVisibleComments != null && !mVisibleComments.isEmpty()) {
10931102
if (mIsSingleCommentThreadMode) {

app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,8 @@ Call<String> uploadSrImg(@HeaderMap Map<String, String> headers,
422422
@FormUrlEncoded
423423
@POST("/api/morechildren.json?raw_json=1&api_type=json")
424424
Call<String> moreChildrenOauth(@Field("link_id") String linkId, @Field("children") String children, @Field("sort") SortType.Type sort, @HeaderMap Map<String, String> headers);
425+
426+
@FormUrlEncoded
427+
@POST("/api/sendreplies")
428+
Call<String> toggleRepliesNotification(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
425429
}

app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
107107
}
108108
});
109109
}
110+
111+
if (comment.getAuthor().equals(activity.accountName)) {
112+
binding.notificationViewCommentMoreBottomSheetFragment.setVisibility(View.VISIBLE);
113+
binding.notificationViewCommentMoreBottomSheetFragment.setText(comment.isSendReplies() ? R.string.disable_reply_notifications : R.string.enable_reply_notifications);
114+
binding.notificationViewCommentMoreBottomSheetFragment.setOnClickListener(view -> {
115+
dismiss();
116+
if (activity instanceof ViewPostDetailActivity) {
117+
((ViewPostDetailActivity) activity).toggleReplyNotifications(comment, bundle.getInt(EXTRA_POSITION));
118+
} else if (activity instanceof ViewUserDetailActivity) {
119+
((ViewUserDetailActivity) activity).toggleReplyNotifications(comment, bundle.getInt(EXTRA_POSITION));
120+
}
121+
});
122+
}
110123
}
111124

112125
if (showReplyAndSaveOption) {

app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public Comment[] newArray(int size) {
5252
private boolean hasReply;
5353
private boolean scoreHidden;
5454
private boolean saved;
55+
private boolean sendReplies;
5556
private boolean isExpanded;
5657
private boolean hasExpandedBefore;
5758
private boolean isFilteredOut;
@@ -69,7 +70,7 @@ public Comment(String id, String fullName, String author, String authorFlair,
6970
String linkId, String subredditName, String parentId, int score,
7071
int voteType, boolean isSubmitter, String distinguished, String permalink,
7172
int depth, boolean collapsed, boolean hasReply,
72-
boolean scoreHidden, boolean saved, long edited, Map<String, MediaMetadata> mediaMetadataMap) {
73+
boolean scoreHidden, boolean saved, boolean sendReplies, long edited, Map<String, MediaMetadata> mediaMetadataMap) {
7374
this.id = id;
7475
this.fullName = fullName;
7576
this.author = author;
@@ -92,6 +93,7 @@ public Comment(String id, String fullName, String author, String authorFlair,
9293
this.hasReply = hasReply;
9394
this.scoreHidden = scoreHidden;
9495
this.saved = saved;
96+
this.sendReplies = sendReplies;
9597
this.isExpanded = false;
9698
this.hasExpandedBefore = false;
9799
this.editedTimeMillis = edited;
@@ -140,6 +142,8 @@ protected Comment(Parcel in) {
140142
collapsed = in.readByte() != 0;
141143
hasReply = in.readByte() != 0;
142144
scoreHidden = in.readByte() != 0;
145+
saved = in.readByte() != 0;
146+
sendReplies = in.readByte() != 0;
143147
isExpanded = in.readByte() != 0;
144148
hasExpandedBefore = in.readByte() != 0;
145149
isFilteredOut = in.readByte() != 0;
@@ -293,6 +297,14 @@ public void setSaved(boolean saved) {
293297
this.saved = saved;
294298
}
295299

300+
public boolean isSendReplies() {
301+
return sendReplies;
302+
}
303+
304+
public void toggleSendReplies() {
305+
sendReplies = !sendReplies;
306+
}
307+
296308
public boolean isExpanded() {
297309
return isExpanded;
298310
}
@@ -444,6 +456,8 @@ public void writeToParcel(Parcel parcel, int i) {
444456
parcel.writeByte((byte) (collapsed ? 1 : 0));
445457
parcel.writeByte((byte) (hasReply ? 1 : 0));
446458
parcel.writeByte((byte) (scoreHidden ? 1 : 0));
459+
parcel.writeByte((byte) (saved ? 1 : 0));
460+
parcel.writeByte((byte) (sendReplies ? 1 : 0));
447461
parcel.writeByte((byte) (isExpanded ? 1 : 0));
448462
parcel.writeByte((byte) (hasExpandedBefore ? 1 : 0));
449463
parcel.writeByte((byte) (isFilteredOut ? 1 : 0));

app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ static Comment parseSingleComment(JSONObject singleCommentData, int depth) throw
298298
long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
299299
boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY);
300300
boolean saved = singleCommentData.getBoolean(JSONUtils.SAVED_KEY);
301+
boolean sendReplies = singleCommentData.getBoolean(JSONUtils.SEND_REPLIES_KEY);
301302

302303
if (singleCommentData.has(JSONUtils.DEPTH_KEY)) {
303304
depth = singleCommentData.getInt(JSONUtils.DEPTH_KEY);
@@ -312,7 +313,7 @@ static Comment parseSingleComment(JSONObject singleCommentData, int depth) throw
312313
return new Comment(id, fullName, author, authorFlair, authorFlairHTMLBuilder.toString(),
313314
linkAuthor, submitTime, commentMarkdown, commentRawText,
314315
linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished,
315-
permalink, depth, collapsed, hasReply, scoreHidden, saved, edited,
316+
permalink, depth, collapsed, hasReply, scoreHidden, saved, sendReplies, edited,
316317
mediaMetadataMap);
317318
}
318319

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import android.os.Build;
1111
import android.os.Bundle;
1212
import android.os.Handler;
13+
import android.os.Looper;
1314
import android.view.HapticFeedbackConstants;
1415
import android.view.LayoutInflater;
1516
import android.view.View;
1617
import android.view.ViewGroup;
18+
import android.widget.Toast;
1719

1820
import androidx.annotation.NonNull;
1921
import androidx.core.content.res.ResourcesCompat;
@@ -39,10 +41,12 @@
3941
import ml.docilealligator.infinityforreddit.R;
4042
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
4143
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
44+
import ml.docilealligator.infinityforreddit.ReplyNotificationsToggle;
4245
import ml.docilealligator.infinityforreddit.SortType;
4346
import ml.docilealligator.infinityforreddit.account.Account;
4447
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
4548
import ml.docilealligator.infinityforreddit.adapters.CommentsListingRecyclerViewAdapter;
49+
import ml.docilealligator.infinityforreddit.comment.Comment;
4650
import ml.docilealligator.infinityforreddit.comment.CommentViewModel;
4751
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
4852
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
@@ -401,6 +405,24 @@ public void editComment(String commentMarkdown, int position) {
401405
}
402406
}
403407

408+
public void toggleReplyNotifications(Comment comment, int position) {
409+
ReplyNotificationsToggle.toggleEnableNotification(new Handler(Looper.getMainLooper()), mOauthRetrofit,
410+
mActivity.accessToken, comment, new ReplyNotificationsToggle.SendNotificationListener() {
411+
@Override
412+
public void onSuccess() {
413+
Toast.makeText(mActivity,
414+
comment.isSendReplies() ? R.string.reply_notifications_disabled : R.string.reply_notifications_enabled,
415+
Toast.LENGTH_SHORT).show();
416+
mAdapter.toggleReplyNotifications(position);
417+
}
418+
419+
@Override
420+
public void onError() {
421+
Toast.makeText(mActivity, R.string.toggle_reply_notifications_failed, Toast.LENGTH_SHORT).show();
422+
}
423+
});
424+
}
425+
404426
@Subscribe
405427
public void onChangeNetworkStatusEvent(ChangeNetworkStatusEvent changeNetworkStatusEvent) {
406428
if (mAdapter != null) {

0 commit comments

Comments
 (0)