Adding translate button from megalodon
This commit is contained in:
parent
7abf15e9e0
commit
04bfdba50e
|
@ -8,16 +8,20 @@ import android.text.TextUtils;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.joinmastodon.android.R;
|
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.fragments.BaseStatusListFragment;
|
||||||
|
import org.joinmastodon.android.model.Instance;
|
||||||
import org.joinmastodon.android.model.Status;
|
import org.joinmastodon.android.model.Status;
|
||||||
import org.joinmastodon.android.model.StatusTranslation;
|
|
||||||
import org.joinmastodon.android.ui.drawables.SpoilerStripesDrawable;
|
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.text.HtmlParser;
|
||||||
import org.joinmastodon.android.ui.utils.CustomEmojiHelper;
|
import org.joinmastodon.android.ui.utils.CustomEmojiHelper;
|
||||||
import org.joinmastodon.android.ui.views.LinkedTextView;
|
import org.joinmastodon.android.ui.views.LinkedTextView;
|
||||||
|
@ -35,18 +39,26 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
|
||||||
private CharSequence parsedSpoilerText;
|
private CharSequence parsedSpoilerText;
|
||||||
public boolean textSelectable;
|
public boolean textSelectable;
|
||||||
public final Status status;
|
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){
|
public TextStatusDisplayItem(String parentID, CharSequence text, BaseStatusListFragment parentFragment, Status status){
|
||||||
super(parentID, parentFragment);
|
super(parentID, parentFragment);
|
||||||
this.text=text;
|
this.text=text;
|
||||||
this.status=status;
|
this.status=status;
|
||||||
// this.wantsTranslation=wantsTranslation;
|
|
||||||
emojiHelper.setText(text);
|
emojiHelper.setText(text);
|
||||||
if(!TextUtils.isEmpty(status.spoilerText)){
|
if(!TextUtils.isEmpty(status.spoilerText)){
|
||||||
parsedSpoilerText=HtmlParser.parseCustomEmoji(status.spoilerText, status.emojis);
|
parsedSpoilerText=HtmlParser.parseCustomEmoji(status.spoilerText, status.emojis);
|
||||||
spoilerEmojiHelper=new CustomEmojiHelper();
|
spoilerEmojiHelper=new CustomEmojiHelper();
|
||||||
spoilerEmojiHelper.setText(parsedSpoilerText);
|
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
|
@Override
|
||||||
|
@ -71,9 +83,10 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
|
||||||
public static class Holder extends StatusDisplayItem.Holder<TextStatusDisplayItem> implements ImageLoaderViewHolder{
|
public static class Holder extends StatusDisplayItem.Holder<TextStatusDisplayItem> implements ImageLoaderViewHolder{
|
||||||
private final LinkedTextView text;
|
private final LinkedTextView text;
|
||||||
private final LinearLayout spoilerHeader;
|
private final LinearLayout spoilerHeader;
|
||||||
private final TextView spoilerTitle, spoilerTitleInline;
|
private final TextView spoilerTitle, spoilerTitleInline, translateInfo;
|
||||||
private final View spoilerOverlay, borderTop, borderBottom;
|
private final View spoilerOverlay, borderTop, borderBottom, textWrap, translateWrap;
|
||||||
private final Drawable backgroundColor, borderColor;
|
private final Drawable backgroundColor, borderColor;
|
||||||
|
private final Button translateButton;
|
||||||
|
|
||||||
public Holder(Activity activity, ViewGroup parent){
|
public Holder(Activity activity, ViewGroup parent){
|
||||||
super(activity, R.layout.display_item_text, parent);
|
super(activity, R.layout.display_item_text, parent);
|
||||||
|
@ -84,6 +97,10 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
|
||||||
spoilerOverlay=findViewById(R.id.spoiler_overlay);
|
spoilerOverlay=findViewById(R.id.spoiler_overlay);
|
||||||
borderTop=findViewById(R.id.border_top);
|
borderTop=findViewById(R.id.border_top);
|
||||||
borderBottom=findViewById(R.id.border_bottom);
|
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));
|
itemView.setOnClickListener(v->item.parentFragment.onRevealSpoilerClick(this));
|
||||||
|
|
||||||
TypedValue outValue=new TypedValue();
|
TypedValue outValue=new TypedValue();
|
||||||
|
@ -97,32 +114,9 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBind(TextStatusDisplayItem item){
|
public void onBind(TextStatusDisplayItem item){
|
||||||
if(item.status.wantsTranslation && item.status.translation == null){
|
text.setText(item.translated
|
||||||
new GetStatusTranslation(item.status.id)
|
? HtmlParser.parse(item.translation.content, item.status.emojis, item.status.mentions, item.status.tags, item.parentFragment.getAccountID())
|
||||||
.setCallback(new Callback<StatusTranslation>(){
|
: item.text);
|
||||||
@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.setTextIsSelectable(item.textSelectable);
|
text.setTextIsSelectable(item.textSelectable);
|
||||||
spoilerTitleInline.setTextIsSelectable(item.textSelectable);
|
spoilerTitleInline.setTextIsSelectable(item.textSelectable);
|
||||||
text.setInvalidateOnEveryFrame(false);
|
text.setInvalidateOnEveryFrame(false);
|
||||||
|
@ -136,20 +130,47 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
|
||||||
if(item.status.spoilerRevealed){
|
if(item.status.spoilerRevealed){
|
||||||
spoilerOverlay.setVisibility(View.GONE);
|
spoilerOverlay.setVisibility(View.GONE);
|
||||||
spoilerHeader.setVisibility(View.VISIBLE);
|
spoilerHeader.setVisibility(View.VISIBLE);
|
||||||
text.setVisibility(View.VISIBLE);
|
textWrap.setVisibility(View.VISIBLE);
|
||||||
itemView.setClickable(false);
|
itemView.setClickable(false);
|
||||||
}else{
|
}else{
|
||||||
spoilerOverlay.setVisibility(View.VISIBLE);
|
spoilerOverlay.setVisibility(View.VISIBLE);
|
||||||
spoilerHeader.setVisibility(View.GONE);
|
spoilerHeader.setVisibility(View.GONE);
|
||||||
text.setVisibility(View.GONE);
|
textWrap.setVisibility(View.GONE);
|
||||||
itemView.setClickable(true);
|
itemView.setClickable(true);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
spoilerOverlay.setVisibility(View.GONE);
|
spoilerOverlay.setVisibility(View.GONE);
|
||||||
spoilerHeader.setVisibility(View.GONE);
|
spoilerHeader.setVisibility(View.GONE);
|
||||||
text.setVisibility(View.VISIBLE);
|
textWrap.setVisibility(View.VISIBLE);
|
||||||
itemView.setClickable(false);
|
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
|
@Override
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
android:paddingBottom="12dp">
|
android:paddingBottom="12dp">
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
android:id="@+id/text_wrap"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
@ -49,8 +50,39 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingHorizontal="16dp"
|
android:paddingHorizontal="16dp"
|
||||||
android:textSize="16sp"
|
android:textSize="16sp"
|
||||||
android:textAppearance="@style/m3_body_large"
|
android:textAppearance="@style/m3_body_large"/>
|
||||||
tools:text="setting up my mstdn"/>
|
|
||||||
|
<org.joinmastodon.android.ui.views.AutoOrientationLinearLayout
|
||||||
|
android:id="@+id/translate_wrap"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
style="?secondaryButtonStyle"
|
||||||
|
android:background="?android:selectableItemBackground"
|
||||||
|
android:textColor="?android:textColorSecondary"
|
||||||
|
android:id="@+id/translate_btn"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="8dp"
|
||||||
|
android:paddingHorizontal="8dp"
|
||||||
|
android:text="Translate"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/translate_info"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="16dp"
|
||||||
|
android:layout_marginVertical="4dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:textColor="?android:textColorSecondary"
|
||||||
|
android:textAlignment="textEnd"
|
||||||
|
tools:text="Translated using TranslateEngine" />
|
||||||
|
|
||||||
|
</org.joinmastodon.android.ui.views.AutoOrientationLinearLayout>
|
||||||
|
|
||||||
|
|
||||||
<!-- <Button-->
|
<!-- <Button-->
|
||||||
<!-- android:id="@+id/translate"-->
|
<!-- android:id="@+id/translate"-->
|
||||||
|
@ -69,7 +101,6 @@
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
android:id="@+id/spoiler_overlay"
|
android:id="@+id/spoiler_overlay"
|
||||||
|
|
Loading…
Reference in New Issue