This commit is contained in:
Thomas 2022-07-26 18:46:58 +02:00
parent 14117b73a3
commit 9f28f64e74
3 changed files with 142 additions and 3 deletions

View File

@ -28,6 +28,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
@ -35,6 +36,7 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
@ -44,6 +46,12 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.github.stom79.mytransl.MyTransL;
import com.github.stom79.mytransl.client.HttpsConnectionException;
import com.github.stom79.mytransl.client.Results;
import com.github.stom79.mytransl.translate.Params;
import com.github.stom79.mytransl.translate.Translate;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@ -137,8 +145,50 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
handler = new Handler();
if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) {
binding.mediaDescription.setText(description);
binding.translate.setOnClickListener(v -> {
String descriptionToTranslate = attachments.get(mediaPosition - 1).description;
MyTransL.translatorEngine et = MyTransL.translatorEngine.LIBRETRANSLATE;
final MyTransL myTransL = MyTransL.getInstance(et);
myTransL.setObfuscation(true);
Params params = new Params();
params.setSplit_sentences(false);
params.setFormat(Params.fType.TEXT);
params.setSource_lang("auto");
myTransL.setLibretranslateDomain("translate.fedilab.app");
String statusToTranslate;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
statusToTranslate = Html.fromHtml(descriptionToTranslate, Html.FROM_HTML_MODE_LEGACY).toString();
else
statusToTranslate = Html.fromHtml(descriptionToTranslate).toString();
myTransL.translate(statusToTranslate, MyTransL.getLocale(), params, new Results() {
@Override
public void onSuccess(Translate translate) {
if (translate.getTranslatedContent() != null) {
attachments.get(mediaPosition - 1).translation = translate.getTranslatedContent();
binding.mediaDescriptionTranslated.setText(translate.getTranslatedContent());
binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE);
binding.mediaDescription.setVisibility(View.GONE);
} else {
Toasty.error(MediaActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFail(HttpsConnectionException httpsConnectionException) {
}
});
});
if (attachments.get(mediaPosition - 1).translation != null) {
binding.mediaDescription.setVisibility(View.GONE);
binding.mediaDescriptionTranslated.setText(attachments.get(mediaPosition - 1).translation);
binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE);
} else {
binding.mediaDescription.setVisibility(View.VISIBLE);
binding.mediaDescriptionTranslated.setVisibility(View.GONE);
}
}
binding.mediaViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
@ -155,6 +205,48 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) {
binding.mediaDescription.setText(description);
}
binding.translate.setOnClickListener(v -> {
String descriptionToTranslate = attachments.get(position).description;
MyTransL.translatorEngine et = MyTransL.translatorEngine.LIBRETRANSLATE;
final MyTransL myTransL = MyTransL.getInstance(et);
myTransL.setObfuscation(true);
Params params = new Params();
params.setSplit_sentences(false);
params.setFormat(Params.fType.TEXT);
params.setSource_lang("auto");
myTransL.setLibretranslateDomain("translate.fedilab.app");
String statusToTranslate;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
statusToTranslate = Html.fromHtml(descriptionToTranslate, Html.FROM_HTML_MODE_LEGACY).toString();
else
statusToTranslate = Html.fromHtml(descriptionToTranslate).toString();
myTransL.translate(statusToTranslate, MyTransL.getLocale(), params, new Results() {
@Override
public void onSuccess(Translate translate) {
if (translate.getTranslatedContent() != null) {
attachments.get(position).translation = translate.getTranslatedContent();
binding.mediaDescriptionTranslated.setText(translate.getTranslatedContent());
binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE);
binding.mediaDescription.setVisibility(View.GONE);
} else {
Toasty.error(MediaActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFail(HttpsConnectionException httpsConnectionException) {
}
});
});
if (attachments.get(position).translation != null) {
binding.mediaDescription.setVisibility(View.GONE);
binding.mediaDescriptionTranslated.setText(attachments.get(position).translation);
binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE);
} else {
binding.mediaDescription.setVisibility(View.VISIBLE);
binding.mediaDescriptionTranslated.setVisibility(View.GONE);
}
}
});
@ -270,7 +362,21 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
handler = new Handler();
if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) {
binding.mediaDescription.setText(description);
if (attachments.get(mediaPosition - 1).translation != null) {
binding.mediaDescription.setVisibility(View.GONE);
binding.mediaDescriptionTranslated.setText(attachments.get(binding.mediaViewpager.getCurrentItem()).translation);
binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE);
} else {
binding.mediaDescription.setVisibility(View.VISIBLE);
binding.mediaDescriptionTranslated.setVisibility(View.GONE);
}
} else {
binding.translate.setVisibility(View.GONE);
}
} else {
binding.translate.setVisibility(View.GONE);
binding.mediaDescriptionTranslated.setVisibility(View.GONE);
binding.mediaDescription.setVisibility(View.GONE);
}
}
break;
@ -323,8 +429,10 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
if (!fullscreen) {
showSystemUI();
binding.mediaDescription.setVisibility(View.VISIBLE);
binding.translate.setVisibility(View.VISIBLE);
} else {
binding.mediaDescription.setVisibility(View.GONE);
binding.translate.setVisibility(View.GONE);
hideSystemUI();
}
}

View File

@ -49,6 +49,7 @@ public class Attachment implements Serializable {
public String peertubeHost = null;
public String peertubeId = null;
public String focus = null;
public String translation = null;
public static class Meta implements Serializable {
@SerializedName("focus")

View File

@ -40,7 +40,7 @@
<TextView
android:id="@+id/media_description"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="70dp"
@ -52,8 +52,38 @@
android:visibility="gone"
tools:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/translate"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/media_description_translated"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="70dp"
android:background="#AA000000"
android:gravity="center"
android:padding="12dp"
android:textColor="#ffffffff"
android:textIsSelectable="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:visibility="visible" />
<com.google.android.material.button.MaterialButton
android:id="@+id/translate"
style="@style/Widget.App.Button.IconOnly.Outline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/translate"
app:icon="@drawable/ic_baseline_translate_24"
app:iconPadding="0dp"
app:layout_constraintBottom_toBottomOf="@+id/media_description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/media_description" />
</androidx.constraintlayout.widget.ConstraintLayout>
</app.futured.hauler.HaulerView>