From e5ffaf3d970510d142c11a0bdb136b910aa3dee5 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 10 Nov 2023 07:49:52 +0100 Subject: [PATCH] Fix cross posting Closes #222 --- .../activities/SubmitCrosspostActivity.java | 60 ++++++++----------- .../infinityforlemmy/post/SubmitPost.java | 4 +- .../services/SubmitPostService.java | 6 +- .../res/layout/activity_submit_crosspost.xml | 14 ----- 4 files changed, 29 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/SubmitCrosspostActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/SubmitCrosspostActivity.java index 6bc1c3ca..1adbcf04 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/SubmitCrosspostActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/SubmitCrosspostActivity.java @@ -112,8 +112,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom 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) @@ -259,11 +257,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom 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); @@ -287,13 +280,14 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom titleEditText.setText(post.getTitle()); } - if (post.getPostType() == Post.TEXT_TYPE) { + if (post.getPostType() == Post.TEXT_TYPE || post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_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()); + contentTextView.setText(generateCrossPostText(post)); } else { + if (post.getSelfTextPlain() != null && !post.getSelfTextPlain().equals("")) { + contentTextView.setVisibility(View.VISIBLE); + contentTextView.setText(generateCrossPostText(post)); + } Post.Preview preview = getPreview(post); if (preview != null) { frameLayout.setVisibility(View.VISIBLE); @@ -368,18 +362,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom } }); - 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) { @@ -461,7 +443,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom nsfwBackgroundColor = mCustomThemeWrapper.getNsfwBackgroundColor(); nsfwTextColor = mCustomThemeWrapper.getNsfwTextColor(); flairTextView.setTextColor(primaryTextColor); - spoilerTextView.setTextColor(primaryTextColor); nsfwTextView.setTextColor(primaryTextColor); titleEditText.setTextColor(primaryTextColor); titleEditText.setHintTextColor(secondaryTextColor); @@ -474,7 +455,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom rulesButton.setTypeface(typeface); receivePostReplyNotificationsTextView.setTypeface(typeface); flairTextView.setTypeface(typeface); - spoilerTextView.setTypeface(typeface); nsfwTextView.setTypeface(typeface); titleEditText.setTypeface(typeface); } @@ -561,22 +541,19 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom mPostingSnackbar.show(); - String subredditName; - if (subredditIsUser) { - subredditName = "u_" + subredditNameTextView.getText().toString(); - } else { - subredditName = subredditNameTextView.getText().toString(); - } + String subredditName = subredditNameTextView.getText().toString(); + Intent intent = new Intent(this, SubmitPostService.class); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, communityData.getId()); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); - if (post.isCrosspost()) { - intent.putExtra(SubmitPostService.EXTRA_BODY, "t3_" + post.getCrosspostParentId()); - } else { - intent.putExtra(SubmitPostService.EXTRA_BODY, post.getFullName()); + + intent.putExtra(SubmitPostService.EXTRA_BODY, generateCrossPostText(post)); + if (post.getUrl() != null && !post.getUrl().equals("")) { + intent.putExtra(SubmitPostService.EXTRA_URL, post.getUrl()); } + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_CROSSPOST); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); @@ -699,4 +676,15 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom } } } + + private static String generateCrossPostText(Post post) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("cross-posted from: ").append(post.getPermalink()).append("\n"); + String[] lines = post.getSelfTextPlain().split("\n"); + for (String line : lines) { + stringBuilder.append("> ").append(line).append("\n"); + } + + return stringBuilder.toString(); + } } \ No newline at end of file diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/SubmitPost.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/SubmitPost.java index cbd1af20..435f6e07 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/SubmitPost.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/SubmitPost.java @@ -55,11 +55,11 @@ public class SubmitPost { public static void submitCrosspost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken, int communityId, String title, String crosspostFullname, - Flair flair, boolean isSpoiler, boolean isNSFW, + String url, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications, String kind, PostEnricher postEnricher, SubmitPostListener submitPostListener) { submitPost(executor, handler, oauthRetrofit, accessToken, communityId, title, crosspostFullname, - isNSFW, receivePostReplyNotifications, kind, null, postEnricher, submitPostListener); + isNSFW, receivePostReplyNotifications, kind, url, postEnricher, submitPostListener); } private static void submitPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken, diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/services/SubmitPostService.java b/app/src/main/java/eu/toldi/infinityforlemmy/services/SubmitPostService.java index b3c54cc9..24201a27 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/services/SubmitPostService.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/services/SubmitPostService.java @@ -144,7 +144,7 @@ public class SubmitPostService extends Service { receivePostReplyNotifications, kind); } else if (postType == EXTRA_POST_TYPE_CROSSPOST) { submitCrosspost(mExecutor, handler, mRetrofit.getRetrofit(), account, subredditName, title, body, - flair, isSpoiler, isNSFW, receivePostReplyNotifications); + url, isSpoiler, isNSFW, receivePostReplyNotifications); } else if (postType == EXTRA_POST_TYPE_IMAGE) { Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI)); submitImagePost(mRetrofit, account, mediaUri, subredditName, title, body, flair, isSpoiler, isNSFW, @@ -243,10 +243,10 @@ public class SubmitPostService extends Service { private void submitCrosspost(Executor executor, Handler handler, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, int communityId, - String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, + String title, String content, String url, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) { SubmitPost.submitCrosspost(executor, handler, newAuthenticatorOauthRetrofit, selectedAccount.getAccessToken(), communityId, title, - content, flair, isSpoiler, isNSFW, receivePostReplyNotifications, APIUtils.KIND_CROSSPOST, postEnricher, + content, url, isSpoiler, isNSFW, receivePostReplyNotifications, APIUtils.KIND_CROSSPOST, postEnricher, new SubmitPost.SubmitPostListener() { @Override public void submitSuccessful(Post post) { diff --git a/app/src/main/res/layout/activity_submit_crosspost.xml b/app/src/main/res/layout/activity_submit_crosspost.xml index 9fadc791..92f1b675 100644 --- a/app/src/main/res/layout/activity_submit_crosspost.xml +++ b/app/src/main/res/layout/activity_submit_crosspost.xml @@ -124,20 +124,6 @@ app:lib_setRoundedView="true" app:lib_setShape="rectangle" /> - -