From c8d9269efa5be4f53848dae4ca750afdda1c4870 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 28 Nov 2019 00:42:07 +0800 Subject: Show a alert dialog when go back from CommentActivity, EditCommentActivity and EditPostActivity. --- .../Activity/CommentActivity.java | 106 ++++++++++++++------- .../Activity/EditCommentActivity.java | 30 +++++- .../Activity/EditPostActivity.java | 30 +++++- .../Activity/PostImageActivity.java | 8 +- .../Activity/PostLinkActivity.java | 8 +- .../Activity/PostTextActivity.java | 8 +- .../Activity/PostVideoActivity.java | 8 +- app/src/main/res/values/strings.xml | 10 +- 8 files changed, 147 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java index a70fc0c2..452c92f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java @@ -19,6 +19,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; @@ -76,6 +77,7 @@ public class CommentActivity extends BaseActivity { private String parentFullname; private int parentDepth; private int parentPosition; + private boolean isSubmitting = false; private boolean isReplying; @Override @@ -177,55 +179,72 @@ public class CommentActivity extends BaseActivity { public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: - finish(); + onBackPressed(); return true; case R.id.action_send_comment_activity: - if (commentEditText.getText() == null || commentEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show(); - return true; - } + if (!isSubmitting) { + isSubmitting = true; + if (commentEditText.getText() == null || commentEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show(); + return true; + } - 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, - mAccessToken, - new SendComment.SendCommentListener() { - @Override - public void sendCommentSuccess(CommentData commentData) { - Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show(); - Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData); - returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname); - if (isReplying) { - returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); + 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, + mAccessToken, + new SendComment.SendCommentListener() { + @Override + public void sendCommentSuccess(CommentData commentData) { + isSubmitting = false; + item.setEnabled(true); + item.getIcon().setAlpha(255); + Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show(); + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData); + returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname); + if (isReplying) { + returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); + } + setResult(RESULT_OK, returnIntent); + finish(); } - setResult(RESULT_OK, returnIntent); - finish(); - } - - @Override - public void sendCommentFailed(@Nullable String errorMessage) { - sendingSnackbar.dismiss(); - item.setEnabled(true); - item.getIcon().setAlpha(255); - if (errorMessage == null) { - Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); - } else { - Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); + @Override + public void sendCommentFailed(@Nullable String errorMessage) { + isSubmitting = false; + sendingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); + + if (errorMessage == null) { + Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); + } } - } - }); + }); + } return true; } return false; } + private void promptAlertDialog(int titleResId, int messageResId) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(titleResId) + .setMessage(messageResId) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -233,6 +252,19 @@ public class CommentActivity extends BaseActivity { outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } + @Override + public void onBackPressed() { + if (isSubmitting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail); + } else { + if (commentEditText.getText().toString().equals("")) { + finish(); + } else { + promptAlertDialog(R.string.discard, R.string.discard_detail); + } + } + } + @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java index c432325d..7aff4cdf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java @@ -14,6 +14,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; @@ -61,6 +62,7 @@ public class EditCommentActivity extends BaseActivity { SharedPreferences mSharedPreferences; private String mFullName; private String mAccessToken; + private String mCommentContent; private boolean isSubmitting = false; @Override @@ -80,7 +82,8 @@ public class EditCommentActivity extends BaseActivity { mFullName = getIntent().getStringExtra(EXTRA_FULLNAME); mAccessToken = getIntent().getStringExtra(EXTRA_ACCESS_TOKEN); - contentEditText.setText(getIntent().getStringExtra(EXTRA_CONTENT)); + mCommentContent = getIntent().getStringExtra(EXTRA_CONTENT); + contentEditText.setText(mCommentContent); contentEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); @@ -153,12 +156,35 @@ public class EditCommentActivity extends BaseActivity { } return true; } else if (item.getItemId() == android.R.id.home) { - finish(); + onBackPressed(); return true; } return false; } + private void promptAlertDialog(int titleResId, int messageResId) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(titleResId) + .setMessage(messageResId) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } + + @Override + public void onBackPressed() { + if (isSubmitting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail); + } else { + if (contentEditText.getText().toString().equals(mCommentContent)) { + finish(); + } else { + promptAlertDialog(R.string.discard, R.string.discard_detail); + } + } + } + @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java index d8c2fae2..6776c43e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java @@ -15,6 +15,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; @@ -60,6 +61,7 @@ public class EditPostActivity extends BaseActivity { SharedPreferences mSharedPreferences; private String mFullName; private String mAccessToken; + private String mPostContent; private boolean isSubmitting = false; @Override @@ -80,7 +82,8 @@ public class EditPostActivity extends BaseActivity { mFullName = getIntent().getStringExtra(EXTRA_FULLNAME); mAccessToken = getIntent().getStringExtra(EXTRA_ACCESS_TOKEN); titleTextView.setText(getIntent().getStringExtra(EXTRA_TITLE)); - contentEditText.setText(getIntent().getStringExtra(EXTRA_CONTENT)); + mPostContent = getIntent().getStringExtra(EXTRA_CONTENT); + contentEditText.setText(mPostContent); contentEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); @@ -143,12 +146,35 @@ public class EditPostActivity extends BaseActivity { } return true; } else if (item.getItemId() == android.R.id.home) { - finish(); + onBackPressed(); return true; } return false; } + private void promptAlertDialog(int titleResId, int messageResId) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(titleResId) + .setMessage(messageResId) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } + + @Override + public void onBackPressed() { + if (isSubmitting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_post_detail); + } else { + if (contentEditText.getText().toString().equals(mPostContent)) { + finish(); + } else { + promptAlertDialog(R.string.discard, R.string.discard_detail); + } + } + } + @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java index b1ae72f3..93e6ed69 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java @@ -389,11 +389,11 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF switch (item.getItemId()) { case android.R.id.home: if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; } else { if (!titleEditText.getText().toString().equals("") || imageUri != null) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } } @@ -455,10 +455,10 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF @Override public void onBackPressed() { if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); } else { if (!titleEditText.getText().toString().equals("") || imageUri != null) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); } else { finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java index 28bdc2ff..e96cf75e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java @@ -318,11 +318,11 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr switch (item.getItemId()) { case android.R.id.home: if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; } else { if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } } @@ -379,10 +379,10 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr @Override public void onBackPressed() { if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); } else { if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); } else { finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java index 4a758517..10ec3996 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java @@ -323,11 +323,11 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr switch (item.getItemId()) { case android.R.id.home: if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; } else { if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } } @@ -379,10 +379,10 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr @Override public void onBackPressed() { if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); } else { if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); } else { finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java index f0a0aa99..2fb18a16 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java @@ -395,11 +395,11 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF switch (item.getItemId()) { case android.R.id.home: if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; } else { if (!titleEditText.getText().toString().equals("") || videoUri != null) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } } @@ -461,10 +461,10 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF @Override public void onBackPressed() { if (isPosting) { - promptAlertDialog(R.string.exit_when_submit_post, R.string.exit_when_submit_post_detail); + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); } else { if (!titleEditText.getText().toString().equals("") || videoUri != null) { - promptAlertDialog(R.string.discard_post, R.string.discard_post_detail); + promptAlertDialog(R.string.discard, R.string.discard_detail); } else { finish(); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 36484819..24bd34c7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -116,7 +116,7 @@ Lazy Mode stopped Your interesting thought here - Where is your interesting thought + Where is your interesting thought? Sending Comment sent Could not send this comment @@ -330,10 +330,12 @@ Cannot get the link - Leave? + Leave? The post will still be submitted even if you leave here. - Discard? - All the draft will NOT be saved. + The post may still be submitted even if you leave here. + The comment may still be submitted even if you leave here. + Discard? + All the draft will NOT be saved. Yes No -- cgit v1.2.3