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.

This commit is contained in:
Alex Ning 2019-08-27 18:18:55 +08:00
parent 995701174d
commit 942e2d52a4
9 changed files with 278 additions and 31 deletions

View File

@ -0,0 +1,3 @@
package ml.docilealligator.infinityforreddit;
public class CancelSubmittingPostEvent {}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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>