diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 912d1051..684a33d2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,10 +24,16 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> + + + android:exported="false" /> = 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"; diff --git a/app/src/main/res/layout/activity_submit_crosspost.xml b/app/src/main/res/layout/activity_submit_crosspost.xml new file mode 100644 index 00000000..8e15afb1 --- /dev/null +++ b/app/src/main/res/layout/activity_submit_crosspost.xml @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + +