From 24677ea239786ea53fe1c824d53f3dd182195ab6 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Wed, 1 Nov 2023 19:23:09 +0100 Subject: [PATCH] feat(status): translate media attachments --- .../fragments/BaseStatusListFragment.java | 11 ++++++++ .../android/model/Translation.java | 6 +++++ .../MediaGridStatusDisplayItem.java | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java index a73d7d7228..2964b45224 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java @@ -858,6 +858,10 @@ public void onSuccess(Translation result){ return; status.translation=result; status.translationState=Status.TranslationState.SHOWN; + MediaGridStatusDisplayItem.Holder media=findHolderOfType(itemID, MediaGridStatusDisplayItem.Holder.class); + if (media!=null) { + media.rebind(); + } TextStatusDisplayItem.Holder text=findHolderOfType(itemID, TextStatusDisplayItem.Holder.class); if(text!=null){ text.updateTranslation(true); @@ -896,8 +900,15 @@ public void onError(ErrorResponse error){ }else{ notifyItemChanged(itemID, TextStatusDisplayItem.class); } + + MediaGridStatusDisplayItem.Holder media=findHolderOfType(itemID, MediaGridStatusDisplayItem.Holder.class); + if (media!=null) { + media.rebind(); + } } + private void updateTranslation() {} + public void rebuildAllDisplayItems(){ displayItems.clear(); for(T item:data){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/Translation.java b/mastodon/src/main/java/org/joinmastodon/android/model/Translation.java index 68487451d7..54216c8739 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/Translation.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/Translation.java @@ -7,4 +7,10 @@ public class Translation extends BaseModel{ public String content; public String detectedSourceLanguage; public String provider; + public MediaAttachment[] mediaAttachments; + + public static class MediaAttachment { + public String id; + public String description; + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/MediaGridStatusDisplayItem.java b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/MediaGridStatusDisplayItem.java index 0d066fdf38..c43ff02f6d 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/MediaGridStatusDisplayItem.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/MediaGridStatusDisplayItem.java @@ -12,6 +12,7 @@ import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.text.TextUtils; +import android.util.Pair; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -26,6 +27,7 @@ import org.joinmastodon.android.fragments.BaseStatusListFragment; import org.joinmastodon.android.model.Attachment; import org.joinmastodon.android.model.Status; +import org.joinmastodon.android.model.Translation; import org.joinmastodon.android.ui.OutlineProviders; import org.joinmastodon.android.ui.PhotoLayoutHelper; import org.joinmastodon.android.ui.drawables.SpoilerStripesDrawable; @@ -38,7 +40,12 @@ import org.joinmastodon.android.utils.TypedObjectPool; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import me.grishka.appkit.imageloader.ImageLoaderViewHolder; import me.grishka.appkit.imageloader.requests.ImageLoaderRequest; @@ -52,6 +59,7 @@ public class MediaGridStatusDisplayItem extends StatusDisplayItem{ private PhotoLayoutHelper.TiledLayoutResult tiledLayout; private final TypedObjectPool viewPool; private final List attachments; + private final Map> translatedAttachments = new HashMap<>(); private final ArrayList requests=new ArrayList<>(); public final Status status; public String sensitiveTitle; @@ -189,6 +197,25 @@ public void onBind(MediaGridStatusDisplayItem item){ c.btnsWrap.setAlpha(1f); } controllers.add(c); + + if (item.status.translation != null){ + if(item.status.translationState==Status.TranslationState.SHOWN){ + if(!item.translatedAttachments.containsKey(att.id)){ + Optional translatedAttachment=Arrays.stream(item.status.translation.mediaAttachments).filter(mediaAttachment->mediaAttachment.id.equals(att.id)).findFirst(); + translatedAttachment.ifPresent(mediaAttachment->{ + item.translatedAttachments.put(mediaAttachment.id, new Pair<>(att.description, mediaAttachment.description)); + att.description=mediaAttachment.description; + }); + }else{ + //SAFETY: must be non-null, as we check if the map contains the attachment before + att.description=Objects.requireNonNull(item.translatedAttachments.get(att.id)).second; + } + }else{ + if (item.translatedAttachments.containsKey(att.id)) { + att.description=Objects.requireNonNull(item.translatedAttachments.get(att.id)).first; + } + } + } c.bind(att, item.status); i++; }