From 72d811186f09aea6a6be93b5e4cf1d35083d2462 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 14 Jun 2019 11:58:52 +0800 Subject: Show a snackbar when sending comment and sending comment fails. Display the sent comment data after the comment is sent. --- .../infinityforreddit/CommentActivity.java | 37 +++++-- .../infinityforreddit/MainActivity.java | 2 +- .../infinityforreddit/ParseComment.java | 122 +++++++++++++++------ .../infinityforreddit/SendComment.java | 24 ++-- .../infinityforreddit/ViewPostDetailActivity.java | 16 ++- app/src/main/res/values/strings.xml | 2 + 6 files changed, 150 insertions(+), 53 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index 8f4a6958..bc2697da 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -24,9 +24,10 @@ import retrofit2.Retrofit; public class CommentActivity extends AppCompatActivity { - static final String COMMENT_PARENT_TEXT = "CPT"; - static final String PARENT_FULLNAME = "PF"; - static final String EXTRA_COMMENT_DATA = "ECD"; + static final String EXTRA_COMMENT_PARENT_TEXT_KEY = "ECPTK"; + static final String EXTRA_PARENT_FULLNAME_KEY = "EPFK"; + static final String EXTRA_COMMENT_DATA_KEY = "ECDK"; + static final String EXTRA_PARENT_DEPTH_KEY = "EPDK"; static final int WRITE_COMMENT_REQUEST_CODE = 1; @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @@ -35,6 +36,7 @@ public class CommentActivity extends AppCompatActivity { @BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText; private String parentFullname; + private int parentDepth; @Inject @Named("oauth") @@ -55,8 +57,10 @@ public class CommentActivity extends AppCompatActivity { setSupportActionBar(toolbar); - commentParentTextView.setText(getIntent().getExtras().getString(COMMENT_PARENT_TEXT)); - parentFullname = getIntent().getExtras().getString(PARENT_FULLNAME); + Intent intent = getIntent(); + commentParentTextView.setText(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); + parentFullname = intent.getExtras().getString(EXTRA_PARENT_FULLNAME_KEY); + parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY); } @Override @@ -72,22 +76,37 @@ public class CommentActivity extends AppCompatActivity { finish(); return true; case R.id.action_send_comment_activity: - CommentData commentData = null; - SendComment.sendComment(commentEditText.getText().toString(), parentFullname, mOauthRetrofit, + item.setEnabled(false); + item.getIcon().setAlpha(130); + Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE); + sendingSnackbar.show(); + + SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth, + getResources().getConfiguration().locale, mOauthRetrofit, sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""), new SendComment.SendCommentListener() { @Override - public void sendCommentSuccess() { + public void sendCommentSuccess(CommentData commentData) { Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_COMMENT_DATA, commentData); + returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData); setResult(RESULT_OK, returnIntent); finish(); } @Override public void sendCommentFailed() { + sendingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); } + + @Override + public void parseSentCommentFailed() { + Intent returnIntent = new Intent(); + setResult(RESULT_OK, returnIntent); + finish(); + } }); } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 57f44cc3..a099960b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -387,7 +387,7 @@ public class MainActivity extends AppCompatActivity { mInsertSuccess = false; loadUserData();*/ Intent intent = new Intent(this, CommentActivity.class); - intent.putExtra(CommentActivity.EXTRA_COMMENT_DATA, "asdfasdfas"); + intent.putExtra(CommentActivity.EXTRA_COMMENT_DATA_KEY, "asdfasdfas"); startActivity(intent); return true; case R.id.action_lazy_mode_main_activity: diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 78bc2581..fba61483 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -24,6 +24,11 @@ class ParseComment { void onParseMoreCommentBasicInfoFailed(); } + interface ParseSentCommentListener { + void onParseSentCommentSuccess(CommentData commentData); + void onParseSentCommentFailed(); + } + static void parseComment(String response, ArrayList commentData, Locale locale, boolean isPost, int parentDepth, ParseCommentListener parseCommentListener) { try { @@ -39,7 +44,9 @@ class ParseComment { new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); - Log.i("comment json error", e.getMessage()); + if(e.getMessage() != null) { + Log.i("comment json error", e.getMessage()); + } parseCommentListener.onParseCommentFailed(); } } @@ -55,11 +62,18 @@ class ParseComment { new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); - Log.i("comment json error", e.getMessage()); + if(e.getMessage() != null) { + Log.i("comment json error", e.getMessage()); + } parseCommentListener.onParseCommentFailed(); } } + static void parseSentComment(String response, int parentDepth, Locale locale, + ParseSentCommentListener parseSentCommentListener) { + new ParseSentCommentAsyncTask(response, parentDepth, locale, parseSentCommentListener).execute(); + } + private static class ParseCommentAsyncTask extends AsyncTask { private JSONArray comments; private ArrayList commentData; @@ -110,38 +124,13 @@ class ParseComment { for (int i = 0; i < actualCommentLength; i++) { JSONObject data = comments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - String id = data.getString(JSONUtils.ID_KEY); - String fullName = data.getString(JSONUtils.LINK_ID_KEY); - String author = data.getString(JSONUtils.AUTHOR_KEY); - boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY); - String commentContent = ""; - if(!data.isNull(JSONUtils.BODY_HTML_KEY)) { - commentContent = data.getString(JSONUtils.BODY_HTML_KEY).trim(); - } - String permalink = data.getString(JSONUtils.PERMALINK_KEY); - int score = data.getInt(JSONUtils.SCORE_KEY); - long submitTime = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; - boolean scoreHidden = data.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); - - Calendar submitTimeCalendar = Calendar.getInstance(); - submitTimeCalendar.setTimeInMillis(submitTime); - String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", - locale).format(submitTimeCalendar.getTime()); - - int depth; - if(data.has(JSONUtils.DEPTH_KEY)) { - depth = data.getInt(JSONUtils.DEPTH_KEY) + parentDepth; - } else { - depth = parentDepth; - } - boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY); - boolean hasReply = !(data.get(JSONUtils.REPLIES_KEY) instanceof String); - - newcommentData.add(new CommentData(id, fullName, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden)); + newcommentData.add(parseSingleComment(data, parentDepth, locale)); } } catch (JSONException e) { parseFailed = true; - Log.i("parse comment error", e.getMessage()); + if(e.getMessage() != null) { + Log.i("parse comment error", e.getMessage()); + } } return null; } @@ -201,4 +190,75 @@ class ParseComment { } } } + + private static class ParseSentCommentAsyncTask extends AsyncTask { + private String response; + private int parentDepth; + private Locale locale; + private ParseSentCommentListener parseSentCommentListener; + private boolean parseFailed; + private CommentData commentData; + + ParseSentCommentAsyncTask(String response, int parentDepth, Locale locale, ParseSentCommentListener parseSentCommentListener) { + this.response = response; + this.parentDepth = parentDepth; + this.locale = locale; + this.parseSentCommentListener = parseSentCommentListener; + parseFailed = false; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject sentCommentData = new JSONObject(response); + commentData = parseSingleComment(sentCommentData, parentDepth, locale); + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if(parseFailed) { + parseSentCommentListener.onParseSentCommentFailed(); + } else { + parseSentCommentListener.onParseSentCommentSuccess(commentData); + } + } + } + + private static CommentData parseSingleComment(JSONObject singleCommentData, int parentDepth, Locale locale) throws JSONException { + String id = singleCommentData.getString(JSONUtils.ID_KEY); + String fullName = singleCommentData.getString(JSONUtils.LINK_ID_KEY); + String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); + boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY); + String commentContent = ""; + if(!singleCommentData.isNull(JSONUtils.BODY_HTML_KEY)) { + commentContent = singleCommentData.getString(JSONUtils.BODY_HTML_KEY).trim(); + } + String permalink = singleCommentData.getString(JSONUtils.PERMALINK_KEY); + int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); + long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; + boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); + + Calendar submitTimeCalendar = Calendar.getInstance(); + submitTimeCalendar.setTimeInMillis(submitTime); + String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", + locale).format(submitTimeCalendar.getTime()); + + int depth; + if(singleCommentData.has(JSONUtils.DEPTH_KEY)) { + depth = singleCommentData.getInt(JSONUtils.DEPTH_KEY) + parentDepth; + } else { + depth = parentDepth; + } + boolean collapsed = singleCommentData.getBoolean(JSONUtils.COLLAPSED_KEY); + boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); + + return new CommentData(id, fullName, author, formattedSubmitTime, commentContent, score, + isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java index 5f7da2d7..18eac5cf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java @@ -1,10 +1,9 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import retrofit2.Call; @@ -15,12 +14,14 @@ import retrofit2.Retrofit; class SendComment { interface SendCommentListener { - void sendCommentSuccess(); + void sendCommentSuccess(CommentData commentData); void sendCommentFailed(); + void parseSentCommentFailed(); } - static void sendComment(String commentMarkdown, String thingFullname, Retrofit oauthRetrofit, - String accessToken, SendCommentListener sendCommentListener) { + static void sendComment(String commentMarkdown, String thingFullname, int parentDepth, + Locale locale, Retrofit oauthRetrofit, String accessToken, + SendCommentListener sendCommentListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); Map headers = RedditUtils.getOAuthHeader(accessToken); Map params = new HashMap<>(); @@ -35,8 +36,17 @@ class SendComment { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - Log.i("asdfasdfaf", response.body()); - sendCommentListener.sendCommentSuccess(); + ParseComment.parseSentComment(response.body(), parentDepth, locale, new ParseComment.ParseSentCommentListener() { + @Override + public void onParseSentCommentSuccess(CommentData commentData) { + sendCommentListener.sendCommentSuccess(commentData); + } + + @Override + public void onParseSentCommentFailed() { + + } + }); } else { sendCommentListener.sendCommentFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index ff9d3dd4..fc4a0fe5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -64,6 +64,7 @@ import retrofit2.Retrofit; import ru.noties.markwon.SpannableConfiguration; import ru.noties.markwon.view.MarkwonView; +import static ml.docilealligator.infinityforreddit.CommentActivity.EXTRA_COMMENT_DATA_KEY; import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT_REQUEST_CODE; public class ViewPostDetailActivity extends AppCompatActivity { @@ -684,8 +685,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { return true; case R.id.action_comment_view_post_detail_activity: Intent intent = new Intent(this, CommentActivity.class); - intent.putExtra(CommentActivity.COMMENT_PARENT_TEXT, mPost.getTitle()); - intent.putExtra(CommentActivity.PARENT_FULLNAME, mPost.getFullName()); + intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle()); + intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName()); + intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0); startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE); return true; case android.R.id.home: @@ -698,9 +700,13 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); - if(resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) { - /*CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA); - mAdapter.addComment(comment);*/ + if(data != null && resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) { + if(data.hasExtra(EXTRA_COMMENT_DATA_KEY)) { + CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA_KEY); + mAdapter.addComment(comment); + } else { + Toast.makeText(this, R.string.send_comment_failed, Toast.LENGTH_SHORT).show(); + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1b4968b0..c9f5b44d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,6 +72,8 @@ Lazy Mode stopped Your interesting thoughts here + Sending Could not send this comment + The comment is sent but unable to get the sent comment -- cgit v1.2.3