mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 04:53:36 +00:00
[android] Improve custom map server dialog UX
Signed-off-by: NoelClick <dev@noel.click>
This commit is contained in:
@@ -3,20 +3,26 @@ package app.organicmaps.settings;
|
||||
import static app.organicmaps.leftbutton.LeftButtonsHolder.DISABLE_BUTTON_CODE;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.EditTextPreference;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.TwoStatePreference;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
|
||||
import app.organicmaps.MwmApplication;
|
||||
import app.organicmaps.R;
|
||||
@@ -544,26 +550,84 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment implements La
|
||||
|
||||
private void initCustomMapDownloadUrlPrefsCallbacks()
|
||||
{
|
||||
EditTextPreference customUrlPref = getPreference(getString(R.string.pref_custom_map_download_url));
|
||||
customUrlPref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
String url = newValue != null ? ((String) newValue).trim() : "";
|
||||
Preference customUrlPref = getPreference(getString(R.string.pref_custom_map_download_url));
|
||||
|
||||
if (!url.isEmpty()
|
||||
&& !url.startsWith("http://")
|
||||
&& !url.startsWith("https://")) {
|
||||
Toast.makeText(requireContext(),
|
||||
R.string.download_resources_custom_url_error_scheme,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
|
||||
|
||||
Framework.applyCustomMapDownloadUrl(requireContext(), url);
|
||||
return true; // save the value
|
||||
String current = prefs.getString(getString(R.string.pref_custom_map_download_url), "");
|
||||
String normalized = current.trim();
|
||||
if (!normalized.isEmpty() && !normalized.endsWith("/"))
|
||||
normalized = normalized + "/";
|
||||
|
||||
// Initial summary
|
||||
customUrlPref.setSummary(normalized.isEmpty()
|
||||
? getString(R.string.download_resources_custom_url_summary_none)
|
||||
: normalized);
|
||||
|
||||
// Sync native
|
||||
Framework.applyCustomMapDownloadUrl(requireContext(), normalized);
|
||||
|
||||
// Show dialog
|
||||
customUrlPref.setOnPreferenceClickListener(preference -> {
|
||||
openCustomServerDialog(customUrlPref);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private void openCustomServerDialog(Preference pref)
|
||||
{
|
||||
View dialogView = LayoutInflater.from(requireContext())
|
||||
.inflate(R.layout.dialog_custom_map_server, null);
|
||||
TextInputLayout til = dialogView.findViewById(R.id.til_custom_map_server);
|
||||
TextInputEditText edit = dialogView.findViewById(R.id.edit_custom_map_server);
|
||||
|
||||
SharedPreferences prefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext());
|
||||
String current = prefs.getString(getString(R.string.pref_custom_map_download_url), "");
|
||||
edit.setText(current);
|
||||
|
||||
MaterialAlertDialogBuilder builder =
|
||||
new MaterialAlertDialogBuilder(requireContext(), R.style.MwmTheme_AlertDialog)
|
||||
.setTitle(R.string.download_resources_custom_url_title)
|
||||
.setMessage(R.string.download_resources_custom_url_message)
|
||||
.setView(dialogView)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setPositiveButton(R.string.save, null);
|
||||
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.setOnShowListener(dlg -> {
|
||||
Button ok = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||
ok.setOnClickListener(v -> {
|
||||
String url = edit.getText() != null ? edit.getText().toString().trim() : "";
|
||||
|
||||
if (!url.isEmpty()
|
||||
&& !url.startsWith("http://")
|
||||
&& !url.startsWith("https://")) {
|
||||
til.setError(getString(R.string.download_resources_custom_url_error_scheme));
|
||||
return;
|
||||
}
|
||||
|
||||
til.setError(null);
|
||||
|
||||
if (!url.isEmpty() && !url.endsWith("/"))
|
||||
url = url + "/";
|
||||
|
||||
prefs.edit()
|
||||
.putString(getString(R.string.pref_custom_map_download_url), url)
|
||||
.apply();
|
||||
|
||||
// Apply native
|
||||
Framework.applyCustomMapDownloadUrl(requireContext(), url);
|
||||
|
||||
pref.setSummary(url.isEmpty()
|
||||
? getString(R.string.download_resources_custom_url_summary_none)
|
||||
: url);
|
||||
|
||||
dialog.dismiss();
|
||||
});
|
||||
});
|
||||
|
||||
// Ensure native side is updated when the screen opens
|
||||
String current = customUrlPref.getText();
|
||||
Framework.applyCustomMapDownloadUrl(requireContext(), current != null ? current.trim() : "");
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void removePreference(@NonNull String categoryKey, @NonNull Preference preference)
|
||||
|
||||
@@ -11,6 +11,18 @@
|
||||
android:layout_height="match_parent"
|
||||
android:layout_above="@+id/button_container"
|
||||
android:layout_gravity="center">
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btn_advanced"
|
||||
style="@style/Widget.Material3.Button.IconButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:icon="@drawable/ic_settings"
|
||||
app:iconTint="?colorOnSurface"
|
||||
android:contentDescription="@string/download_resources_custom_url_title"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_margin="@dimen/margin_half" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
||||
@@ -6,15 +6,6 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btn_advanced"
|
||||
style="@style/MwmWidget.M3.Button.Secondary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/margin_base_plus"
|
||||
android:layout_marginEnd="@dimen/margin_base_plus"
|
||||
android:text="@string/download_resources_advanced"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body2"/>
|
||||
<com.google.android.material.checkbox.MaterialCheckBox
|
||||
android:id="@+id/chb_download_country"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
android:id="@+id/til_custom_map_server"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="@dimen/margin_base"
|
||||
android:paddingEnd="@dimen/margin_base"
|
||||
android:paddingTop="@dimen/margin_base"
|
||||
android:paddingBottom="@dimen/margin_half"
|
||||
android:hint="@string/download_resources_custom_url_title"
|
||||
app:placeholderText="@string/download_resources_custom_url_hint">
|
||||
|
||||
@@ -12,8 +16,6 @@
|
||||
android:id="@+id/edit_custom_map_server"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/margin_base"
|
||||
android:layout_marginEnd="@dimen/margin_base"
|
||||
android:inputType="textUri"
|
||||
android:singleLine="true" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
@@ -974,5 +974,6 @@
|
||||
<string name="download_resources_custom_url_title">Custom Map Server</string>
|
||||
<string name="download_resources_custom_url_message">Override the default map download server used for map downloads. Leave empty to use CoMaps default server.</string>
|
||||
<string name="download_resources_custom_url_hint">https://maps.comaps.app/</string>
|
||||
<string name="download_resources_custom_url_summary_none">Not set</string>
|
||||
<string name="download_resources_custom_url_error_scheme">Please enter a full URL starting with http:// or https:// and ending with /.</string>
|
||||
</resources>
|
||||
|
||||
@@ -212,14 +212,11 @@
|
||||
android:defaultValue="true"
|
||||
android:widgetLayout="@layout/preference_switch"
|
||||
android:order="2"/>
|
||||
<EditTextPreference
|
||||
android:key="@string/pref_custom_map_download_url"
|
||||
android:title="@string/pref_custom_map_download_url_title"
|
||||
android:summary="@string/pref_custom_map_download_url_summary"
|
||||
android:inputType="textUri"
|
||||
android:singleLine="true"
|
||||
app:placeholderText="@string/pref_custom_map_download_url_hint"
|
||||
android:order="3"/>
|
||||
<Preference
|
||||
android:key="@string/pref_custom_map_download_url"
|
||||
android:title="@string/pref_custom_map_download_url_title"
|
||||
android:summary="@string/pref_custom_map_download_url_summary"
|
||||
android:order="3" />
|
||||
|
||||
</androidx.preference.PreferenceCategory>
|
||||
</androidx.preference.PreferenceScreen>
|
||||
|
||||
Reference in New Issue
Block a user