Permalink
Browse files

Added checkbox for disabling "replies to inbox" when replying to comm…

…ents (#543)

When replying to comments, it's now possible to disable that replies will be sent to the inbox. (Potential improvement: preference for default behaviour)

Reddit's API endpoint /sendreplies is used for this.
About the actual implementation, I'm not sure if it would be more elegant to move the actual call into CommentReplyActivity, but then we have to escalate the id of the newly created post, which would entail some refactoring.

On another note, Reddit's API responses surely have to be a joke, right? No sane person could do that...

Fixes #286
  • Loading branch information...
Clubfan22 authored and QuantumBadger committed Nov 5, 2017
1 parent 095de55 commit a684370187459169b8d2ef7076ad96a40843c29f
View
@@ -2,6 +2,7 @@
Fix for fullscreen video in internal browser (thanks to Clubfan22)
Preference to exclude text when sharing comment (thanks to Clubfan22)
Scroll to top when collapsing self post (thanks to ajgoda90)
Disable sending replies to inbox for comments (thanks to Clubfan22)
82/1.9.8.3
Fix for some Gfycat links (thanks to Mark Lee)
@@ -24,12 +24,16 @@
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.account.RedditAccountManager;
@@ -53,6 +57,9 @@
private Spinner usernameSpinner;
private EditText textEdit;
private CheckBox inboxReplies;
private boolean sendRepliesToInbox = true;
private String parentIdAndType = null;
@@ -91,8 +98,13 @@ protected void onCreate(Bundle savedInstanceState) {
final LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.comment_reply, null);
usernameSpinner = (Spinner)layout.findViewById(R.id.comment_reply_username);
inboxReplies = (CheckBox)layout.findViewById(R.id.comment_reply_inbox);
textEdit = (EditText)layout.findViewById(R.id.comment_reply_text);
if (mParentType == ParentType.COMMENT_OR_POST){
inboxReplies.setVisibility(View.VISIBLE);
}
if(intent != null && intent.hasExtra(PARENT_ID_AND_TYPE_KEY)) {
parentIdAndType = intent.getStringExtra(PARENT_ID_AND_TYPE_KEY);
@@ -250,6 +262,28 @@ public void run() {
}
};
final APIResponseHandler.ActionResponseHandler inboxHandler = new APIResponseHandler.ActionResponseHandler(this) {
@Override
protected void onSuccess() {
// Do nothing (result expected)
}
@Override
protected void onCallbackException(Throwable t) {
BugReportActivity.handleGlobalError(CommentReplyActivity.this, t);
}
@Override
protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t, Integer status, String readableMessage) {
Toast.makeText(context, getString(R.string.disable_replies_to_infobox_failed), Toast.LENGTH_SHORT).show();
}
@Override
protected void onFailure(final APIFailureType type) {
Toast.makeText(context, getString(R.string.disable_replies_to_infobox_failed), Toast.LENGTH_SHORT).show();
}
};
final CacheManager cm = CacheManager.getInstance(this);
final ArrayList<RedditAccount> accounts = RedditAccountManager.getInstance(this).getAccounts();
@@ -261,8 +295,12 @@ public void run() {
break;
}
}
RedditAPI.comment(cm, handler, selectedAccount, parentIdAndType, textEdit.getText().toString(), this);
if (mParentType == ParentType.COMMENT_OR_POST) {
sendRepliesToInbox = inboxReplies.isChecked();
} else {
sendRepliesToInbox = true;
}
RedditAPI.comment(cm, handler, inboxHandler, selectedAccount, parentIdAndType, textEdit.getText().toString(), sendRepliesToInbox, this);
progressDialog.show();
@@ -20,6 +20,7 @@
import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.activities.BugReportActivity;
import org.quantumbadger.redreader.cache.CacheManager;
@@ -36,6 +37,7 @@
import org.quantumbadger.redreader.reddit.things.RedditThing;
import org.quantumbadger.redreader.reddit.things.RedditUser;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URI;
@@ -179,9 +181,11 @@ protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t,
public static void comment(final CacheManager cm,
final APIResponseHandler.ActionResponseHandler responseHandler,
final APIResponseHandler.ActionResponseHandler inboxResponseHandler,
final RedditAccount user,
final String parentIdAndType,
final String markdown,
final boolean sendRepliesToInbox,
final Context context) {
final LinkedList<PostField> postFields = new LinkedList<>();
@@ -195,11 +199,17 @@ public void onJsonParseStarted(JsonValue result, long timestamp, UUID session, b
try {
final APIResponseHandler.APIFailureType failureType = findFailureType(result);
if(failureType != null) {
responseHandler.notifyFailure(failureType);
return;
}
// sending replies to inbox is the default behaviour
if (!sendRepliesToInbox) {
String commentFullname = findThingIdFromCommentResponse(result);
if (commentFullname != null && commentFullname.length() > 0) {
sendReplies(cm, inboxResponseHandler, user, commentFullname, false, context);
}
}
} catch(Throwable t) {
notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE, t, null, "JSON failed to parse");
@@ -503,6 +513,63 @@ public void onJsonParseStarted(final JsonValue result, final long timestamp, fin
});
}
public static void sendReplies(final CacheManager cm,
final APIResponseHandler.ActionResponseHandler responseHandler,
final RedditAccount user,
final String fullname,
final boolean state,
final Context context) {
final LinkedList<PostField> postFields = new LinkedList<>();
postFields.add(new PostField("id", fullname));
postFields.add(new PostField("state", String.valueOf(state)));
cm.makeRequest(new APIPostRequest(Constants.Reddit.getUri("/api/sendreplies"), user, postFields, context) {
@Override
public void onJsonParseStarted(JsonValue result, long timestamp, UUID session, boolean fromCache) {
try {
final APIResponseHandler.APIFailureType failureType = findFailureType(result);
if(failureType != null) {
responseHandler.notifyFailure(failureType);
return;
}
} catch(Throwable t) {
notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE, t, null, "JSON failed to parse");
}
responseHandler.notifySuccess();
}
@Override
protected void onCallbackException(Throwable t) {
BugReportActivity.handleGlobalError(context, t);
}
@Override
protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t, Integer status, String readableMessage) {
responseHandler.notifyFailure(type, t, status, readableMessage);
}
});
}
private static String findThingIdFromCommentResponse(final JsonValue response) {
// Returns either the correct value or null
try {
return "t1_" + response.asObject().getArray("jquery").getArray(30)
.getArray(3).getArray(0).getObject(0)
.getObject("data").getString("id");
} catch (NullPointerException e) {
// Do noting
} catch (InterruptedException e) {
// Do nothing
} catch (IOException e) {
// Do nothing
}
return null;
}
// lol, reddit api
private static APIResponseHandler.APIFailureType findFailureType(final JsonValue response) {
@@ -18,29 +18,40 @@
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner android:id="@+id/comment_reply_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<Spinner
android:id="@+id/comment_reply_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<EditText android:id="@+id/comment_reply_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:singleLine="false"
android:scrollHorizontally="false"
android:scrollbars="none"
android:inputType="textCapSentences|textMultiLine"/>
<CheckBox
android:id="@+id/comment_reply_inbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:checked="true"
android:visibility="gone"
android:text="@string/send_replies_to_inbox" />
<EditText
android:id="@+id/comment_reply_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:inputType="textCapSentences|textMultiLine"
android:scrollHorizontally="false"
android:scrollbars="none"
android:singleLine="false" />
<TextView
android:id="@+id/comment_parent_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textIsSelectable="true"/>
android:textIsSelectable="true" />
</LinearLayout>
@@ -763,4 +763,5 @@ Thanks to /u/balducien and /u/andiho for this translation!
<string name="collapsed_self_post">Text minimiert</string>
<string name="pref_behaviour_self_post_tap_actions_title">Auf Self-Post tippen</string>
<string name="pref_behaviour_comment_share_text_title">Text des Kommentars teilen</string>
<string name="disable_replies_to_infobox_failed">Das Abbestellen der Weiterleitung der Antworten in den Posteingang ist leider fehlgeschlagen.</string>
</resources>
@@ -1021,4 +1021,7 @@
<!-- 2017-10-22 -->
<string name="pref_behaviour_comment_share_text_key" translatable="false">pref_behaviour_comment_share_text</string>
<string name="pref_behaviour_comment_share_text_title">Include text when sharing comment</string>
<!-- 2017-11-01 -->
<string name="disable_replies_to_infobox_failed">Disabling replies to inbox failed</string>
</resources>

0 comments on commit a684370

Please sign in to comment.