From 71d6f803e06a64376a7ec3f9fb3d8652aa6d0089 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Aug 2019 18:02:01 +0800 Subject: Use PostMediaService to submit text and link posts. --- .../infinityforreddit/PostLinkActivity.java | 82 +++++++++++++------- .../infinityforreddit/PostMediaService.java | 43 +++++++++-- .../infinityforreddit/PostTextActivity.java | 88 +++++++++++++++------- .../SubmitTextOrLinkPostEvent.java | 13 ++++ 4 files changed, 166 insertions(+), 60 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubmitTextOrLinkPostEvent.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 6c240293..bbbd5540 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -22,7 +22,8 @@ import com.bumptech.glide.request.RequestOptions; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; -import java.util.Locale; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; @@ -43,6 +44,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private static final String SUBREDDIT_SELECTED_STATE = "SSS"; private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; private static final String LOAD_SUBREDDIT_ICON_STATE = "LSIS"; + private static final String IS_POSTING_STATE = "IPS"; private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; @@ -65,14 +67,16 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private boolean subredditSelected = false; private boolean subredditIsUser; private boolean loadSubredditIconSuccessful = true; + private boolean isPosting; private String flair = null; private boolean isSpoiler = false; private boolean isNSFW = false; + private Menu mMemu; private RequestManager mGlide; - private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + private Snackbar mPostingSnackbar; @Inject @Named("no_oauth") @@ -99,7 +103,6 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); - mLocale = getResources().getConfiguration().locale; if(savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); @@ -107,6 +110,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); + isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); flair = savedInstanceState.getString(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); @@ -120,6 +124,11 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } displaySubredditIcon(); + if(isPosting) { + mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); + mPostingSnackbar.show(); + } + if(flair != null) { flairTextView.setText(flair); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); @@ -131,6 +140,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } else { + isPosting = false; + if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); subredditNameTextView.setText(subredditName); @@ -228,6 +239,11 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.post_link_activity, menu); + mMemu = menu; + if(isPosting) { + mMemu.findItem(R.id.action_send_post_image_activity).setEnabled(false); + mMemu.findItem(R.id.action_send_post_image_activity).getIcon().setAlpha(130); + } return true; } @@ -255,29 +271,16 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh subredditName = subredditNameTextView.getText().toString(); } - SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName, - titleEditText.getText().toString(), contentEditText.getText().toString(), - flair, isSpoiler, isNSFW, RedditUtils.KIND_LINK, new SubmitPost.SubmitPostListener() { - @Override - public void submitSuccessful(Post post) { - Intent intent = new Intent(PostLinkActivity.this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); - startActivity(intent); - finish(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - postingSnackbar.dismiss(); - item.setEnabled(true); - item.getIcon().setAlpha(255); - if(errorMessage == null) { - Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); - } else { - Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); - } - } - }); + Intent intent = new Intent(this, PostMediaService.class); + intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(PostMediaService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(PostMediaService.EXTRA_KIND, RedditUtils.KIND_LINK); + intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); + intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_IMAGE); + return true; } @@ -292,6 +295,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); + outState.putBoolean(IS_POSTING_STATE, isPosting); outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); @@ -319,6 +323,12 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } } + @Override + protected void onDestroy() { + EventBus.getDefault().unregister(this); + super.onDestroy(); + } + @Override public void flairSelected(String flair) { this.flair = flair; @@ -326,4 +336,24 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairSelectionBottomSheetFragment.dismiss(); } + + @Subscribe + public void onSubmitLinkPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { + isPosting = false; + if(submitTextOrLinkPostEvent.postSuccess) { + Intent intent = new Intent(PostLinkActivity.this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, submitTextOrLinkPostEvent.post); + startActivity(intent); + finish(); + } else { + mPostingSnackbar.dismiss(); + mMemu.findItem(R.id.action_send_post_link_activity).setEnabled(true); + mMemu.findItem(R.id.action_send_post_link_activity).getIcon().setAlpha(255); + if(submitTextOrLinkPostEvent.errorMessage == null) { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java index fa02eb41..21f12cd4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java @@ -35,12 +35,15 @@ import retrofit2.Retrofit; public class PostMediaService extends Service { static final String EXTRA_SUBREDDIT_NAME = "ESN"; static final String EXTRA_TITLE = "ET"; + static final String EXTRA_CONTENT = "EC"; + static final String EXTRA_KIND = "EK"; static final String EXTRA_FLAIR = "EF"; static final String EXTRA_IS_SPOILER = "EIS"; static final String EXTRA_IS_NSFW = "EIN"; static final String EXTRA_POST_TYPE = "EPT"; - static final int EXTRA_POST_TYPE_IMAGE = 0; - static final int EXTRA_POST_TYPE_VIDEO = 1; + static final int EXTRA_POST_TEXT_OR_LINK = 0; + static final int EXTRA_POST_TYPE_IMAGE = 1; + static final int EXTRA_POST_TYPE_VIDEO = 2; @Inject @Named("oauth") @@ -74,7 +77,6 @@ public class PostMediaService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getmAppComponent().inject(this); - Uri mediaUri = intent.getData(); String subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); String title = intent.getExtras().getString(EXTRA_TITLE); String flair = intent.getExtras().getString(EXTRA_FLAIR); @@ -93,12 +95,19 @@ public class PostMediaService extends Service { manager.createNotificationChannel(serviceChannel); } - if(postType == EXTRA_POST_TYPE_IMAGE) { + if(postType == EXTRA_POST_TEXT_OR_LINK) { + String content = intent.getExtras().getString(EXTRA_CONTENT); + String kind = intent.getExtras().getString(EXTRA_KIND); + startForeground(1, createNotification(R.string.posting)); + submitTextOrLinkPost(subredditName, title, content, flair, isSpoiler, isNSFW, kind); + } else if(postType == EXTRA_POST_TYPE_IMAGE) { + Uri imageUri = intent.getData(); startForeground(1, createNotification(R.string.posting_image)); - submitImagePost(mediaUri, subredditName, title, flair, isSpoiler, isNSFW); + submitImagePost(imageUri, subredditName, title, flair, isSpoiler, isNSFW); } else { + Uri videoUri = intent.getData(); startForeground(1, createNotification(R.string.posting_video)); - submitVideoPost(mediaUri, subredditName, title, flair, isSpoiler, isNSFW); + submitVideoPost(videoUri, subredditName, title, flair, isSpoiler, isNSFW); } return START_NOT_STICKY; @@ -112,6 +121,28 @@ public class PostMediaService extends Service { .build(); } + private void submitTextOrLinkPost(String subredditName, String title, String content, String flair, + boolean isSpoiler, boolean isNSFW, String kind) { + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, getResources().getConfiguration().locale, + subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)); + + stopForeground(true); + stopSelf(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); + + stopForeground(true); + stopSelf(); + } + }); + } + private void submitImagePost(Uri imageUri, String subredditName, String title, String flair, boolean isSpoiler, boolean isNSFW) { Glide.with(this) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index e5601697..fd485e4b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -22,7 +22,8 @@ import com.bumptech.glide.request.RequestOptions; import com.google.android.material.snackbar.Snackbar; import com.libRG.CustomTextView; -import java.util.Locale; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; @@ -43,6 +44,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private static final String SUBREDDIT_SELECTED_STATE = "SSS"; private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; private static final String LOAD_SUBREDDIT_ICON_STATE = "LSIS"; + private static final String IS_POSTING_STATE = "IPS"; private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; @@ -65,14 +67,16 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private boolean subredditSelected = false; private boolean subredditIsUser; private boolean loadSubredditIconSuccessful = true; + private boolean isPosting; private String flair = null; private boolean isSpoiler = false; private boolean isNSFW = false; + private Menu mMemu; private RequestManager mGlide; - private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + private Snackbar mPostingSnackbar; @Inject @Named("no_oauth") @@ -93,13 +97,14 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh ButterKnife.bind(this); + EventBus.getDefault().register(this); + ((Infinity) getApplication()).getmAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); - mLocale = getResources().getConfiguration().locale; if(savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); @@ -107,6 +112,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); + isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); flair = savedInstanceState.getString(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); @@ -120,6 +126,11 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } displaySubredditIcon(); + if(isPosting) { + mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); + mPostingSnackbar.show(); + } + if(flair != null) { flairTextView.setText(flair); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); @@ -131,6 +142,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } else { + isPosting = false; + if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { loadSubredditIconSuccessful = false; subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); @@ -233,6 +246,11 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.post_text_activity, menu); + mMemu = menu; + if(isPosting) { + mMemu.findItem(R.id.action_send_post_image_activity).setEnabled(false); + mMemu.findItem(R.id.action_send_post_image_activity).getIcon().setAlpha(130); + } return true; } @@ -250,8 +268,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh item.setEnabled(false); item.getIcon().setAlpha(130); - Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); - postingSnackbar.show(); + mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); + mPostingSnackbar.show(); String subredditName; if(subredditIsUser) { @@ -260,29 +278,16 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh subredditName = subreditNameTextView.getText().toString(); } - SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName, - titleEditText.getText().toString(), contentEditText.getText().toString(), - flair, isSpoiler, isNSFW, RedditUtils.KIND_SELF, new SubmitPost.SubmitPostListener() { - @Override - public void submitSuccessful(Post post) { - Intent intent = new Intent(PostTextActivity.this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); - startActivity(intent); - finish(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - postingSnackbar.dismiss(); - item.setEnabled(true); - item.getIcon().setAlpha(255); - if(errorMessage == null) { - Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); - } else { - Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); - } - } - }); + Intent intent = new Intent(this, PostMediaService.class); + intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(PostMediaService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(PostMediaService.EXTRA_KIND, RedditUtils.KIND_SELF); + intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); + intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_IMAGE); + return true; } @@ -297,6 +302,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); + outState.putBoolean(IS_POSTING_STATE, isPosting); outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); @@ -324,6 +330,12 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } } + @Override + protected void onDestroy() { + EventBus.getDefault().unregister(this); + super.onDestroy(); + } + @Override public void flairSelected(String flair) { this.flair = flair; @@ -331,4 +343,24 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairSelectionBottomSheetFragment.dismiss(); } + + @Subscribe + public void onSubmitTextPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { + isPosting = false; + if(submitTextOrLinkPostEvent.postSuccess) { + Intent intent = new Intent(PostTextActivity.this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, submitTextOrLinkPostEvent.post); + startActivity(intent); + finish(); + } else { + mPostingSnackbar.dismiss(); + mMemu.findItem(R.id.action_send_post_text_activity).setEnabled(true); + mMemu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(255); + if(submitTextOrLinkPostEvent.errorMessage == null) { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitTextOrLinkPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitTextOrLinkPostEvent.java new file mode 100644 index 00000000..073298f5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitTextOrLinkPostEvent.java @@ -0,0 +1,13 @@ +package ml.docilealligator.infinityforreddit; + +public class SubmitTextOrLinkPostEvent { + public boolean postSuccess; + public Post post; + public String errorMessage; + + public SubmitTextOrLinkPostEvent(boolean postSuccess, Post post, String errorMessage) { + this.postSuccess = postSuccess; + this.post = post; + this.errorMessage = errorMessage; + } +} -- cgit v1.2.3