aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-11-27 16:42:07 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-11-27 16:42:07 +0000
commitc8d9269efa5be4f53848dae4ca750afdda1c4870 (patch)
tree1f038f42ac3002642c0469ed88144359b76f303f /app/src/main
parentb96552a5ca133c3d5e71232a0b8ba7b42c46440f (diff)
downloadinfinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar.gz
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar.bz2
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar.lz
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar.xz
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.tar.zst
infinity-for-reddit-c8d9269efa5be4f53848dae4ca750afdda1c4870.zip
Show a alert dialog when go back from CommentActivity, EditCommentActivity and EditPostActivity.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java106
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java30
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java30
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java8
-rw-r--r--app/src/main/res/values/strings.xml10
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);
@@ -234,6 +253,19 @@ public class CommentActivity extends BaseActivity {
}
@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();
EventBus.getDefault().unregister(this);
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 @@
<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>