diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-03-12 02:56:44 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-03-12 02:56:44 +0000 |
commit | b2af137bf2192bf1764f9f982b1d62e6420c5ea5 (patch) | |
tree | d66e89c3b37f02fa0eae484985508dd2a983b112 | |
parent | 1a2d0c397c453ae16cf6ae68aa88755e79e609bc (diff) | |
download | infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar.gz infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar.bz2 infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar.lz infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar.xz infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.tar.zst infinity-for-reddit-b2af137bf2192bf1764f9f982b1d62e6420c5ea5.zip |
Edit comments with embedded images.
7 files changed, 59 insertions, 21 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index a0ef1052..78460e00 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -574,8 +574,8 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA int start = Math.max(binding.commentCommentEditText.getSelectionStart(), 0); int end = Math.max(binding.commentCommentEditText.getSelectionEnd(), 0); binding.commentCommentEditText.getText().replace(Math.min(start, end), Math.max(start, end), - "[" + uploadedImage.imageName + "](" + uploadedImage.imageUrlOrKey + ")", - 0, "[]()".length() + uploadedImage.imageName.length() + uploadedImage.imageUrlOrKey.length()); + "![](" + uploadedImage.imageUrlOrKey + ")", + 0, "![]()".length() + uploadedImage.imageUrlOrKey.length()); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java index 04976d31..a8caa0f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java @@ -28,6 +28,7 @@ import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import org.json.JSONException; import java.io.File; import java.io.IOException; @@ -42,6 +43,7 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MediaMetadata; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.UploadImageEnabledActivity; import ml.docilealligator.infinityforreddit.UploadedImage; @@ -51,6 +53,7 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.UploadedImagesB import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.RichTextJSONConverter; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -63,9 +66,10 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab public static final String EXTRA_CONTENT = "EC"; public static final String EXTRA_FULLNAME = "EF"; + public static final String EXTRA_MEDIA_METADATA_LIST = "EMML"; public static final String EXTRA_POSITION = "EP"; - public static final String EXTRA_EDITED_COMMENT_CONTENT = "EECC"; - public static final String EXTRA_EDITED_COMMENT_POSITION = "EECP"; + public static final String RETURN_EXTRA_EDITED_COMMENT_CONTENT = "REECC"; + public static final String RETURN_EXTRA_EDITED_COMMENT_POSITION = "REECP"; private static final int PICK_IMAGE_REQUEST_CODE = 100; private static final int CAPTURE_IMAGE_REQUEST_CODE = 200; @@ -132,6 +136,25 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab mFullName = getIntent().getStringExtra(EXTRA_FULLNAME); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mCommentContent = getIntent().getStringExtra(EXTRA_CONTENT); + ArrayList<MediaMetadata> mediaMetadataList = getIntent().getParcelableArrayListExtra(EXTRA_MEDIA_METADATA_LIST); + + if (mediaMetadataList != null) { + StringBuilder sb = new StringBuilder(mCommentContent); + for (MediaMetadata m : mediaMetadataList) { + int index = sb.indexOf(m.original.url); + if (index >= 0) { + if (index > 0 && sb.charAt(index - 1) == '(') { + sb.replace(index, index + m.original.url.length(), m.id); + } else { + sb.insert(index + m.original.url.length(), ')') + .insert(index, "![](") + .replace(index + 4, index + 4 + m.original.url.length(), m.id); + } + uploadedImages.add(new UploadedImage(m.id, m.id)); + } + } + mCommentContent = sb.toString(); + } contentEditText.setText(mCommentContent); if (savedInstanceState != null) { @@ -139,7 +162,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab } MarkdownBottomBarRecyclerViewAdapter adapter = new MarkdownBottomBarRecyclerViewAdapter( - mCustomThemeWrapper, new MarkdownBottomBarRecyclerViewAdapter.ItemClickListener() { + mCustomThemeWrapper, true, new MarkdownBottomBarRecyclerViewAdapter.ItemClickListener() { @Override public void onClick(int item) { MarkdownBottomBarRecyclerViewAdapter.bindEditTextWithItemClickListener( @@ -232,7 +255,18 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab Map<String, String> params = new HashMap<>(); params.put(APIUtils.THING_ID_KEY, mFullName); - params.put(APIUtils.TEXT_KEY, content); + if (!uploadedImages.isEmpty()) { + try { + params.put(APIUtils.RICHTEXT_JSON_KEY, new RichTextJSONConverter().constructRichTextJSON(this, content, uploadedImages)); + params.put(APIUtils.TEXT_KEY, ""); + } catch (JSONException e) { + isSubmitting = false; + Snackbar.make(coordinatorLayout, R.string.convert_to_richtext_json_failed, Snackbar.LENGTH_SHORT).show(); + return; + } + } else { + params.put(APIUtils.TEXT_KEY, content); + } mOauthRetrofit.create(RedditAPI.class) .editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params) @@ -244,8 +278,8 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show(); Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content)); - returnIntent.putExtra(EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_CONTENT, Utils.modifyMarkdown(content)); + returnIntent.putExtra(RETURN_EXTRA_EDITED_COMMENT_POSITION, getIntent().getExtras().getInt(EXTRA_POSITION)); setResult(RESULT_OK, returnIntent); finish(); @@ -353,7 +387,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab int start = Math.max(contentEditText.getSelectionStart(), 0); int end = Math.max(contentEditText.getSelectionEnd(), 0); contentEditText.getText().replace(Math.min(start, end), Math.max(start, end), - "[" + uploadedImage.imageName + "](" + uploadedImage.imageUrlOrKey + ")", - 0, "[]()".length() + uploadedImage.imageName.length() + uploadedImage.imageUrlOrKey.length()); + "![](" + uploadedImage.imageUrlOrKey + ")", + 0, "![]()".length() + uploadedImage.imageUrlOrKey.length()); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index da03b3fb..8df14433 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -727,8 +727,8 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr int start = Math.max(contentEditText.getSelectionStart(), 0); int end = Math.max(contentEditText.getSelectionEnd(), 0); contentEditText.getText().replace(Math.min(start, end), Math.max(start, end), - "![" + uploadedImage.getCaption() + "](" + uploadedImage.imageUrlOrKey + ")", - 0, "![]()".length() + uploadedImage.getCaption().length() + uploadedImage.imageUrlOrKey.length()); + "![](" + uploadedImage.imageUrlOrKey + ")", + 0, "![]()".length() + uploadedImage.imageUrlOrKey.length()); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 4547e342..a34ea1b0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -757,8 +757,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null && resultCode == Activity.RESULT_OK) { editComment(null, - data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), - data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); + data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), + data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION)); } } else if (requestCode == CommentActivity.WRITE_COMMENT_REQUEST_CODE) { if (data != null && resultCode == Activity.RESULT_OK) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 6fd6a882..9ab1e2d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -1215,8 +1215,8 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele if (data != null) { if (sectionsPagerAdapter != null) { sectionsPagerAdapter.editComment( - data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), - data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); + data.getStringExtra(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_CONTENT), + data.getExtras().getInt(EditCommentActivity.RETURN_EXTRA_EDITED_COMMENT_POSITION)); } } } else if (requestCode == ADD_TO_MULTIREDDIT_REQUEST_CODE) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java index 3026029a..6daf7b86 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java @@ -16,6 +16,9 @@ import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.MediaMetadata; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.activities.BaseActivity; @@ -81,6 +84,10 @@ public class CommentMoreBottomSheetFragment extends LandscapeExpandedRoundedBott Intent intent = new Intent(activity, EditCommentActivity.class); intent.putExtra(EditCommentActivity.EXTRA_FULLNAME, comment.getFullName()); intent.putExtra(EditCommentActivity.EXTRA_CONTENT, comment.getCommentMarkdown()); + if (comment.getMediaMetadataMap() != null) { + ArrayList<MediaMetadata> mediaMetadataList = new ArrayList<>(comment.getMediaMetadataMap().values()); + intent.putParcelableArrayListExtra(EditCommentActivity.EXTRA_MEDIA_METADATA_LIST, mediaMetadataList); + } intent.putExtra(EditCommentActivity.EXTRA_POSITION, bundle.getInt(EXTRA_POSITION)); if (activity instanceof ViewPostDetailActivity) { activity.startActivityForResult(intent, ViewPostDetailActivity.EDIT_COMMENT_REQUEST_CODE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java index 530b5dc4..d3cd46a3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit.markdown; -import android.util.Log; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; @@ -16,8 +15,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.commonmark.node.Node; -import org.json.JSONException; -import org.json.JSONObject; import java.util.Collections; import java.util.List; @@ -92,12 +89,12 @@ public class CustomMarkwonAdapter extends MarkwonAdapter { public void setParsedMarkdown(@NonNull Markwon markwon, @NonNull List<Node> nodes) { // clear all entries before applying - try { + /*try { JSONObject object = new RichTextJSONConverter().constructRichTextJSON(nodes); Log.i("asdf", "s" + object); } catch (JSONException e) { throw new RuntimeException(e); - } + }*/ defaultEntry.clear(); |