diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-27 10:18:55 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-27 10:18:55 +0000 |
commit | 942e2d52a408b70458fcdf7ca6f91b6becd191fb (patch) | |
tree | 0f05615adb35cd2508da72775055e362c603bae6 | |
parent | 995701174de54219c528993674cfa92995c03d17 (diff) | |
download | infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar.gz infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar.bz2 infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar.lz infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar.xz infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.tar.zst infinity-for-reddit-942e2d52a408b70458fcdf7ca6f91b6becd191fb.zip |
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.
9 files changed, 278 insertions, 31 deletions
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: @@ -442,6 +470,37 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } @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); outState.putString(SUBREDDIT_NAME_STATE, subredditName); 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: @@ -370,6 +395,34 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } @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); outState.putString(SUBREDDIT_NAME_STATE, subredditName); 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: @@ -369,6 +397,37 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } @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); outState.putString(SUBREDDIT_NAME_STATE, subredditName); 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: @@ -437,6 +465,37 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS } @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(); videoView.stopPlayback(); 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<String> call = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class) .getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()), - FetchMessages.WHERE_INBOX, null); + FetchMessages.WHERE_UNREAD, null); Response<String> 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 @@ <string name="settings_credits_material_icons_title">Material Icons</string> <string name="no_link_available">Cannot get the link</string> + + <string name="cancel_submit_post">Cancel Submitting</string> + <string name="cancel_submit_post_detail">The post may still be submitted even if you cancel here.</string> + <string name="discard_post">Discard?</string> + <string name="discard_post_detail">All the draft will not be saved.</string> + <string name="yes">Yes</string> + <string name="no">No</string> </resources> |