From 8b0aed58b21b4ba5f62b4785b145d1b95ff3965e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 22 Jul 2021 19:26:36 +0800 Subject: [PATCH] Directly submit post in FullMarkdownActivity. --- .../activities/CommentActivity.java | 1 + .../activities/EditCommentActivity.java | 1 + .../activities/EditPostActivity.java | 1 + .../activities/FullMarkdownActivity.java | 18 ++++ .../activities/PostTextActivity.java | 85 ++++++++++--------- .../main/res/menu/full_markdown_activity.xml | 10 +++ 6 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 app/src/main/res/menu/full_markdown_activity.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index a7e675b2..6d9abd84 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -348,6 +348,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA } else if (itemId == R.id.action_preview_comment_activity) { Intent intent = new Intent(this, FullMarkdownActivity.class); intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, commentEditText.getText().toString()); + intent.putExtra(FullMarkdownActivity.EXTRA_SUBMIT_POST, true); startActivity(intent); } else if (itemId == R.id.action_send_comment_activity) { if (!isSubmitting) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java index 8ebb0015..3e69dd9a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java @@ -206,6 +206,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab if (item.getItemId() == R.id.action_preview_edit_comment_activity) { Intent intent = new Intent(this, FullMarkdownActivity.class); intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); + intent.putExtra(FullMarkdownActivity.EXTRA_SUBMIT_POST, true); startActivity(intent); } else if (item.getItemId() == R.id.action_send_edit_comment_activity) { if (!isSubmitting) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java index 50574e15..e17e8753 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java @@ -218,6 +218,7 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled if (item.getItemId() == R.id.action_preview_edit_post_activity) { Intent intent = new Intent(this, FullMarkdownActivity.class); intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); + intent.putExtra(FullMarkdownActivity.EXTRA_SUBMIT_POST, true); startActivity(intent); } else if (item.getItemId() == R.id.action_send_edit_post_activity) { if (!isSubmitting) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java index 312fe6cd..117adf81 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; @@ -11,6 +12,7 @@ import android.text.Spanned; import android.text.TextPaint; import android.text.style.ClickableSpan; import android.text.util.Linkify; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; @@ -58,6 +60,7 @@ public class FullMarkdownActivity extends BaseActivity { public static final String EXTRA_COMMENT_MARKDOWN = "ECM"; public static final String EXTRA_IS_NSFW = "EIN"; + public static final String EXTRA_SUBMIT_POST = "ESP"; @BindView(R.id.coordinator_layout_comment_full_markdown_activity) CoordinatorLayout coordinatorLayout; @@ -233,11 +236,26 @@ public class FullMarkdownActivity extends BaseActivity { markwonAdapter.notifyDataSetChanged(); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if (getIntent().getBooleanExtra(EXTRA_SUBMIT_POST, false)) { + getMenuInflater().inflate(R.menu.full_markdown_activity, menu); + return true; + } + + return false; + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; + } else if (item.getItemId() == R.id.action_send_full_markdown_activity) { + Intent returnIntent = new Intent(); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + return true; } return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index cf0758f7..78009c94 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -91,6 +91,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int PICK_IMAGE_REQUEST_CODE = 100; private static final int CAPTURE_IMAGE_REQUEST_CODE = 200; + private static final int MARKDOWN_PREVIEW_REQUEST_CODE = 300; @BindView(R.id.coordinator_layout_post_text_activity) CoordinatorLayout coordinatorLayout; @@ -474,51 +475,57 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr } else if (itemId == R.id.action_preview_post_text_activity) { Intent intent = new Intent(this, FullMarkdownActivity.class); intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); - startActivity(intent); + intent.putExtra(FullMarkdownActivity.EXTRA_SUBMIT_POST, true); + startActivityForResult(intent, MARKDOWN_PREVIEW_REQUEST_CODE); } else if (itemId == R.id.action_send_post_text_activity) { - if (!subredditSelected) { - Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); - return true; - } - - if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); - return true; - } - - isPosting = true; - - item.setEnabled(false); - item.getIcon().setAlpha(130); - - mPostingSnackbar.show(); - - String subredditName; - if (subredditIsUser) { - subredditName = "u_" + subredditNameTextView.getText().toString(); - } else { - subredditName = subredditNameTextView.getText().toString(); - } - - Intent intent = new Intent(this, SubmitPostService.class); - intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); - intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); - intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); - intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, receivePostReplyNotificationsSwitchMaterial.isChecked()); - intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); - ContextCompat.startForegroundService(this, intent); - + submitPost(item); return true; } return false; } + private void submitPost(MenuItem item) { + if (!subredditSelected) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + return; + } + + if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + return; + } + + isPosting = true; + + if (item != null) { + item.setEnabled(false); + item.getIcon().setAlpha(130); + } + + mPostingSnackbar.show(); + + String subredditName; + if (subredditIsUser) { + subredditName = "u_" + subredditNameTextView.getText().toString(); + } else { + subredditName = subredditNameTextView.getText().toString(); + } + + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, receivePostReplyNotificationsSwitchMaterial.isChecked()); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + ContextCompat.startForegroundService(this, intent); + } + @Override public void onBackPressed() { if (isPosting) { @@ -577,6 +584,8 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr } else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) { Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, contentEditText, coordinatorLayout, capturedImageUri, uploadedImages); + } else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) { + submitPost(mMenu.findItem(R.id.action_send_post_text_activity)); } } diff --git a/app/src/main/res/menu/full_markdown_activity.xml b/app/src/main/res/menu/full_markdown_activity.xml new file mode 100644 index 00000000..ef700721 --- /dev/null +++ b/app/src/main/res/menu/full_markdown_activity.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file