From fc40fb3b243c259347dda749230ef042d9cb3705 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 8 Sep 2020 10:55:32 +0800 Subject: Crossposting is available. --- .../Activity/PostTextActivity.java | 12 +- .../Activity/SubmitCrosspostActivity.java | 605 +++++++++++++++++++++ .../Activity/ViewPostDetailActivity.java | 5 + .../infinityforreddit/AppComponent.java | 3 + .../Event/SubmitCrosspostEvent.java | 15 + .../infinityforreddit/Post/SubmitPost.java | 11 + .../Service/SubmitPostService.java | 26 + .../infinityforreddit/Utils/APIUtils.java | 2 + 8 files changed, 673 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitCrosspostEvent.java (limited to 'app/src/main/java') 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 2a666d5b..69568d88 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java @@ -129,7 +129,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr private boolean isSpoiler = false; private boolean isNSFW = false; private Resources resources; - private Menu mMemu; + private Menu mMenu; private RequestManager mGlide; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; private Snackbar mPostingSnackbar; @@ -391,10 +391,10 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.post_text_activity, menu); applyMenuItemTheme(menu); - mMemu = menu; + mMenu = menu; if (isPosting) { - mMemu.findItem(R.id.action_send_post_text_activity).setEnabled(false); - mMemu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(130); + mMenu.findItem(R.id.action_send_post_text_activity).setEnabled(false); + mMenu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(130); } return true; } @@ -539,8 +539,8 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr startActivity(intent); finish(); } else { - mMemu.findItem(R.id.action_send_post_text_activity).setEnabled(true); - mMemu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(255); + mMenu.findItem(R.id.action_send_post_text_activity).setEnabled(true); + mMenu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(255); if (submitTextOrLinkPostEvent.errorMessage == null || submitTextOrLinkPostEvent.errorMessage.equals("")) { Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java new file mode 100644 index 00000000..4718755b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java @@ -0,0 +1,605 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.ColorStateList; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; +import com.davemorrissey.labs.subscaleview.ImageSource; +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.snackbar.Snackbar; +import com.libRG.CustomTextView; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.LoadSubredditIconAsyncTask; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.FlairBottomSheetFragment; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.SubmitCrosspostEvent; +import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.Flair; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.Service.SubmitPostService; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import pl.droidsonroids.gif.GifImageView; +import retrofit2.Retrofit; + +public class SubmitCrosspostActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback { + + public static final String EXTRA_POST = "EP"; + + private static final String SUBREDDIT_NAME_STATE = "SNS"; + private static final String SUBREDDIT_ICON_STATE = "SIS"; + 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"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; + + private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; + + @BindView(R.id.coordinator_layout_submit_crosspost_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_submit_crosspost_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_submit_crosspost_activity) + Toolbar toolbar; + @BindView(R.id.subreddit_icon_gif_image_view_submit_crosspost_activity) + GifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_submit_crosspost_activity) + TextView subredditNameTextView; + @BindView(R.id.rules_button_submit_crosspost_activity) + Button rulesButton; + @BindView(R.id.divider_1_submit_crosspost_activity) + View divider1; + @BindView(R.id.flair_custom_text_view_submit_crosspost_activity) + CustomTextView flairTextView; + @BindView(R.id.spoiler_custom_text_view_submit_crosspost_activity) + CustomTextView spoilerTextView; + @BindView(R.id.nsfw_custom_text_view_submit_crosspost_activity) + CustomTextView nsfwTextView; + @BindView(R.id.divider_2_submit_crosspost_activity) + View divider2; + @BindView(R.id.post_title_edit_text_submit_crosspost_activity) + EditText titleEditText; + @BindView(R.id.divider_3_submit_crosspost_activity) + View divider3; + @BindView(R.id.post_content_text_view_submit_crosspost_activity) + TextView contentTextView; + @BindView(R.id.frame_layout_submit_crosspost_activity) + FrameLayout frameLayout; + @BindView(R.id.image_view_submit_crosspost_activity) + SubsamplingScaleImageView imageView; + @BindView(R.id.play_button_image_view_submit_crosspost_activity) + ImageView playButton; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + private boolean mNullAccessToken = false; + private String mAccessToken; + private Post post; + private String iconUrl; + private String subredditName; + private boolean subredditSelected = false; + private boolean subredditIsUser; + private boolean loadSubredditIconSuccessful = true; + private boolean isPosting; + private int primaryTextColor; + private int flairBackgroundColor; + private int flairTextColor; + private int spoilerBackgroundColor; + private int spoilerTextColor; + private int nsfwBackgroundColor; + private int nsfwTextColor; + private Flair flair; + private boolean isSpoiler = false; + private boolean isNSFW = false; + private Resources resources; + private Menu mMenu; + private RequestManager mGlide; + private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + private Snackbar mPostingSnackbar; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + setImmersiveModeNotApplicable(); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_submit_crosspost); + + ButterKnife.bind(this); + + EventBus.getDefault().register(this); + + applyCustomTheme(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + mGlide = Glide.with(this); + + mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); + + resources = getResources(); + + post = getIntent().getParcelableExtra(EXTRA_POST); + + if (savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + + if (!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); + } + + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); + iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); + 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.getParcelable(FLAIR_STATE); + isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); + isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); + + if (subredditName != null) { + subredditNameTextView.setTextColor(primaryTextColor); + subredditNameTextView.setText(subredditName); + flairTextView.setVisibility(View.VISIBLE); + if (!loadSubredditIconSuccessful) { + loadSubredditIcon(); + } + } + displaySubredditIcon(); + + if (isPosting) { + mPostingSnackbar.show(); + } + + if (flair != null) { + flairTextView.setText(flair.getText()); + flairTextView.setBackgroundColor(flairBackgroundColor); + flairTextView.setBorderColor(flairBackgroundColor); + flairTextView.setTextColor(flairTextColor); + } + if (isSpoiler) { + spoilerTextView.setBackgroundColor(spoilerBackgroundColor); + spoilerTextView.setBorderColor(spoilerBackgroundColor); + spoilerTextView.setTextColor(spoilerTextColor); + } + if (isNSFW) { + nsfwTextView.setBackgroundColor(nsfwBackgroundColor); + nsfwTextView.setBorderColor(nsfwBackgroundColor); + nsfwTextView.setTextColor(nsfwTextColor); + } + } else { + getCurrentAccount(); + + isPosting = false; + + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + + if (post.isSpoiler()) { + spoilerTextView.setBackgroundColor(spoilerBackgroundColor); + spoilerTextView.setBorderColor(spoilerBackgroundColor); + spoilerTextView.setTextColor(spoilerTextColor); + } + if (post.isNSFW()) { + nsfwTextView.setBackgroundColor(nsfwBackgroundColor); + nsfwTextView.setBorderColor(nsfwBackgroundColor); + nsfwTextView.setTextColor(nsfwTextColor); + } + + titleEditText.setText(post.getTitle()); + } + + if (post.getPostType() == Post.TEXT_TYPE) { + contentTextView.setVisibility(View.VISIBLE); + contentTextView.setText(post.getSelfTextPlain()); + } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { + contentTextView.setVisibility(View.VISIBLE); + contentTextView.setText(post.getUrl()); + } else if (post.getPostType() == Post.GIF_TYPE || post.getPostType() == Post.GALLERY_TYPE || post.getPostType() == Post.IMAGE_TYPE) { + frameLayout.setVisibility(View.VISIBLE); + mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + imageView.setImage(ImageSource.bitmap(resource)); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } else if (post.getPostType() == Post.VIDEO_TYPE) { + frameLayout.setVisibility(View.VISIBLE); + mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + imageView.setImage(ImageSource.bitmap(resource)); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + playButton.setVisibility(View.VISIBLE); + } + + iconGifImageView.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + + subredditNameTextView.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + + rulesButton.setOnClickListener(view -> { + if (subredditName == null) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + } else { + Intent intent = new Intent(this, RulesActivity.class); + if (subredditIsUser) { + intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); + } else { + intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName); + } + startActivity(intent); + } + }); + + flairTextView.setOnClickListener(view -> { + if (flair == null) { + flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + if (subredditIsUser) { + bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); + } else { + bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); + } + flairSelectionBottomSheetFragment.setArguments(bundle); + flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); + } else { + flairTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + flairTextView.setTextColor(primaryTextColor); + flairTextView.setText(getString(R.string.flair)); + flair = null; + } + }); + + spoilerTextView.setOnClickListener(view -> { + if (!isSpoiler) { + spoilerTextView.setBackgroundColor(spoilerBackgroundColor); + spoilerTextView.setBorderColor(spoilerBackgroundColor); + spoilerTextView.setTextColor(spoilerTextColor); + isSpoiler = true; + } else { + spoilerTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + spoilerTextView.setTextColor(primaryTextColor); + isSpoiler = false; + } + }); + + nsfwTextView.setOnClickListener(view -> { + if (!isNSFW) { + nsfwTextView.setBackgroundColor(nsfwBackgroundColor); + nsfwTextView.setBorderColor(nsfwBackgroundColor); + nsfwTextView.setTextColor(nsfwTextColor); + isNSFW = true; + } else { + nsfwTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + nsfwTextView.setTextColor(primaryTextColor); + isNSFW = false; + } + }); + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + int secondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor(); + subredditNameTextView.setTextColor(secondaryTextColor); + rulesButton.setTextColor(mCustomThemeWrapper.getButtonTextColor()); + rulesButton.setBackgroundTintList(ColorStateList.valueOf(mCustomThemeWrapper.getColorPrimaryLightTheme())); + int dividerColor = mCustomThemeWrapper.getDividerColor(); + divider1.setBackgroundColor(dividerColor); + divider2.setBackgroundColor(dividerColor); + divider3.setBackgroundColor(dividerColor); + primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor(); + flairBackgroundColor = mCustomThemeWrapper.getFlairBackgroundColor(); + flairTextColor = mCustomThemeWrapper.getFlairTextColor(); + spoilerBackgroundColor = mCustomThemeWrapper.getSpoilerBackgroundColor(); + spoilerTextColor = mCustomThemeWrapper.getSpoilerTextColor(); + nsfwBackgroundColor = mCustomThemeWrapper.getNsfwBackgroundColor(); + nsfwTextColor = mCustomThemeWrapper.getNsfwTextColor(); + flairTextView.setTextColor(primaryTextColor); + spoilerTextView.setTextColor(primaryTextColor); + nsfwTextView.setTextColor(primaryTextColor); + titleEditText.setTextColor(primaryTextColor); + titleEditText.setHintTextColor(secondaryTextColor); + contentTextView.setTextColor(primaryTextColor); + contentTextView.setHintTextColor(secondaryTextColor); + } + + private void getCurrentAccount() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + } + }).execute(); + } + + private void displaySubredditIcon() { + if (iconUrl != null && !iconUrl.equals("")) { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(iconGifImageView); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } + } + + private void loadSubredditIcon() { + new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit, iconImageUrl -> { + iconUrl = iconImageUrl; + displaySubredditIcon(); + loadSubredditIconSuccessful = true; + }).execute(); + } + + 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 boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.submit_crosspost_activity, menu); + applyMenuItemTheme(menu); + mMenu = menu; + if (isPosting) { + mMenu.findItem(R.id.action_send_submit_crosspost_activity).setEnabled(false); + mMenu.findItem(R.id.action_send_submit_crosspost_activity).getIcon().setAlpha(130); + } + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + if (isPosting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); + return true; + } else { + if (!titleEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); + return true; + } + } + finish(); + return true; + case R.id.action_send_submit_crosspost_activity: + if (!subredditSelected) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + return true; + } + + if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + return true; + } + + isPosting = true; + + item.setEnabled(false); + item.getIcon().setAlpha(130); + + mPostingSnackbar.show(); + + String subredditName; + if (subredditIsUser) { + subredditName = "u_" + subredditNameTextView.getText().toString(); + } else { + subredditName = subredditNameTextView.getText().toString(); + } + + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + if (post.isCrosspost()) { + intent.putExtra(SubmitPostService.EXTRA_CONTENT, "t3_" + post.getCrosspostParentId()); + } else { + intent.putExtra(SubmitPostService.EXTRA_CONTENT, post.getFullName()); + } + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_CROSSPOST); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_CROSSPOST); + startService(intent); + + return true; + } + + return false; + } + + @Override + public void onBackPressed() { + if (isPosting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); + } else { + if (!titleEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); + } else { + finish(); + } + } + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(SUBREDDIT_NAME_STATE, subredditName); + outState.putString(SUBREDDIT_ICON_STATE, iconUrl); + 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.putParcelable(FLAIR_STATE, flair); + outState.putBoolean(IS_SPOILER_STATE, isSpoiler); + outState.putBoolean(IS_NSFW_STATE, isNSFW); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { + if (resultCode == RESULT_OK) { + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + subredditSelected = true; + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); + + subredditNameTextView.setTextColor(primaryTextColor); + subredditNameTextView.setText(subredditName); + displaySubredditIcon(); + + flairTextView.setVisibility(View.VISIBLE); + flairTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + flairTextView.setTextColor(primaryTextColor); + flairTextView.setText(getString(R.string.flair)); + flair = null; + } + } + } + + @Override + protected void onDestroy() { + EventBus.getDefault().unregister(this); + super.onDestroy(); + } + + @Override + public void flairSelected(Flair flair) { + this.flair = flair; + flairTextView.setText(flair.getText()); + flairTextView.setBackgroundColor(flairBackgroundColor); + flairTextView.setBorderColor(flairBackgroundColor); + flairTextView.setTextColor(flairTextColor); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe + public void onSubmitTextPostEvent(SubmitCrosspostEvent submitCrosspostEvent) { + isPosting = false; + mPostingSnackbar.dismiss(); + if (submitCrosspostEvent.postSuccess) { + Intent intent = new Intent(this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, submitCrosspostEvent.post); + startActivity(intent); + finish(); + } else { + mMenu.findItem(R.id.action_send_post_text_activity).setEnabled(true); + mMenu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(255); + if (submitCrosspostEvent.errorMessage == null || submitCrosspostEvent.errorMessage.equals("")) { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, submitCrosspostEvent.errorMessage.substring(0, 1).toUpperCase() + + submitCrosspostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show(); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index a150f656..062d7f5b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -1466,6 +1466,11 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS case R.id.action_see_removed_view_post_detail_activity: showRemovedPost(); return true; + case R.id.action_crosspost_view_post_detail_activity: + Intent submitCrosspostIntent = new Intent(this, SubmitCrosspostActivity.class); + submitCrosspostIntent.putExtra(SubmitCrosspostActivity.EXTRA_POST, mPost); + startActivity(submitCrosspostIntent); + return true; case android.R.id.home: onBackPressed(); return true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index add19533..34695608 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -29,6 +29,7 @@ import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActiv import ml.docilealligator.infinityforreddit.Activity.SelectedSubredditsActivity; import ml.docilealligator.infinityforreddit.Activity.SendPrivateMessageActivity; import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; +import ml.docilealligator.infinityforreddit.Activity.SubmitCrosspostActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActivity; @@ -194,4 +195,6 @@ public interface AppComponent { void inject(DownloadMediaService downloadMediaService); void inject(DownloadLocationPreferenceFragment downloadLocationPreferenceFragment); + + void inject(SubmitCrosspostActivity submitCrosspostActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitCrosspostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitCrosspostEvent.java new file mode 100644 index 00000000..55b4a5bd --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitCrosspostEvent.java @@ -0,0 +1,15 @@ +package ml.docilealligator.infinityforreddit.Event; + +import ml.docilealligator.infinityforreddit.Post.Post; + +public class SubmitCrosspostEvent { + public boolean postSuccess; + public Post post; + public String errorMessage; + + public SubmitCrosspostEvent(boolean postSuccess, Post post, String errorMessage) { + this.postSuccess = postSuccess; + this.post = post; + this.errorMessage = errorMessage; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java index 01a0cb3d..c1c908f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java @@ -157,6 +157,14 @@ public class SubmitPost { }); } + public static void submitCrosspost(Retrofit oauthRetrofit, String accessToken, + Locale locale, String subredditName, String title, String crosspostFullname, + Flair flair, boolean isSpoiler, boolean isNSFW, String kind, + SubmitPostListener submitPostListener) { + submitPost(oauthRetrofit, accessToken, locale, subredditName, title, crosspostFullname, + flair, isSpoiler, isNSFW, kind, null, submitPostListener); + } + private static void submitPost(Retrofit oauthRetrofit, String accessToken, Locale locale, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, String kind, @@ -185,6 +193,9 @@ public class SubmitPost { params.put(APIUtils.URL_KEY, content); params.put(APIUtils.VIDEO_POSTER_URL_KEY, posterUrl); break; + case APIUtils.KIND_CROSSPOST: + params.put(APIUtils.CROSSPOST_FULLNAME_KEY, content); + break; } if (flair != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java index 5457ef84..c65196d8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java @@ -34,6 +34,7 @@ import javax.inject.Inject; import javax.inject.Named; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.SubmitCrosspostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitImagePostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitVideoOrGifPostEvent; @@ -43,6 +44,7 @@ import ml.docilealligator.infinityforreddit.NotificationUtils; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.SubmitPost; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Retrofit; public class SubmitPostService extends Service { @@ -58,6 +60,7 @@ public class SubmitPostService extends Service { public static final int EXTRA_POST_TEXT_OR_LINK = 0; public static final int EXTRA_POST_TYPE_IMAGE = 1; public static final int EXTRA_POST_TYPE_VIDEO = 2; + public static final int EXTRA_POST_TYPE_CROSSPOST = 3; @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -115,6 +118,10 @@ public class SubmitPostService extends Service { kind = intent.getStringExtra(EXTRA_KIND); startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); submitTextOrLinkPost(); + } else if (postType == EXTRA_POST_TYPE_CROSSPOST) { + content = intent.getStringExtra(EXTRA_CONTENT); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); + submitCrosspost(); } else if (postType == EXTRA_POST_TYPE_IMAGE) { mediaUri = intent.getData(); startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_image)); @@ -156,6 +163,25 @@ public class SubmitPostService extends Service { }); } + private void submitCrosspost() { + SubmitPost.submitCrosspost(mOauthRetrofit, mAccessToken, getResources().getConfiguration().locale, + subredditName, title, content, flair, isSpoiler, isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null)); + + stopService(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitCrosspostEvent(false, null, errorMessage)); + + stopService(); + } + }); + } + private void submitImagePost() { Glide.with(this) .asBitmap() diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java index f824eafa..8f54b256 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java @@ -71,12 +71,14 @@ public class APIUtils { public static final String FLAIR_TEXT_KEY = "flair_text"; public static final String SPOILER_KEY = "spoiler"; public static final String NSFW_KEY = "nsfw"; + public static final String CROSSPOST_FULLNAME_KEY = "crosspost_fullname"; public static final String KIND_KEY = "kind"; public static final String KIND_SELF = "self"; public static final String KIND_LINK = "link"; public static final String KIND_IMAGE = "image"; public static final String KIND_VIDEO = "video"; public static final String KIND_VIDEOGIF = "videogif"; + public static final String KIND_CROSSPOST = "crosspost"; public static final String FILEPATH_KEY = "filepath"; public static final String MIMETYPE_KEY = "mimetype"; -- cgit v1.2.3