Added favorite languages menu separator

pull/204/head
M66B 3 years ago
parent f2a9708a90
commit 37db782e3c

@ -6731,7 +6731,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
final TextView tvText = view.findViewById(R.id.tvText); final TextView tvText = view.findViewById(R.id.tvText);
final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait); final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
List<DeepL.Language> languages = DeepL.getTargetLanguages(context); List<DeepL.Language> languages = DeepL.getTargetLanguages(context, false);
ArrayAdapter<DeepL.Language> adapter = new ArrayAdapter<DeepL.Language>(context, android.R.layout.simple_spinner_item, android.R.id.text1, languages) { ArrayAdapter<DeepL.Language> adapter = new ArrayAdapter<DeepL.Language>(context, android.R.layout.simple_spinner_item, android.R.id.text1, languages) {
@NonNull @NonNull
@Override @Override

@ -86,7 +86,7 @@ public class DeepL {
return !TextUtils.isEmpty(deepl_key); return !TextUtils.isEmpty(deepl_key);
} }
public static List<Language> getTargetLanguages(Context context) { public static List<Language> getTargetLanguages(Context context, boolean favorites) {
try { try {
ensureLanguages(context); ensureLanguages(context);
@ -106,13 +106,13 @@ public class DeepL {
name = locale.getDisplayName(); name = locale.getDisplayName();
int frequency = prefs.getInt("translated_" + target, 0); int frequency = prefs.getInt("translated_" + target, 0);
if (BuildConfig.DEBUG && frequency > 0)
name += " ★";
String resname = "language_" + target.toLowerCase().replace('-', '_'); String resname = "language_" + target.toLowerCase().replace('-', '_');
int resid = res.getIdentifier(resname, "drawable", pkg); int resid = res.getIdentifier(resname, "drawable", pkg);
languages.add(new Language(name, target, resid == 0 ? null : resid)); languages.add(new Language(name, target,
resid == 0 ? null : resid,
favorites && frequency > 0));
frequencies.put(target, frequency); frequencies.put(target, frequency);
} }
@ -124,7 +124,7 @@ public class DeepL {
int freq1 = frequencies.get(l1.target); int freq1 = frequencies.get(l1.target);
int freq2 = frequencies.get(l2.target); int freq2 = frequencies.get(l2.target);
if (freq1 == freq2 || !BuildConfig.DEBUG) if (freq1 == freq2 || !favorites)
return collator.compare(l1.name, l2.name); return collator.compare(l1.name, l2.name);
else else
return -Integer.compare(freq1, freq2); return -Integer.compare(freq1, freq2);
@ -290,11 +290,13 @@ public class DeepL {
public String name; public String name;
public String target; public String target;
public Integer icon; public Integer icon;
public boolean favorite;
private Language(String name, String target, Integer icon) { private Language(String name, String target, Integer icon, boolean favorit) {
this.name = name; this.name = name;
this.target = target; this.target = target;
this.icon = icon; this.icon = icon;
this.favorite = favorit;
} }
@Override @Override

@ -114,6 +114,7 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SwitchCompat; import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.view.MenuCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import androidx.cursoradapter.widget.SimpleCursorAdapter; import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
@ -2043,7 +2044,8 @@ public class FragmentCompose extends FragmentBase {
private void onTranslate(View anchor) { private void onTranslate(View anchor) {
final Context context = anchor.getContext(); final Context context = anchor.getContext();
List<DeepL.Language> languages = DeepL.getTargetLanguages(context); boolean grouped = BuildConfig.DEBUG;
List<DeepL.Language> languages = DeepL.getTargetLanguages(context, grouped);
if (languages == null) if (languages == null)
languages = new ArrayList<>(); languages = new ArrayList<>();
@ -2056,13 +2058,17 @@ public class FragmentCompose extends FragmentBase {
for (int i = 0; i < languages.size(); i++) { for (int i = 0; i < languages.size(); i++) {
DeepL.Language lang = languages.get(i); DeepL.Language lang = languages.get(i);
MenuItem item = popupMenu.getMenu().add(Menu.NONE, i + 2, i + 2, lang.name) MenuItem item = popupMenu.getMenu()
.add(lang.favorite ? Menu.FIRST : Menu.NONE, i + 2, i + 2, lang.name)
.setIntent(new Intent().putExtra("target", lang.target)); .setIntent(new Intent().putExtra("target", lang.target));
if (lang.icon != null) if (lang.icon != null)
item.setIcon(lang.icon); item.setIcon(lang.icon);
item.setEnabled(canTranslate); item.setEnabled(canTranslate);
} }
if (grouped)
MenuCompat.setGroupDividerEnabled(popupMenu.getMenu(), true);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {

Loading…
Cancel
Save