Show a alert dialog when go back from CommentActivity, EditCommentActivity and EditPostActivity.

This commit is contained in:
Alex Ning 2019-11-28 00:42:07 +08:00
parent b96552a5ca
commit c8d9269efa
8 changed files with 150 additions and 64 deletions

View File

@ -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) {
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();
}
@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();
}
}
});
}
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);
}
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();
}
}
});
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();

View File

@ -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();

View File

@ -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();

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -116,7 +116,7 @@
<string name="lazy_mode_stop">Lazy Mode stopped</string>
<string name="write_comment_hint">Your interesting thought here</string>
<string name="comment_content_required">Where is your interesting thought</string>
<string name="comment_content_required">Where is your interesting thought?</string>
<string name="sending_comment">Sending</string>
<string name="send_comment_success">Comment sent</string>
<string name="send_comment_failed">Could not send this comment</string>
@ -330,10 +330,12 @@
<string name="no_link_available">Cannot get the link</string>
<string name="exit_when_submit_post">Leave?</string>
<string name="exit_when_submit">Leave?</string>
<string name="exit_when_submit_post_detail">The post will still be submitted even if you leave here.</string>
<string name="discard_post">Discard?</string>
<string name="discard_post_detail">All the draft will NOT be saved.</string>
<string name="exit_when_edit_post_detail">The post may still be submitted even if you leave here.</string>
<string name="exit_when_edit_comment_detail">The comment may still be submitted even if you leave here.</string>
<string name="discard">Discard?</string>
<string name="discard_detail">All the draft will NOT be saved.</string>
<string name="yes">Yes</string>
<string name="no">No</string>