[android] Remove om.Option

Signed-off-by: Andrew Shkrob <andrew.shkrob.social@yandex.by>
This commit is contained in:
Andrew Shkrob 2024-09-27 13:21:26 +02:00 committed by Viktor Havaka
parent 5433ca59cc
commit 55be963066
5 changed files with 18 additions and 49 deletions

View file

@ -9,19 +9,19 @@ import androidx.annotation.Nullable;
import app.organicmaps.R;
import app.organicmaps.bookmarks.data.BookmarkManager;
import app.organicmaps.dialog.EditTextDialogFragment;
import app.organicmaps.util.Option;
class CategoryValidator implements EditTextDialogFragment.Validator
{
@Nullable
@Override
public Option<String> validate(@NonNull Activity activity, @Nullable String text)
public String validate(@NonNull Activity activity, @Nullable String text)
{
if (TextUtils.isEmpty(text))
return new Option<>(activity.getString(R.string.bookmarks_error_title_empty_list_name));
return activity.getString(R.string.bookmarks_error_title_empty_list_name);
if (BookmarkManager.INSTANCE.isUsedCategoryName(text))
return new Option<>(activity.getString(R.string.bookmarks_error_title_list_name_already_taken));
return activity.getString(R.string.bookmarks_error_title_list_name_already_taken);
return Option.empty();
return null;
}
}

View file

@ -24,7 +24,6 @@ import com.google.android.material.textfield.TextInputEditText;
import app.organicmaps.R;
import app.organicmaps.base.BaseMwmDialogFragment;
import app.organicmaps.util.InputUtils;
import app.organicmaps.util.Option;
import app.organicmaps.util.StringUtils;
public class EditTextDialogFragment extends BaseMwmDialogFragment
@ -56,7 +55,8 @@ public class EditTextDialogFragment extends BaseMwmDialogFragment
// Interface of dialog input validator.
public interface Validator
{
Option<String> validate(@NonNull Activity activity, @Nullable String text);
@Nullable
String validate(@NonNull Activity activity, @Nullable String text);
}
public static EditTextDialogFragment show(@Nullable String title, @Nullable String initialText,
@ -154,9 +154,9 @@ public class EditTextDialogFragment extends BaseMwmDialogFragment
{
if (mPositiveButton != null && mInputValidator != null)
{
Option<String> maybeError = mInputValidator.validate(activity, input);
mPositiveButton.setEnabled(!maybeError.hasValue());
mEtInputLayout.getEditText().setError(maybeError.getOrElse(null));
final String maybeError = mInputValidator.validate(activity, input);
mPositiveButton.setEnabled(maybeError == null);
mEtInputLayout.getEditText().setError(maybeError);
}
}

View file

@ -35,7 +35,6 @@ import app.organicmaps.editor.data.TimeFormatUtils;
import app.organicmaps.editor.data.Timetable;
import app.organicmaps.util.Graphics;
import app.organicmaps.util.InputUtils;
import app.organicmaps.util.Option;
import app.organicmaps.util.StringUtils;
import app.organicmaps.util.UiUtils;
import app.organicmaps.util.Utils;
@ -687,9 +686,9 @@ public class EditorFragment extends BaseMwmFragment implements View.OnClickListe
{
return (activity, text) -> {
if (TextUtils.isEmpty(text))
return new Option<>(activity.getString(R.string.delete_place_empty_comment_error));
return activity.getString(R.string.delete_place_empty_comment_error);
else
return Option.empty();
return null;
};
}
}

View file

@ -14,7 +14,8 @@ import app.organicmaps.R;
import app.organicmaps.base.BaseMwmRecyclerFragment;
import app.organicmaps.dialog.EditTextDialogFragment;
import app.organicmaps.editor.data.LocalizedStreet;
import app.organicmaps.util.Option;
import java.util.Optional;
public class StreetFragment extends BaseMwmRecyclerFragment<StreetAdapter>
{
@ -35,7 +36,7 @@ public class StreetFragment extends BaseMwmRecyclerFragment<StreetAdapter>
}
@Override
public void onSaveInstanceState(Bundle outState)
public void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
Editor.nativeSetStreet(getStreet());
@ -65,13 +66,14 @@ public class StreetFragment extends BaseMwmRecyclerFragment<StreetAdapter>
return text -> saveStreet(new LocalizedStreet(text, ""));
}
@NonNull
public static EditTextDialogFragment.Validator getStreetValidator()
{
return (activity, text) -> {
if (TextUtils.isEmpty(text))
return new Option<>(activity.getString(R.string.empty_street_name_error));
return activity.getString(R.string.empty_street_name_error);
else
return Option.empty();
return null;
};
}
}

View file

@ -1,32 +0,0 @@
package app.organicmaps.util;
public class Option<T>
{
private final T mValue;
public static<U> Option<U> empty()
{
return new Option<>(null);
}
public Option(T value)
{
mValue = value;
}
public boolean hasValue()
{
return mValue != null;
}
public T get()
{
assert(hasValue());
return mValue;
}
public T getOrElse(T defaultValue)
{
return hasValue() ? mValue : defaultValue;
}
}