From 0b5e1523090c612a2eae7a314c254ef859b6823e Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 19 Sep 2024 16:58:54 -0400 Subject: Editing comments with Giphy gif is available. --- .../activities/CommentActivity.java | 8 +++- .../activities/EditCommentActivity.java | 53 ++++++++++++++++++++-- .../infinityforreddit/comment/SendComment.java | 3 +- .../markdown/GiphyGifBlockParser.java | 25 ++++++++-- .../infinityforreddit/markdown/GiphyGifPlugin.java | 7 ++- .../markdown/RichTextJSONConverter.java | 12 ++++- .../infinityforreddit/thing/GiphyGif.java | 41 +++++++++++++++-- 7 files changed, 133 insertions(+), 16 deletions(-) (limited to 'app/src') 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 cb012642..67d5cf3a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -81,7 +81,8 @@ import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import retrofit2.Retrofit; -public class CommentActivity extends BaseActivity implements UploadImageEnabledActivity, AccountChooserBottomSheetFragment.AccountChooserListener, GiphyDialogFragment.GifSelectionListener { +public class CommentActivity extends BaseActivity implements UploadImageEnabledActivity, + AccountChooserBottomSheetFragment.AccountChooserListener, GiphyDialogFragment.GifSelectionListener { public static final String EXTRA_COMMENT_PARENT_TITLE_KEY = "ECPTK"; public static final String EXTRA_COMMENT_PARENT_BODY_KEY = "ECPBK"; @@ -99,6 +100,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA private static final String SELECTED_ACCOUNT_STATE = "SAS"; private static final String UPLOADED_IMAGES_STATE = "UIS"; + private static final String GIPHY_GIF_STATE = "GGS"; @Inject @Named("no_oauth") @@ -272,6 +274,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA if (savedInstanceState != null) { selectedAccount = savedInstanceState.getParcelable(SELECTED_ACCOUNT_STATE); uploadedImages = savedInstanceState.getParcelableArrayList(UPLOADED_IMAGES_STATE); + giphyGif = savedInstanceState.getParcelable(GIPHY_GIF_STATE); if (selectedAccount != null) { mGlide.load(selectedAccount.getProfileImageUrl()) @@ -353,6 +356,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA super.onSaveInstanceState(outState); outState.putParcelable(SELECTED_ACCOUNT_STATE, selectedAccount); outState.putParcelableArrayList(UPLOADED_IMAGES_STATE, uploadedImages); + outState.putParcelable(GIPHY_GIF_STATE, giphyGif); } @Override @@ -621,7 +625,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA @Override public void onGifSelected(@NonNull Media media, @Nullable String s, @NonNull GPHContentType gphContentType) { - this.giphyGif = new GiphyGif(media.getId()); + this.giphyGif = new GiphyGif(media.getId(), true); int start = Math.max(binding.commentCommentEditText.getSelectionStart(), 0); int end = Math.max(binding.commentCommentEditText.getSelectionEnd(), 0); 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 c5446771..1cb27b7c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java @@ -18,6 +18,10 @@ import androidx.annotation.Nullable; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; +import com.giphy.sdk.core.models.Media; +import com.giphy.sdk.ui.GPHContentType; +import com.giphy.sdk.ui.Giphy; +import com.giphy.sdk.ui.views.GiphyDialogFragment; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.snackbar.Snackbar; @@ -36,6 +40,7 @@ import javax.inject.Inject; import javax.inject.Named; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.thing.GiphyGif; import ml.docilealligator.infinityforreddit.thing.MediaMetadata; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.thing.UploadedImage; @@ -55,7 +60,8 @@ import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; -public class EditCommentActivity extends BaseActivity implements UploadImageEnabledActivity { +public class EditCommentActivity extends BaseActivity implements UploadImageEnabledActivity, + GiphyDialogFragment.GifSelectionListener { public static final String EXTRA_CONTENT = "EC"; public static final String EXTRA_FULLNAME = "EF"; @@ -69,6 +75,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab private static final int MARKDOWN_PREVIEW_REQUEST_CODE = 300; private static final String UPLOADED_IMAGES_STATE = "UIS"; + private static final String GIPHY_GIF_STATE = "GGS"; @Inject @Named("oauth") @@ -92,6 +99,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab private boolean isSubmitting = false; private Uri capturedImageUri; private ArrayList uploadedImages = new ArrayList<>(); + private GiphyGif giphyGif; private ActivityEditCommentBinding binding; @Override @@ -141,10 +149,11 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab if (savedInstanceState != null) { uploadedImages = savedInstanceState.getParcelableArrayList(UPLOADED_IMAGES_STATE); + giphyGif = savedInstanceState.getParcelable(GIPHY_GIF_STATE); } MarkdownBottomBarRecyclerViewAdapter adapter = new MarkdownBottomBarRecyclerViewAdapter( - mCustomThemeWrapper, true, new MarkdownBottomBarRecyclerViewAdapter.ItemClickListener() { + mCustomThemeWrapper, true, true, new MarkdownBottomBarRecyclerViewAdapter.ItemClickListener() { @Override public void onClick(int item) { MarkdownBottomBarRecyclerViewAdapter.bindEditTextWithItemClickListener( @@ -161,6 +170,11 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab fragment.setArguments(arguments); fragment.show(getSupportFragmentManager(), fragment.getTag()); } + + @Override + public void onSelectGiphyGif() { + GiphyDialogFragment.Companion.newInstance().show(getSupportFragmentManager(), "giphy_dialog"); + } }); binding.markdownBottomBarRecyclerViewEditCommentActivity.setLayoutManager(new LinearLayoutManagerBugFixed(this, @@ -169,6 +183,8 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab binding.commentEditTextEditCommentActivity.requestFocus(); Utils.showKeyboard(this, new Handler(), binding.commentEditTextEditCommentActivity); + + Giphy.INSTANCE.configure(this, APIUtils.GIPHY_GIF_API_KEY); } @Override @@ -237,9 +253,9 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab Map params = new HashMap<>(); params.put(APIUtils.THING_ID_KEY, mFullName); - if (!uploadedImages.isEmpty()) { + if (!uploadedImages.isEmpty() || giphyGif != null) { try { - params.put(APIUtils.RICHTEXT_JSON_KEY, new RichTextJSONConverter().constructRichTextJSON(this, content, uploadedImages)); + params.put(APIUtils.RICHTEXT_JSON_KEY, new RichTextJSONConverter().constructRichTextJSON(this, content, uploadedImages, giphyGif)); params.put(APIUtils.TEXT_KEY, ""); } catch (JSONException e) { isSubmitting = false; @@ -314,6 +330,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList(UPLOADED_IMAGES_STATE, uploadedImages); + outState.putParcelable(GIPHY_GIF_STATE, giphyGif); } @Override @@ -379,4 +396,32 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab 0, "![]()\n".length() + uploadedImage.imageUrlOrKey.length()); } } + + @Override + public void didSearchTerm(@NonNull String s) { + + } + + @Override + public void onGifSelected(@NonNull Media media, @Nullable String s, @NonNull GPHContentType gphContentType) { + this.giphyGif = new GiphyGif(media.getId(), true); + + int start = Math.max(binding.commentEditTextEditCommentActivity.getSelectionStart(), 0); + int end = Math.max(binding.commentEditTextEditCommentActivity.getSelectionEnd(), 0); + int realStart = Math.min(start, end); + if (realStart > 0 && binding.commentEditTextEditCommentActivity.getText().toString().charAt(realStart - 1) != '\n') { + binding.commentEditTextEditCommentActivity.getText().replace(realStart, Math.max(start, end), + "\n![gif](" + giphyGif.id + ")\n", + 0, "\n![gif]()\n".length() + giphyGif.id.length()); + } else { + binding.commentEditTextEditCommentActivity.getText().replace(realStart, Math.max(start, end), + "![gif](" + giphyGif.id + ")\n", + 0, "![gif]()\n".length() + giphyGif.id.length()); + } + } + + @Override + public void onDismissed(@NonNull GPHContentType gphContentType) { + + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/SendComment.java index 41f510af..0972801e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/SendComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/SendComment.java @@ -37,7 +37,8 @@ public class SendComment { params.put(APIUtils.RETURN_RTJSON_KEY, "true"); if (!uploadedImages.isEmpty() || giphyGif != null) { try { - params.put(APIUtils.RICHTEXT_JSON_KEY, new RichTextJSONConverter().constructRichTextJSON(context, commentMarkdown, uploadedImages, giphyGif)); + params.put(APIUtils.RICHTEXT_JSON_KEY, new RichTextJSONConverter().constructRichTextJSON( + context, commentMarkdown, uploadedImages, giphyGif)); params.put(APIUtils.TEXT_KEY, ""); } catch (JSONException e) { sendCommentListener.sendCommentFailed(context.getString(R.string.convert_to_richtext_json_failed)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifBlockParser.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifBlockParser.java index 784a590a..e2cee762 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifBlockParser.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifBlockParser.java @@ -10,10 +10,14 @@ import org.commonmark.parser.block.BlockStart; import org.commonmark.parser.block.MatchedBlockParser; import org.commonmark.parser.block.ParserState; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import ml.docilealligator.infinityforreddit.thing.GiphyGif; +import ml.docilealligator.infinityforreddit.thing.UploadedImage; public class GiphyGifBlockParser extends AbstractBlockParser { private final GiphyGifBlock giphyGifBlock; @@ -37,13 +41,26 @@ public class GiphyGifBlockParser extends AbstractBlockParser { @Nullable private GiphyGif giphyGif; - public Factory(@Nullable GiphyGif giphyGif) { + // Only for editing comments with GiphyGif. No need to convert MediaMetadata to GiphyGif. + @Nullable + private Map uploadedImageMap; + + public Factory(@Nullable GiphyGif giphyGif, @Nullable List uploadedImages) { this.giphyGif = giphyGif; + + if (uploadedImages == null) { + return; + } + + uploadedImageMap = new HashMap<>(); + for (UploadedImage u : uploadedImages) { + uploadedImageMap.put(u.imageName, u); + } } @Override public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) { - if (giphyGif == null) { + if (giphyGif == null && (uploadedImageMap == null || uploadedImageMap.isEmpty())) { return BlockStart.none(); } @@ -54,8 +71,10 @@ public class GiphyGifBlockParser extends AbstractBlockParser { if (startIndex > 0) { int endIndex = line.indexOf(')', startIndex); String id = line.substring(startIndex + 1, endIndex); - if (giphyGif.id.equals(id)) { + if (giphyGif != null && giphyGif.id.equals(id)) { return BlockStart.of(new GiphyGifBlockParser(giphyGif)); + } else if (uploadedImageMap != null && uploadedImageMap.containsKey(id)) { + return BlockStart.of(new GiphyGifBlockParser(new GiphyGif(id, false))); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifPlugin.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifPlugin.java index b76fff58..966d4a4d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifPlugin.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/GiphyGifPlugin.java @@ -5,14 +5,17 @@ import androidx.annotation.Nullable; import org.commonmark.parser.Parser; +import java.util.List; + import io.noties.markwon.AbstractMarkwonPlugin; import ml.docilealligator.infinityforreddit.thing.GiphyGif; +import ml.docilealligator.infinityforreddit.thing.UploadedImage; public class GiphyGifPlugin extends AbstractMarkwonPlugin { private final GiphyGifBlockParser.Factory factory; - public GiphyGifPlugin(@Nullable GiphyGif giphyGif) { - this.factory = new GiphyGifBlockParser.Factory(giphyGif); + public GiphyGifPlugin(@Nullable GiphyGif giphyGif, @Nullable List uploadedImages) { + this.factory = new GiphyGifBlockParser.Factory(giphyGif, uploadedImages); } @NonNull diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java index 6e2826a6..25ba7166 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java @@ -128,12 +128,22 @@ public class RichTextJSONConverter implements Visitor { return richText.toString(); } + /** + * + * @param context + * @param markdown + * @param uploadedImages + * @param giphyGif + * @param uploadedImagesMap this is for editing comment with giphy gifs. Too lazy to convert UploadedImage to GiphyGif. + * @return + * @throws JSONException + */ public String constructRichTextJSON(Context context, String markdown, List uploadedImages, @Nullable GiphyGif giphyGif) throws JSONException { UploadedImagePlugin uploadedImagePlugin = new UploadedImagePlugin(); uploadedImagePlugin.setUploadedImages(uploadedImages); Markwon markwon = MarkdownUtils.createContentSubmissionRedditMarkwon( - context, uploadedImagePlugin, new GiphyGifPlugin(giphyGif)); + context, uploadedImagePlugin, new GiphyGifPlugin(giphyGif, uploadedImages)); List nodes = MarkwonReducer.directChildren().reduce(markwon.parse(markdown)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/thing/GiphyGif.java b/app/src/main/java/ml/docilealligator/infinityforreddit/thing/GiphyGif.java index ef0b7ff0..d8ad11cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/thing/GiphyGif.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/thing/GiphyGif.java @@ -1,8 +1,43 @@ package ml.docilealligator.infinityforreddit.thing; -public class GiphyGif { +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.NonNull; + +public class GiphyGif implements Parcelable { public final String id; - public GiphyGif(String id) { - this.id = "giphy|" + id + "|downsized"; + private GiphyGif(String id) { + this.id = id; + } + + public GiphyGif(String id, boolean modifyId) { + this(modifyId ? "giphy|" + id + "|downsized" : id); + } + + protected GiphyGif(Parcel in) { + id = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public GiphyGif createFromParcel(Parcel in) { + return new GiphyGif(in); + } + + @Override + public GiphyGif[] newArray(int size) { + return new GiphyGif[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(id); } } -- cgit v1.2.3