1
0
Fork 0

language list is now properly sorted on Android < 7.0

This commit is contained in:
sspanak 2024-02-24 10:49:04 +02:00 committed by Dimo Karaivanov
parent 29e8ebd9f5
commit 2547eda8bd
2 changed files with 18 additions and 8 deletions

View file

@ -7,7 +7,7 @@ import java.util.HashMap;
import java.util.Locale;
public class Language {
public class Language implements Comparable<Language> {
public static String SPECIAL_CHARS_KEY = "0";
public static String PUNCTUATION_KEY = "1";
@ -271,4 +271,12 @@ public class Language {
public String toString() {
return getName();
}
@Override
public int compareTo(Language other) {
String key = getName().equals("Suomi") ? "su" : getLocale().toString();
String otherKey = other.getName().equals("Suomi") ? "su" : other.getLocale().toString();
return key.compareTo(otherKey);
}
}

View file

@ -1,12 +1,11 @@
package io.github.sspanak.tt9.languages;
import android.content.Context;
import android.os.Build;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.HashMap;
import io.github.sspanak.tt9.Logger;
@ -77,10 +76,11 @@ public class LanguageCollection {
}
}
if (sort && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
langList.sort(Comparator.comparing(l -> l.getLocale().toString()));
if (sort) {
Collections.sort(langList);
}
return langList;
}
@ -91,8 +91,8 @@ public class LanguageCollection {
public static ArrayList<Language> getAll(Context context, boolean sort) {
ArrayList<Language> langList = new ArrayList<>(getInstance(context).languages.values());
if (sort && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
langList.sort(Comparator.comparing(l -> l.getLocale().toString()));
if (sort) {
Collections.sort(langList);
}
return langList;
@ -108,7 +108,9 @@ public class LanguageCollection {
for (int i = 0; i < listSize; i++) {
stringList.append(list.get(i));
stringList.append((i < listSize - 1) ? ", " : " ");
if (i < listSize - 1) {
stringList.append(", ");
}
}
return stringList.toString();