From 942e2d52a408b70458fcdf7ca6f91b6becd191fb Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 27 Aug 2019 18:18:55 +0800 Subject: [PATCH] Display an alert dialog if the user want to cancel submitting a post or press back button when there is something written in PostXXXActivity. Minor bugs fixed. --- .../CancelSubmittingPostEvent.java | 3 + .../infinityforreddit/NotificationUtils.java | 3 +- .../infinityforreddit/PostImageActivity.java | 59 +++++++++++++++++ .../infinityforreddit/PostLinkActivity.java | 53 +++++++++++++++ .../infinityforreddit/PostTextActivity.java | 59 +++++++++++++++++ .../infinityforreddit/PostVideoActivity.java | 59 +++++++++++++++++ .../PullNotificationWorker.java | 2 +- .../infinityforreddit/SubmitPostService.java | 64 ++++++++++--------- app/src/main/res/values/strings.xml | 7 ++ 9 files changed, 278 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/CancelSubmittingPostEvent.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CancelSubmittingPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CancelSubmittingPostEvent.java new file mode 100644 index 00000000..4eb26072 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CancelSubmittingPostEvent.java @@ -0,0 +1,3 @@ +package ml.docilealligator.infinityforreddit; + +public class CancelSubmittingPostEvent {} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java index f9e4bdee..4b985563 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java @@ -8,9 +8,10 @@ import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; class NotificationUtils { - static final String CHANNEL_POST_MEDIA = "Post Media"; + static final String CHANNEL_SUBMIT_POST = "Submit Post"; static final String CHANNEL_ID_NEW_MESSAGES = "new_messages"; static final String CHANNEL_NEW_MESSAGES = "New Messages"; + static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000; private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0; private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index ca684b35..6cbb8e7b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -30,6 +30,7 @@ import androidx.core.content.FileProvider; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; @@ -387,6 +388,33 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } else { + if(!titleEditText.getText().toString().equals("") || imageUri != null) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } + } finish(); return true; case R.id.action_send_post_image_activity: @@ -441,6 +469,37 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS return false; } + @Override + public void onBackPressed() { + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + if(!titleEditText.getText().toString().equals("") || imageUri != null) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + finish(); + } + } + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 415929c4..c8e8f875 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -24,6 +24,7 @@ import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; @@ -319,6 +320,30 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } else { + if(!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } + } finish(); return true; case R.id.action_send_post_link_activity: @@ -369,6 +394,34 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh return false; } + @Override + public void onBackPressed() { + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + if(!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } else { + finish(); + } + } + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 79e22955..978253d4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -24,6 +24,7 @@ import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; @@ -323,6 +324,33 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } else { + if(!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } + } finish(); return true; case R.id.action_send_post_text_activity: @@ -368,6 +396,37 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh return false; } + @Override + public void onBackPressed() { + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + if(!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + finish(); + } + } + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 92bef7e7..7755121e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -28,6 +28,7 @@ import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; @@ -382,6 +383,33 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } else { + if(!titleEditText.getText().toString().equals("") || videoUri != null) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + } + } finish(); return true; case R.id.action_send_post_video_activity: @@ -436,6 +464,37 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS return false; } + @Override + public void onBackPressed() { + if(isPosting) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.cancel_submit_post) + .setMessage(R.string.cancel_submit_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + if(!titleEditText.getText().toString().equals("") || videoUri != null) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.discard_post) + .setMessage(R.string.discard_post_detail) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + EventBus.getDefault().post(new CancelSubmittingPostEvent()); + finish(); + }) + .setNegativeButton(R.string.no, null) + .show(); + } else { + finish(); + } + } + } + @Override protected void onStop() { super.onStop(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 46cffca7..bb03c8e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -199,7 +199,7 @@ public class PullNotificationWorker extends Worker { Call call = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class) .getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()), - FetchMessages.WHERE_INBOX, null); + FetchMessages.WHERE_UNREAD, null); Response response = call.execute(); if(response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java index 67b90f20..17cd1628 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java @@ -22,6 +22,7 @@ import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.transition.Transition; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import java.io.FileInputStream; import java.io.IOException; @@ -67,8 +68,7 @@ public class SubmitPostService extends Service { @Named("upload_video") Retrofit mUploadVideoRetrofit; - public SubmitPostService() { - } + public SubmitPostService() {} @Override public IBinder onBind(Intent intent) { @@ -79,18 +79,20 @@ public class SubmitPostService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); - mAccessToken = intent.getExtras().getString(EXTRA_ACCESS_TOKEN); - subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); - title = intent.getExtras().getString(EXTRA_TITLE); - flair = intent.getExtras().getString(EXTRA_FLAIR); - isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER); - isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW); - int postType = intent.getExtras().getInt(EXTRA_POST_TYPE); + EventBus.getDefault().register(this); + + mAccessToken = intent.getStringExtra(EXTRA_ACCESS_TOKEN); + subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); + title = intent.getStringExtra(EXTRA_TITLE); + flair = intent.getStringExtra(EXTRA_FLAIR); + isSpoiler = intent.getBooleanExtra(EXTRA_IS_SPOILER, false); + isNSFW = intent.getBooleanExtra(EXTRA_IS_NSFW, false); + int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_POST_MEDIA, - NotificationUtils.CHANNEL_POST_MEDIA, + NotificationUtils.CHANNEL_SUBMIT_POST, + NotificationUtils.CHANNEL_SUBMIT_POST, NotificationManager.IMPORTANCE_LOW ); @@ -101,15 +103,15 @@ public class SubmitPostService extends Service { if(postType == EXTRA_POST_TEXT_OR_LINK) { content = intent.getExtras().getString(EXTRA_CONTENT); kind = intent.getExtras().getString(EXTRA_KIND); - startForeground(1, createNotification(R.string.posting)); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); submitTextOrLinkPost(); } else if(postType == EXTRA_POST_TYPE_IMAGE) { mediaUri = intent.getData(); - startForeground(1, createNotification(R.string.posting_image)); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_image)); submitImagePost(); } else { mediaUri = intent.getData(); - startForeground(1, createNotification(R.string.posting_video)); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_video)); submitVideoPost(); } @@ -117,7 +119,7 @@ public class SubmitPostService extends Service { } private Notification createNotification(int stringResId) { - return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_POST_MEDIA) + return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_SUBMIT_POST) .setContentTitle(getString(stringResId)) .setContentText(getString(R.string.please_wait)) .setSmallIcon(R.mipmap.ic_launcher_round) @@ -131,16 +133,14 @@ public class SubmitPostService extends Service { public void submitSuccessful(Post post) { EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)); - stopForeground(true); - stopSelf(); + stopService(); } @Override public void submitFailed(@Nullable String errorMessage) { EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); - stopForeground(true); - stopSelf(); + stopService(); } }); } @@ -161,16 +161,14 @@ public class SubmitPostService extends Service { EventBus.getDefault().post(new SubmitImagePostEvent(true, null)); Toast.makeText(SubmitPostService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); - stopForeground(true); - stopSelf(); + stopService(); } @Override public void submitFailed(@Nullable String errorMessage) { EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); - stopForeground(true); - stopSelf(); + stopService(); } }); } @@ -204,16 +202,14 @@ public class SubmitPostService extends Service { EventBus.getDefault().post(new SubmitVideoPostEvent(true, false, null)); Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show(); - stopForeground(true); - stopSelf(); + stopService(); } @Override public void submitFailed(@Nullable String errorMessage) { EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); - stopForeground(true); - stopSelf(); + stopService(); } }); } @@ -227,8 +223,18 @@ public class SubmitPostService extends Service { e.printStackTrace(); EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - stopForeground(true); - stopSelf(); + stopService(); } } + + private void stopService() { + EventBus.getDefault().unregister(this); + stopForeground(true); + stopSelf(); + } + + @Subscribe + public void onCancelSubmittingPostEvent(CancelSubmittingPostEvent event) { + stopService(); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 64be0073..4dbf4a7e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -279,4 +279,11 @@ Material Icons Cannot get the link + + Cancel Submitting + The post may still be submitted even if you cancel here. + Discard? + All the draft will not be saved. + Yes + No