Language selector

This commit is contained in:
Thomas 2022-11-15 15:43:53 +01:00
parent 197a8d56e1
commit 81c012c8f0
4 changed files with 80 additions and 66 deletions

View File

@ -64,7 +64,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@ -79,7 +78,6 @@ import app.fedilab.android.client.entities.api.Mention;
import app.fedilab.android.client.entities.api.ScheduledStatus;
import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.client.entities.app.BaseAccount;
import app.fedilab.android.client.entities.app.Languages;
import app.fedilab.android.client.entities.app.StatusDraft;
import app.fedilab.android.databinding.ActivityPaginationBinding;
import app.fedilab.android.databinding.PopupContactBinding;
@ -383,61 +381,6 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
} else {
Toasty.info(ComposeActivity.this, getString(R.string.toot_error_no_content), Toasty.LENGTH_SHORT).show();
}
} else if (item.getItemId() == R.id.action_language) {
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ComposeActivity.this);
Set<String> storedLanguages = sharedpreferences.getStringSet(getString(R.string.SET_SELECTED_LANGUAGE), null);
String[] codesArr = new String[0];
String[] languagesArr = new String[0];
String currentCode = sharedpreferences.getString(getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, null);
int selection = 0;
if (storedLanguages != null && storedLanguages.size() > 0) {
int i = 0;
codesArr = new String[storedLanguages.size()];
languagesArr = new String[storedLanguages.size()];
for (String language : storedLanguages) {
codesArr[i] = language;
languagesArr[i] = language;
if (currentCode != null && currentCode.equalsIgnoreCase(language)) {
selection = i;
}
i++;
}
} else {
List<Languages.Language> languages = Languages.get(ComposeActivity.this);
if (languages != null) {
codesArr = new String[languages.size()];
languagesArr = new String[languages.size()];
int i = 0;
for (Languages.Language language : languages) {
codesArr[i] = language.code;
languagesArr[i] = language.language;
if (currentCode != null && currentCode.equalsIgnoreCase(language.code)) {
selection = i;
}
i++;
}
}
}
SharedPreferences.Editor editor = sharedpreferences.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(ComposeActivity.this, Helper.dialogStyle());
builder.setTitle(getString(R.string.message_language));
builder.setSingleChoiceItems(languagesArr, selection, null);
String[] finalCodesArr = codesArr;
builder.setPositiveButton(R.string.validate, (dialog, which) -> {
int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
editor.putString(getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, finalCodesArr[selectedPosition]);
editor.apply();
dialog.dismiss();
});
builder.setNegativeButton(R.string.reset, (dialog, which) -> {
editor.putString(getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, null);
editor.apply();
dialog.dismiss();
});
builder.create().show();
}
return true;
}
@ -672,6 +615,9 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
if (statusReply.spoiler_text != null) {
statusDraftList.get(0).spoiler_text = statusReply.spoiler_text;
}
if (statusReply.language != null && !statusReply.language.isEmpty()) {
statusDraftList.get(0).language = statusReply.language;
}
//StatusDraftList at this point should only have one element
statusList.addAll(statusDraftList);
composeAdapter = new ComposeAdapter(statusList, statusCount, account, accountMention, visibility, editMessageId);

View File

