From e8e510daafa952fedeb7dc179a9d71d71064627b Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Sat, 3 Jun 2023 14:05:15 +0200 Subject: [PATCH] added language selector for status editor, layout fix --- .../twidda/backend/api/mastodon/Mastodon.java | 2 + .../backend/helper/update/StatusUpdate.java | 21 ++++++++ .../twidda/ui/activities/LoginActivity.java | 2 +- .../ui/activities/SettingsActivity.java | 10 ++-- .../twidda/ui/adapter/DropdownAdapter.java | 49 ++++++++++++++----- .../twidda/ui/dialogs/PollDialog.java | 2 +- .../ui/dialogs/StatusPreferenceDialog.java | 31 ++++++++++-- .../twidda/ui/dialogs/WebPushDialog.java | 2 +- app/src/main/res/layout/dialog_status.xml | 45 ++++++++++++++--- app/src/main/res/values-de-rDE/strings.xml | 7 +++ app/src/main/res/values/dimens.xml | 3 +- app/src/main/res/values/strings.xml | 8 ++- 12 files changed, 147 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java index b1edc3bc..694800cb 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java @@ -650,6 +650,8 @@ public class Mastodon implements Connection { params.add("spoiler_text=true"); if (update.getText() != null) params.add("status=" + StringUtils.encode(update.getText())); + if (update.getLanguageCode() != null) + params.add("language=" + update.getLanguageCode()); if (update.getReplyId() != 0) params.add("in_reply_to_id=" + update.getReplyId()); if (update.getVisibility() == Status.VISIBLE_DIRECT) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/StatusUpdate.java b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/StatusUpdate.java index f433c839..35a06142 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/StatusUpdate.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/StatusUpdate.java @@ -78,6 +78,7 @@ public class StatusUpdate implements Serializable, Closeable { private boolean spoiler = false; private int visibility = Status.VISIBLE_PUBLIC; private String text; + private String languageCode; // attachment attributes @Nullable @@ -119,6 +120,7 @@ public class StatusUpdate implements Serializable, Closeable { sensitive = status.isSensitive(); spoiler = status.isSpoiler(); visibility = status.getVisibility(); + languageCode = status.getLanguage(); if (status.getPoll() != null) { poll = new PollUpdate(status.getPoll()); } @@ -153,6 +155,15 @@ public class StatusUpdate implements Serializable, Closeable { this.text = text; } + /** + * add status language + * + * @param languageCode ISO 639 language code + */ + public void addLanguage(String languageCode) { + this.languageCode = languageCode; + } + /** * Add file uri and check if file is valid * @@ -327,6 +338,16 @@ public class StatusUpdate implements Serializable, Closeable { return text; } + /** + * get status language + * + * @return ISO 639 language code + */ + @Nullable + public String getLanguageCode() { + return languageCode; + } + /** * get type of attachment * currently there is only one type of media used at once diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/activities/LoginActivity.java b/app/src/main/java/org/nuclearfog/twidda/ui/activities/LoginActivity.java index 3f1c0302..25f85ae8 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/activities/LoginActivity.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/activities/LoginActivity.java @@ -128,7 +128,7 @@ public class LoginActivity extends AppCompatActivity implements ActivityResultCa connectionDialog = new ConnectionDialog(this); loginAsync = new LoginAction(this); - adapter.addItems(R.array.networks); + adapter.setItems(R.array.networks); hostSelector.setAdapter(adapter); if (savedInstanceState != null) { Serializable data = savedInstanceState.getSerializable(KEY_SAVE); diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java b/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java index aa93c6dd..7083f1e8 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java @@ -191,10 +191,10 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen AppStyles.setTheme(root); AppStyles.setOverflowIcon(toolbar, settings.getIconColor()); - fontAdapter.addFonts(GlobalSettings.FONT_TYPES); - fontAdapter.addItems(GlobalSettings.FONT_NAMES); - scaleAdapter.addItems(R.array.scales); - locationAdapter.addItem(settings.getTrendLocation().getFullName()); + fontAdapter.setFonts(GlobalSettings.FONT_TYPES); + fontAdapter.setItems(GlobalSettings.FONT_NAMES); + scaleAdapter.setItems(R.array.scales); + locationAdapter.setItem(settings.getTrendLocation().getFullName()); location_dropdown.setAdapter(locationAdapter); fontSelector.setAdapter(fontAdapter); @@ -664,7 +664,7 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen position = i; } } - locationAdapter.addItems(items); + locationAdapter.setItems(items); // set item of a previously selection if exists if (position >= 0) { location_dropdown.setSelection(position, false); diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/adapter/DropdownAdapter.java b/app/src/main/java/org/nuclearfog/twidda/ui/adapter/DropdownAdapter.java index dc0f0678..8b8370bf 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/adapter/DropdownAdapter.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/adapter/DropdownAdapter.java @@ -16,7 +16,11 @@ import org.nuclearfog.twidda.config.GlobalSettings; import java.util.Arrays; - +/** + * Dropdown list adapter used for spinners + * + * @author nuclearfog + */ public class DropdownAdapter extends BaseAdapter { private GlobalSettings settings; @@ -25,7 +29,9 @@ public class DropdownAdapter extends BaseAdapter { private String[] items = {}; private Typeface[] fonts = {}; - + /** + * + */ public DropdownAdapter(Context context) { settings = GlobalSettings.getInstance(context); this.context = context; @@ -71,31 +77,52 @@ public class DropdownAdapter extends BaseAdapter { return convertView; } - - public void addItem(String item) { + /** + * set single item + * + * @param item item string + */ + public void setItem(String item) { this.items = new String[] {item}; notifyDataSetChanged(); } - - public void addItems(@ArrayRes int arrayRes) { + /** + * set items from string array resource + * + * @param arrayRes array resource containing strings + */ + public void setItems(@ArrayRes int arrayRes) { TypedArray tArray = context.getResources().obtainTypedArray(arrayRes); items = new String[tArray.length()]; for (int i = 0; i < tArray.length(); i++) { - items[i] = tArray.getString(i); + String item = tArray.getString(i); + if (item != null) { + items[i] = item; + } else { + items[i] = ""; + } } tArray.recycle(); notifyDataSetChanged(); } - - public void addItems(String[] items) { + /** + * set items from string array + * + * @param items string array containing items + */ + public void setItems(String[] items) { this.items = Arrays.copyOf(items, items.length); notifyDataSetChanged(); } - - public void addFonts(Typeface[] fonts) { + /** + * set font for items + * + * @param fonts font array + */ + public void setFonts(Typeface[] fonts) { this.fonts = Arrays.copyOf(fonts, fonts.length); } } \ No newline at end of file diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/PollDialog.java b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/PollDialog.java index e50a4313..8d5c612c 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/PollDialog.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/PollDialog.java @@ -59,7 +59,7 @@ public class PollDialog extends Dialog implements OnClickListener { hide_votes = findViewById(R.id.dialog_poll_hide_total); DropdownAdapter adapter = new DropdownAdapter(context); - adapter.addItems(R.array.timeunits); + adapter.setItems(R.array.timeunits); timeUnitSelector.setAdapter(adapter); timeUnitSelector.setSelection(2); diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/StatusPreferenceDialog.java b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/StatusPreferenceDialog.java index 8ad9c18b..362a9b6d 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/StatusPreferenceDialog.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/StatusPreferenceDialog.java @@ -19,6 +19,8 @@ import org.nuclearfog.twidda.config.GlobalSettings; import org.nuclearfog.twidda.model.Status; import org.nuclearfog.twidda.ui.adapter.DropdownAdapter; +import java.util.Locale; + /** * status editor preference dialog used to set additional status information * @@ -30,6 +32,8 @@ public class StatusPreferenceDialog extends Dialog implements OnCheckedChangeLis private StatusUpdate statusUpdate; + private Locale[] languages; + /** * @param statusUpdate status information from status editor */ @@ -42,23 +46,37 @@ public class StatusPreferenceDialog extends Dialog implements OnCheckedChangeLis SwitchButton spoilerCheck = findViewById(R.id.dialog_status_spoiler); View statusVisibility = findViewById(R.id.dialog_status_visibility_container); View statusSpoiler = findViewById(R.id.dialog_status_spoiler_container); + Spinner languageSelector = findViewById(R.id.dialog_status_language); visibilitySelector = findViewById(R.id.dialog_status_visibility); GlobalSettings settings = GlobalSettings.getInstance(context); + AppStyles.setTheme(rootView); - DropdownAdapter adapter = new DropdownAdapter(context); - visibilitySelector.setAdapter(adapter); + DropdownAdapter visibility_adapter = new DropdownAdapter(context); + DropdownAdapter language_adapter = new DropdownAdapter(context); + languageSelector.setAdapter(language_adapter); + languageSelector.setSelected(false); + visibilitySelector.setAdapter(visibility_adapter); visibilitySelector.setSelection(0, false); visibilitySelector.setSelected(false); - AppStyles.setTheme(rootView); + + languages = Locale.getAvailableLocales(); + String[] language_names = new String[languages.length + 1]; + language_names[0] = context.getString(R.string.dialog_status_language_empty); + for (int i = 0 ; i < languages.length ; i++) { + language_names[i + 1] = languages[i].getDisplayLanguage() + " " + languages[i].getCountry(); + } + language_adapter.setItems(language_names); + if (!settings.getLogin().getConfiguration().statusVisibilitySupported()) { statusVisibility.setVisibility(View.GONE); } if (!settings.getLogin().getConfiguration().statusSpoilerSupported()) { statusSpoiler.setVisibility(View.GONE); } - adapter.addItems(R.array.visibility); + visibility_adapter.setItems(R.array.visibility); sensitiveCheck.setOnCheckedChangeListener(this); spoilerCheck.setOnCheckedChangeListener(this); + languageSelector.setOnItemSelectedListener(this); visibilitySelector.setOnItemSelectedListener(this); } @@ -118,6 +136,11 @@ public class StatusPreferenceDialog extends Dialog implements OnCheckedChangeLis statusUpdate.setVisibility(Status.VISIBLE_UNLISTED); break; } + } else if (parent.getId() == R.id.dialog_status_language) { + if (position > 0) { + Locale language = languages[position - 1]; + statusUpdate.addLanguage(language.getLanguage()); + } } } diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/WebPushDialog.java b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/WebPushDialog.java index 87160196..46c0d717 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/WebPushDialog.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/WebPushDialog.java @@ -82,7 +82,7 @@ public class WebPushDialog extends Dialog implements OnCheckedChangeListener, On policySelector.setSelection(2); break; } - adapter.addItems(R.array.push_policy); + adapter.setItems(R.array.push_policy); AppStyles.setTheme(root); mention.setOnCheckedChangeListener(this); diff --git a/app/src/main/res/layout/dialog_status.xml b/app/src/main/res/layout/dialog_status.xml index a48bbf5b..d3c602a7 100644 --- a/app/src/main/res/layout/dialog_status.xml +++ b/app/src/main/res/layout/dialog_status.xml @@ -10,7 +10,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/dialog_status_title" - android:textSize="@dimen/dialog_status_title_textsize" + android:textSize="@dimen/dialog_status_textsize_title" android:layout_marginBottom="@dimen/dialog_status_layout_margins" android:lines="1" /> @@ -18,7 +18,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" - android:layout_marginBottom="@dimen/dialog_status_layout_margins"> + android:layout_marginBottom="@dimen/dialog_status_layout_margins" + android:gravity="center_vertical"> + android:lines="1" + android:textSize="@dimen/dialog_status_textsize_small" /> + + android:layout_marginBottom="@dimen/dialog_status_layout_margins" + android:gravity="center_vertical"> @@ -57,7 +62,9 @@ android:id="@+id/dialog_status_visibility_container" android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="horizontal"> + android:orientation="horizontal" + android:layout_marginBottom="@dimen/dialog_status_layout_margins" + android:gravity="center_vertical"> + android:layout_weight="5" /> + + + + + + + + diff --git a/app/src/main/res/values-de-rDE/strings.xml b/app/src/main/res/values-de-rDE/strings.xml index 3679883e..8ba07805 100644 --- a/app/src/main/res/values-de-rDE/strings.xml +++ b/app/src/main/res/values-de-rDE/strings.xml @@ -29,6 +29,7 @@ Datenbank gelöscht Nachricht verworfen Domain gesperrt! + Push-Konfiguration wurde aktualisiert Fehler! bearbeiten löschen @@ -318,6 +319,12 @@ %1$s hat einen Status favorisiert %1$s hat einen Status geteilt %1$s folgt dir jetzt + %1$s hat was gepostet + Ein Status von dir wurde geteilt. Eine Umfrage wurde beendet + %1$s folgt dir jetzt %1$s hat dich erwähnt + Neue Benachrichtigungen + Sprache + keine Auswahl \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index a16519ba..bdfefacd 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -296,7 +296,8 @@ 5dp - 20sp + 20sp + 11sp 10dp 5dp 22sp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3c3398d7..a9383cdb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -69,7 +69,7 @@ domain removed from the list hashtag followed domain blocked! - Push configuration updated! + Push configuration updated Error @@ -177,10 +177,6 @@ svg icons from: www.svgrepo.com www.entypo.com - - Twitter - Mastodon - Profile image enter PIN add keys @@ -332,6 +328,8 @@ mark content as sensitive mark content as spoiler Visibility + Language + nothing selected on repost on favorite on mention