diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java index baf4db83c..2e0d7b159 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java @@ -8,16 +8,20 @@ import android.text.TextUtils; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.LinearLayout; +import android.widget.Button; import android.widget.TextView; import org.joinmastodon.android.R; -import org.joinmastodon.android.api.requests.statuses.GetStatusTranslation; +import org.joinmastodon.android.api.requests.statuses.TranslateStatus; +import org.joinmastodon.android.api.session.AccountSession; +import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.fragments.BaseStatusListFragment; +import org.joinmastodon.android.model.Instance; import org.joinmastodon.android.model.Status; -import org.joinmastodon.android.model.StatusTranslation; import org.joinmastodon.android.ui.drawables.SpoilerStripesDrawable; +import org.joinmastodon.android.model.StatusPrivacy; +import org.joinmastodon.android.model.TranslatedStatus; import org.joinmastodon.android.ui.text.HtmlParser; import org.joinmastodon.android.ui.utils.CustomEmojiHelper; import org.joinmastodon.android.ui.views.LinkedTextView; @@ -35,18 +39,26 @@ public class TextStatusDisplayItem extends StatusDisplayItem{ private CharSequence parsedSpoilerText; public boolean textSelectable; public final Status status; + public boolean translated = false; + public TranslatedStatus translation = null; + + private AccountSession session; + private Instance instanceInfo; + private boolean translateEnabled; public TextStatusDisplayItem(String parentID, CharSequence text, BaseStatusListFragment parentFragment, Status status){ super(parentID, parentFragment); this.text=text; this.status=status; -// this.wantsTranslation=wantsTranslation; emojiHelper.setText(text); if(!TextUtils.isEmpty(status.spoilerText)){ parsedSpoilerText=HtmlParser.parseCustomEmoji(status.spoilerText, status.emojis); spoilerEmojiHelper=new CustomEmojiHelper(); spoilerEmojiHelper.setText(parsedSpoilerText); } + session = AccountSessionManager.getInstance().getAccount(parentFragment.getAccountID()); + instanceInfo = AccountSessionManager.getInstance().getInstanceInfo(session.domain); + translateEnabled = instanceInfo.v2 != null && instanceInfo.v2.configuration.translation != null && instanceInfo.v2.configuration.translation.enabled; } @Override @@ -71,9 +83,10 @@ public class TextStatusDisplayItem extends StatusDisplayItem{ public static class Holder extends StatusDisplayItem.Holder implements ImageLoaderViewHolder{ private final LinkedTextView text; private final LinearLayout spoilerHeader; - private final TextView spoilerTitle, spoilerTitleInline; - private final View spoilerOverlay, borderTop, borderBottom; + private final TextView spoilerTitle, spoilerTitleInline, translateInfo; + private final View spoilerOverlay, borderTop, borderBottom, textWrap, translateWrap; private final Drawable backgroundColor, borderColor; + private final Button translateButton; public Holder(Activity activity, ViewGroup parent){ super(activity, R.layout.display_item_text, parent); @@ -84,6 +97,10 @@ public class TextStatusDisplayItem extends StatusDisplayItem{ spoilerOverlay=findViewById(R.id.spoiler_overlay); borderTop=findViewById(R.id.border_top); borderBottom=findViewById(R.id.border_bottom); + textWrap=findViewById(R.id.text_wrap); + translateWrap=findViewById(R.id.translate_wrap); + translateButton=findViewById(R.id.translate_btn); + translateInfo=findViewById(R.id.translate_info); itemView.setOnClickListener(v->item.parentFragment.onRevealSpoilerClick(this)); TypedValue outValue=new TypedValue(); @@ -97,32 +114,9 @@ public class TextStatusDisplayItem extends StatusDisplayItem{ @Override public void onBind(TextStatusDisplayItem item){ - if(item.status.wantsTranslation && item.status.translation == null){ - new GetStatusTranslation(item.status.id) - .setCallback(new Callback(){ - @Override - public void onSuccess(StatusTranslation statusTranslation){ - item.status.translation = statusTranslation.getStrippedText(); - text.setText(item.status.translation); - } - @Override - public void onError(ErrorResponse error){ - item.status.wantsTranslation=false; - text.setText(item.text); - error.showToast(item.parentFragment.getActivity()); - } - }) - .wrapProgress(item.parentFragment.getActivity(), R.string.loading, true) - .exec(item.parentFragment.getAccountID()); - } - if(item.status.wantsTranslation){ - if(item.status.translation != null) - { - text.setText(item.status.translation); - } - }else{ - text.setText(item.text); - } + text.setText(item.translated + ? HtmlParser.parse(item.translation.content, item.status.emojis, item.status.mentions, item.status.tags, item.parentFragment.getAccountID()) + : item.text); text.setTextIsSelectable(item.textSelectable); spoilerTitleInline.setTextIsSelectable(item.textSelectable); text.setInvalidateOnEveryFrame(false); @@ -136,20 +130,47 @@ public class TextStatusDisplayItem extends StatusDisplayItem{ if(item.status.spoilerRevealed){ spoilerOverlay.setVisibility(View.GONE); spoilerHeader.setVisibility(View.VISIBLE); - text.setVisibility(View.VISIBLE); + textWrap.setVisibility(View.VISIBLE); itemView.setClickable(false); }else{ spoilerOverlay.setVisibility(View.VISIBLE); spoilerHeader.setVisibility(View.GONE); - text.setVisibility(View.GONE); + textWrap.setVisibility(View.GONE); itemView.setClickable(true); } }else{ spoilerOverlay.setVisibility(View.GONE); spoilerHeader.setVisibility(View.GONE); - text.setVisibility(View.VISIBLE); + textWrap.setVisibility(View.VISIBLE); itemView.setClickable(false); } + + translateWrap.setVisibility(item.textSelectable && item.translateEnabled && + !item.status.visibility.isLessVisibleThan(StatusPrivacy.UNLISTED) && + (item.session.preferences == null || !item.status.language.equalsIgnoreCase(item.session.preferences.postingDefaultLanguage)) + ? View.VISIBLE : View.GONE); + translateButton.setText(item.translated ? R.string.sk_translate_show_original : R.string.sk_translate_post); + translateInfo.setText(item.translated ? itemView.getResources().getString(R.string.sk_translated_using, item.translation.provider) : ""); + translateButton.setOnClickListener(v->{ + if (item.translation == null) { + new TranslateStatus(item.status.id).setCallback(new Callback<>() { + @Override + public void onSuccess(TranslatedStatus translatedStatus) { + item.translation = translatedStatus; + item.translated = true; + rebind(); + } + + @Override + public void onError(ErrorResponse error) { + error.showToast(itemView.getContext()); + } + }).exec(item.parentFragment.getAccountID()); + } else { + item.translated = !item.translated; + rebind(); + } + }); } @Override diff --git a/mastodon/src/main/res/layout/display_item_text.xml b/mastodon/src/main/res/layout/display_item_text.xml index cd6d935ab..ec33f1f56 100644 --- a/mastodon/src/main/res/layout/display_item_text.xml +++ b/mastodon/src/main/res/layout/display_item_text.xml @@ -7,6 +7,7 @@ android:paddingBottom="12dp"> @@ -49,8 +50,39 @@ android:layout_height="wrap_content" android:paddingHorizontal="16dp" android:textSize="16sp" - android:textAppearance="@style/m3_body_large" - tools:text="setting up my mstdn"/> + android:textAppearance="@style/m3_body_large"/> + + + +