@ -14,7 +14,7 @@ package app.fedilab.android.client.entities.app;
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
@ -32,9 +32,9 @@ public class Languages implements Serializable {
@SerializedName("languages")
public List<Language> languages;
public static List<Language> get(AppCompatActivity activity) {
public static List<Language> get(Context context) {
try {
InputStream is = activity.getAssets().open("languages/iso_639_1.json");
InputStream is = context.getAssets().open("languages/iso_639_1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);

View File

@ -87,6 +87,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -101,6 +102,7 @@ import app.fedilab.android.client.entities.api.Poll;
import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.client.entities.api.Tag;
import app.fedilab.android.client.entities.app.BaseAccount;
import app.fedilab.android.client.entities.app.Languages;
import app.fedilab.android.client.entities.app.StatusDraft;
import app.fedilab.android.databinding.ComposeAttachmentItemBinding;
import app.fedilab.android.databinding.ComposePollBinding;
@ -745,6 +747,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
@Override
public void afterTextChanged(Editable s) {
int currentLength = MastodonHelper.countLength(holder);
statusList.get(holder.getLayoutPosition()).cursorPosition = holder.binding.content.getSelectionStart();
//Copy/past
int max_car = MastodonHelper.getInstanceMaxChars(context);
if (currentLength > max_car) {
@ -760,7 +763,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
buttonVisibility(holder);
}
//Update cursor position
statusList.get(holder.getBindingAdapterPosition()).cursorPosition = holder.binding.content.getSelectionStart();
//statusList.get(holder.getBindingAdapterPosition()).cursorPosition = holder.binding.content.getSelectionStart();
if (autocomplete) {
holder.binding.content.removeTextChangedListener(this);
Thread thread = new Thread() {
@ -1276,6 +1279,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
//Last compose drawer
buttonVisibility(holder);
holder.binding.buttonEmoji.setOnClickListener(v -> {
try {
displayEmojiPicker(holder);
@ -1361,6 +1365,75 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
notifyItemChanged(position);
manageDrafts.onSubmit(prepareDraft(statusList, this, account.instance, account.user_id));
});
if (statusDraft.language == null || statusDraft.language.isEmpty()) {
String currentCode = sharedpreferences.getString(context.getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, Locale.getDefault().getLanguage());
if (currentCode == null || currentCode.isEmpty()) {
currentCode = "EN";
}
statusDraft.language = currentCode;
}
holder.binding.buttonLanguage.setText(statusDraft.language);
holder.binding.buttonLanguage.setOnClickListener(v -> {
Set<String> storedLanguages = sharedpreferences.getStringSet(context.getString(R.string.SET_SELECTED_LANGUAGE), null);
String[] codesArr = new String[0];
String[] languagesArr = new String[0];
int selection = 0;
if (storedLanguages != null && storedLanguages.size() > 0) {
int i = 0;
codesArr = new String[storedLanguages.size()];
languagesArr = new String[storedLanguages.size()];
for (String language : storedLanguages) {
codesArr[i] = language;
languagesArr[i] = language;
if (statusDraft.language.equalsIgnoreCase(language)) {
selection = i;
}
i++;
}
} else {
List<Languages.Language> languages = Languages.get(context);
if (languages != null) {
codesArr = new String[languages.size()];
languagesArr = new String[languages.size()];
int i = 0;
for (Languages.Language language : languages) {
codesArr[i] = language.code;
languagesArr[i] = language.language;
if (statusDraft.language.equalsIgnoreCase(language.code)) {
selection = i;
}
i++;
}
}
}
SharedPreferences.Editor editor = sharedpreferences.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(context, Helper.dialogStyle());
builder.setTitle(context.getString(R.string.message_language));
builder.setSingleChoiceItems(languagesArr, selection, null);
String[] finalCodesArr = codesArr;
builder.setPositiveButton(R.string.validate, (dialog, which) -> {
int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
editor.putString(context.getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, finalCodesArr[selectedPosition]);
editor.apply();
notifyItemChanged(holder.getLayoutPosition());
dialog.dismiss();
});
builder.setNegativeButton(R.string.reset, (dialog, which) -> {
editor.putString(context.getString(R.string.SET_COMPOSE_LANGUAGE) + account.user_id + account.instance, null);
editor.apply();
notifyItemChanged(holder.getLayoutPosition());
dialog.dismiss();
});
builder.create().show();
});
}
}

View File

@ -11,11 +11,6 @@
android:icon="@drawable/ic_baseline_contact_page_24"
android:title="@string/contact"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_language"
android:icon="@drawable/ic_language"
android:title="@string/languages"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_microphone"
android:icon="@drawable/ic_baseline_mic_24"