[android] Use error in textfield instead alert dialog

Signed-off-by: Jean-Baptiste <jeanbaptiste.charron@outlook.fr>
This commit is contained in:
Jean-Baptiste
2026-01-01 07:37:37 +01:00
parent 008a785564
commit 8b7679d7d8
2 changed files with 41 additions and 42 deletions

View File

@@ -16,8 +16,7 @@ import app.organicmaps.sdk.bookmarks.data.BookmarkCategory;
import app.organicmaps.sdk.bookmarks.data.BookmarkManager;
import app.organicmaps.util.InputUtils;
import app.organicmaps.util.Utils;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.Objects;
@@ -34,11 +33,15 @@ public class BookmarkCategorySettingsFragment extends BaseMwmToolbarFragment
@NonNull
private TextInputEditText mEditDescView;
@SuppressWarnings("NullableProblems")
@NonNull
private TextInputLayout mEditCategoryNameLayout;
@SuppressWarnings("NullableProblems")
@NonNull
private TextInputEditText mEditCategoryNameView;
@NonNull
private ShapeableImageView mSaveView;
private MaterialButton mSaveView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
@@ -61,8 +64,8 @@ public class BookmarkCategorySettingsFragment extends BaseMwmToolbarFragment
private void initViews(@NonNull View root)
{
mEditCategoryNameView = root.findViewById(R.id.edit_list_name_view);
TextInputLayout clearNameBtn = root.findViewById(R.id.edit_list_name_input);
clearNameBtn.setEndIconOnClickListener(v -> clearAndFocus(mEditCategoryNameView));
mEditCategoryNameLayout = root.findViewById(R.id.edit_list_name_input);
mEditCategoryNameLayout.setEndIconOnClickListener(v -> clearAndFocus(mEditCategoryNameView));
mEditCategoryNameView.setText(mCategory.getName());
InputFilter[] f = {new InputFilter.LengthFilter(TEXT_LENGTH_LIMIT)};
mEditCategoryNameView.setFilters(f);
@@ -75,7 +78,15 @@ public class BookmarkCategorySettingsFragment extends BaseMwmToolbarFragment
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
clearNameBtn.setEndIconVisible(charSequence.length() > 0);
if (charSequence.length() > 0)
{
mEditCategoryNameLayout.setEndIconVisible(true);
}
else
{
mEditCategoryNameLayout.setEndIconVisible(false);
mEditCategoryNameLayout.setError(getString(R.string.bookmarks_error_title_empty_list_name));
}
}
@Override
@@ -91,16 +102,23 @@ public class BookmarkCategorySettingsFragment extends BaseMwmToolbarFragment
private void onEditDoneClicked()
{
final String newCategoryName = getEditableCategoryName();
if (!validateCategoryName(newCategoryName))
return;
if (BookmarkManager.INSTANCE.isUsedCategoryName(newCategoryName) && !TextUtils.equals(newCategoryName, mCategory.getName())){
mEditCategoryNameLayout.setError(getString(R.string.bookmarks_error_title_list_name_already_taken));
}
else if (mEditCategoryNameView.length() < 1)
{
mEditCategoryNameLayout.setError(getString(R.string.bookmarks_error_title_empty_list_name));
}
else
{
if (isCategoryNameChanged())
BookmarkManager.INSTANCE.setCategoryName(mCategory.getId(), newCategoryName);
if (isCategoryNameChanged())
BookmarkManager.INSTANCE.setCategoryName(mCategory.getId(), newCategoryName);
if (isCategoryDescChanged())
BookmarkManager.INSTANCE.setCategoryDescription(mCategory.getId(), getEditableCategoryDesc());
if (isCategoryDescChanged())
BookmarkManager.INSTANCE.setCategoryDescription(mCategory.getId(), getEditableCategoryDesc());
requireActivity().finish();
requireActivity().finish();
}
}
private boolean isCategoryNameChanged()
@@ -109,30 +127,6 @@ public class BookmarkCategorySettingsFragment extends BaseMwmToolbarFragment
return !TextUtils.equals(categoryName, mCategory.getName());
}
private boolean validateCategoryName(@Nullable String name)
{
if (TextUtils.isEmpty(name))
{
new MaterialAlertDialogBuilder(requireActivity(), R.style.MwmTheme_AlertDialog)
.setTitle(R.string.bookmarks_error_title_empty_list_name)
.setMessage(R.string.bookmarks_error_message_empty_list_name)
.setPositiveButton(R.string.ok, null)
.show();
return false;
}
if (BookmarkManager.INSTANCE.isUsedCategoryName(name) && !TextUtils.equals(name, mCategory.getName()))
{
new MaterialAlertDialogBuilder(requireActivity(), R.style.MwmTheme_AlertDialog)
.setTitle(R.string.bookmarks_error_title_list_name_already_taken)
.setMessage(R.string.bookmarks_error_message_list_name_already_taken)
.setPositiveButton(R.string.ok, null)
.show();
return false;
}
return true;
}
@NonNull
private String getEditableCategoryName()
{

View File

@@ -12,13 +12,18 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/MwmWidget.ToolbarTheme">
<com.google.android.material.imageview.ShapeableImageView
<com.google.android.material.button.MaterialButton
android:id="@+id/save"
android:layout_width="?actionBarSize"
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="?actionBarSize"
android:layout_height="?actionBarSize"
android:scaleType="centerInside"
android:layout_gravity="end|center_vertical"
app:srcCompat="@drawable/ic_done"/>
app:icon="@drawable/ic_done"
app:iconGravity="textStart"
app:iconTint="?iconTint"
android:enabled="false"
app:iconPadding="0dp"
app:iconSize="24dp" />
</com.google.android.material.appbar.MaterialToolbar>
<androidx.core.widget.NestedScrollView
android:scrollbars="none"