Fix issue #623 - Allow to display the translate button at the bottom.

This commit is contained in:
Thomas 2022-12-12 12:18:19 +01:00
parent 1b643bcb1d
commit 5a0eff4ae8
4 changed files with 85 additions and 37 deletions

View file

@ -395,6 +395,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
boolean confirmBoost = sharedpreferences.getBoolean(context.getString(R.string.SET_NOTIF_VALIDATION), true);
boolean fullAttachement = sharedpreferences.getBoolean(context.getString(R.string.SET_FULL_PREVIEW), false);
boolean displayBookmark = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_BOOKMARK), true);
boolean displayTranslate = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_TRANSLATE), false);
boolean displayCounters = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_COUNTER_FAV_BOOST), false);
String loadMediaType = sharedpreferences.getString(context.getString(R.string.SET_LOAD_MEDIA_TYPE), "ALWAYS");
@ -628,6 +629,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} else {
holder.binding.actionButtonBookmark.setVisibility(View.GONE);
}
if (displayTranslate) {
holder.binding.actionButtonTranslate.setVisibility(View.VISIBLE);
} else {
holder.binding.actionButtonTranslate.setVisibility(View.GONE);
}
//--- ACTIONS ---
holder.binding.actionButtonBookmark.setChecked(statusToDeal.bookmarked);
//---> BOOKMARK/UNBOOKMARK
@ -635,6 +641,9 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
CrossActionHelper.doCrossAction(context, CrossActionHelper.TypeOfCrossAction.BOOKMARK_ACTION, null, statusToDeal);
return true;
});
holder.binding.actionButtonTranslate.setOnClickListener(v -> {
translate(context, statusToDeal, holder, adapter);
});
holder.binding.actionButtonBookmark.setOnClickListener(v -> {
if (remote) {
Toasty.info(context, context.getString(R.string.retrieve_remote_status), Toasty.LENGTH_SHORT).show();
@ -887,6 +896,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
holder.binding.actionButtonReply.getLayoutParams().width = (int) (normalSize * scaleIcon);
holder.binding.actionButtonReply.getLayoutParams().height = (int) (normalSize * scaleIcon);
holder.binding.actionButtonReply.requestLayout();
holder.binding.actionButtonTranslate.getLayoutParams().width = (int) (normalSize * scaleIcon);
holder.binding.actionButtonTranslate.getLayoutParams().height = (int) (normalSize * scaleIcon);
holder.binding.actionButtonTranslate.requestLayout();
holder.binding.actionButtonBoost.setImageSize((int) (normalSize * scaleIcon));
holder.binding.actionButtonFavorite.setImageSize((int) (normalSize * scaleIcon));
holder.binding.actionButtonBookmark.setImageSize((int) (normalSize * scaleIcon));
@ -1676,41 +1690,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}));
builderInner.show();
} else if (itemId == R.id.action_translate) {
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;
String translate = sharedpreferences.getString(context.getString(R.string.SET_LIVE_TRANSLATE), MyTransL.getLocale());
if (translate != null && translate.equalsIgnoreCase("default")) {
translate = MyTransL.getLocale();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
statusToTranslate = Html.fromHtml(statusToDeal.content, Html.FROM_HTML_MODE_LEGACY).toString();
else
statusToTranslate = Html.fromHtml(statusToDeal.content).toString();
myTransL.translate(statusToTranslate, translate, params, new Results() {
@Override
public void onSuccess(Translate translate) {
if (translate.getTranslatedContent() != null) {
statusToDeal.translationShown = true;
statusToDeal.translationContent = translate.getTranslatedContent();
adapter.notifyItemChanged(holder.getBindingAdapterPosition());
} else {
Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFail(HttpsConnectionException httpsConnectionException) {
}
});
translate(context, statusToDeal, holder, adapter);
return true;
} else if (itemId == R.id.action_report) {
Intent intent = new Intent(context, ReportActivity.class);
@ -1918,6 +1898,46 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}
private static void translate(Context context, Status statusToDeal,
StatusViewHolder holder,
RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
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;
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
String translate = sharedpreferences.getString(context.getString(R.string.SET_LIVE_TRANSLATE), MyTransL.getLocale());
if (translate != null && translate.equalsIgnoreCase("default")) {
translate = MyTransL.getLocale();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
statusToTranslate = Html.fromHtml(statusToDeal.content, Html.FROM_HTML_MODE_LEGACY).toString();
else
statusToTranslate = Html.fromHtml(statusToDeal.content).toString();
myTransL.translate(statusToTranslate, translate, params, new Results() {
@Override
public void onSuccess(Translate translate) {
if (translate.getTranslatedContent() != null) {
statusToDeal.translationShown = true;
statusToDeal.translationContent = translate.getTranslatedContent();
adapter.notifyItemChanged(holder.getBindingAdapterPosition());
} else {
Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFail(HttpsConnectionException httpsConnectionException) {
}
});
}
private static void loadAndAddAttachment(Context context, LayoutMediaBinding layoutMediaBinding,
StatusViewHolder holder,
@ -2207,6 +2227,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
Helper.changeDrawableColor(context, R.drawable.ic_person, theme_icons_color);
Helper.changeDrawableColor(context, R.drawable.ic_bot, theme_icons_color);
Helper.changeDrawableColor(context, R.drawable.ic_round_reply_24, theme_icons_color);
Helper.changeDrawableColor(context, holder.binding.actionButtonTranslate, theme_icons_color);
holder.binding.actionButtonFavorite.setInActiveImageTintColor(theme_icons_color);
holder.binding.actionButtonBookmark.setInActiveImageTintColor(theme_icons_color);
holder.binding.actionButtonBoost.setInActiveImageTintColor(theme_icons_color);

View file

@ -659,7 +659,7 @@
<com.varunest.sparkbutton.SparkButton
android:id="@+id/action_button_bookmark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/status_add_custom_emoji"
app:layout_constraintEnd_toStartOf="@+id/action_button_translate"
app:layout_constraintStart_toEndOf="@+id/action_button_favorite"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="48dp"
@ -675,12 +675,30 @@
app:sparkbutton_secondaryColor="@color/marked_icon" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/action_button_translate"
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="@string/translate"
android:focusable="true"
android:src="@drawable/ic_baseline_translate_24"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/status_add_custom_emoji"
app:layout_constraintStart_toEndOf="@+id/action_button_bookmark"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/status_add_custom_emoji"
style="@style/Widget.Material3.Button.IconButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/status_emoji"
app:layout_constraintStart_toEndOf="@+id/action_button_bookmark"
app:layout_constraintStart_toEndOf="@+id/action_button_translate"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="48dp"
android:layout_height="48dp"

View file

@ -1356,6 +1356,7 @@
<string name="SET_FILTER_REGEX_PUBLIC" translatable="false">SET_FILTER_REGEX_PUBLIC</string>
<string name="SET_NOTIF_VALIDATION" translatable="false">SET_NOTIF_VALIDATION</string>
<string name="SET_DISPLAY_BOOKMARK" translatable="false">SET_DISPLAY_BOOKMARK</string>
<string name="SET_DISPLAY_TRANSLATE" translatable="false">SET_DISPLAY_TRANSLATE</string>
<string name="SET_NOTIF_VALIDATION_FAV" translatable="false">SET_NOTIF_VALIDATION_FAV</string>
<string name="SET_DISPLAY_COUNTER_FAV_BOOST" translatable="false">SET_DISPLAY_COUNTER_FAV_BOOST</string>
<string name="SET_INNER_MARKER" translatable="false">SET_INNER_MARKER</string>
@ -1423,6 +1424,7 @@
<string name="poll_type">Poll type:</string>
<string name="poll_duration">Poll duration:</string>
<string name="set_display_bookmark_indication">Always display bookmark button</string>
<string name="set_display_translate_indication">Always display translate button</string>
<string name="display">Display</string>
<string name="bottom_menu">Bottom menu</string>
<string name="top_menu">Top bar menu</string>

View file

@ -42,6 +42,13 @@
app:singleLineTitle="false"
app:title="@string/set_display_bookmark_indication" />
<SwitchPreferenceCompat
android:defaultValue="false"
app:iconSpaceReserved="false"
app:key="@string/SET_DISPLAY_TRANSLATE"
app:singleLineTitle="false"
app:title="@string/set_display_translate_indication" />
<SwitchPreferenceCompat
app:defaultValue="true"
app:iconSpaceReserved="false"