Filter language improvements

pull/199/head
M66B 3 years ago
parent 4739f05906
commit 9beef6f7cb

@ -602,8 +602,7 @@ public interface DaoMessage {
" AND (:folder IS NULL OR message.folder = :folder)" + " AND (:folder IS NULL OR message.folder = :folder)" +
" AND NOT message.ui_hide" + " AND NOT message.ui_hide" +
" AND NOT message.language IS NULL" + " AND NOT message.language IS NULL" +
" GROUP BY language" + " GROUP BY language")
" ORDER BY COUNT(*) DESC")
List<String> getLanguages(Long account, Long folder); List<String> getLanguages(Long account, Long folder);
@Insert @Insert

@ -60,6 +60,7 @@ import android.provider.Settings;
import android.security.KeyChain; import android.security.KeyChain;
import android.security.KeyChainException; import android.security.KeyChainException;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
@ -69,6 +70,7 @@ import android.util.Base64;
import android.util.LongSparseArray; import android.util.LongSparseArray;
import android.util.Pair; import android.util.Pair;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -4489,30 +4491,53 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
args.putLong("account", account); args.putLong("account", account);
args.putLong("folder", folder); args.putLong("folder", folder);
new SimpleTask<List<String>>() { new SimpleTask<List<Locale>>() {
@Override @Override
protected List<String> onExecute(Context context, Bundle args) { protected List<Locale> onExecute(Context context, Bundle args) {
long account = args.getLong("account"); long account = args.getLong("account");
long folder = args.getLong("folder"); long folder = args.getLong("folder");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
return db.message().getLanguages(account < 0 ? null : account, folder < 0 ? null : folder); List<String> languages = db.message().getLanguages(
account < 0 ? null : account,
folder < 0 ? null : folder);
List<Locale> locales = new ArrayList<>();
for (String language : languages)
locales.add(new Locale(language));
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(locales, new Comparator<Locale>() {
@Override
public int compare(Locale l1, Locale l2) {
return collator.compare(l1.getDisplayLanguage(), l2.getDisplayLanguage());
}
});
return locales;
} }
@Override @Override
protected void onExecuted(Bundle args, List<String> languages) { protected void onExecuted(Bundle args, List<Locale> locales) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String current = prefs.getString("filter_language", null); String current = prefs.getString("filter_language", null);
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), vwAnchor); PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), vwAnchor);
String all = getString(R.string.title_language_all) + (current == null ? " ★" : ""); SpannableStringBuilder all = new SpannableStringBuilder(getString(R.string.title_language_all));
if (current == null)
all.setSpan(new StyleSpan(Typeface.BOLD), 0, all.length(), 0);
popupMenu.getMenu().add(Menu.NONE, 0, 0, all); popupMenu.getMenu().add(Menu.NONE, 0, 0, all);
for (int i = 0; i < languages.size(); i++) { for (int i = 0; i < locales.size(); i++) {
String language = languages.get(i); Locale locale = locales.get(i);
Locale locale = new Locale(language); String language = locale.getLanguage();
String title = locale.getDisplayLanguage() + (language.equals(current) ? " ★" : ""); SpannableStringBuilder title = new SpannableStringBuilder(locale.getDisplayLanguage());
if (language.equals(current))
title.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), 0);
popupMenu.getMenu() popupMenu.getMenu()
.add(Menu.NONE, i + 1, i + 1, title) .add(Menu.NONE, i + 1, i + 1, title)
.setIntent(new Intent().putExtra("locale", locale)); .setIntent(new Intent().putExtra("locale", locale));

Loading…
Cancel
Save