show errors on empty text fields

This commit is contained in:
veliymolfar 2020-06-12 14:50:16 +03:00
parent cdaa450de5
commit a0c58cb624

View file

@ -82,6 +82,8 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
private static final String SQLITE_DB_KEY = "sqlite_db_key"; private static final String SQLITE_DB_KEY = "sqlite_db_key";
private static final String FROM_TEMPLATE_KEY = "from_template_key"; private static final String FROM_TEMPLATE_KEY = "from_template_key";
private OsmandApplication app; private OsmandApplication app;
private TextInputLayout nameInputLayout;
private TextInputLayout urlInputLayout;
private TextInputEditText nameEditText; private TextInputEditText nameEditText;
private TextInputEditText urlEditText; private TextInputEditText urlEditText;
private LinearLayout contentContainer; private LinearLayout contentContainer;
@ -168,10 +170,10 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
? ContextCompat.getColor(app, R.color.icon_color_osmand_dark) ? ContextCompat.getColor(app, R.color.icon_color_osmand_dark)
: ContextCompat.getColor(app, R.color.icon_color_osmand_light); : ContextCompat.getColor(app, R.color.icon_color_osmand_light);
int btnBgColorRes = nightMode ? R.color.list_background_color_dark : R.color.list_background_color_light; int btnBgColorRes = nightMode ? R.color.list_background_color_dark : R.color.list_background_color_light;
TextInputLayout nameInputLayout = root.findViewById(R.id.name_input_layout); nameInputLayout = root.findViewById(R.id.name_input_layout);
nameInputLayout.setBoxStrokeColor(boxStrokeColor); nameInputLayout.setBoxStrokeColor(boxStrokeColor);
nameEditText = root.findViewById(R.id.name_edit_text); nameEditText = root.findViewById(R.id.name_edit_text);
TextInputLayout urlInputLayout = root.findViewById(R.id.url_input_layout); urlInputLayout = root.findViewById(R.id.url_input_layout);
urlInputLayout.setBoxStrokeColor(boxStrokeColor); urlInputLayout.setBoxStrokeColor(boxStrokeColor);
urlEditText = root.findViewById(R.id.url_edit_text); urlEditText = root.findViewById(R.id.url_edit_text);
contentContainer = root.findViewById(R.id.content_container); contentContainer = root.findViewById(R.id.content_container);
@ -323,37 +325,19 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
} }
} }
private TextWatcher getTextWatcher() { private void setSaveBtnEnabled() {
return new TextWatcher() { boolean enabled = !nameEditText.getText().toString().isEmpty()
@Override && !urlEditText.getText().toString().isEmpty();
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { saveBtn.setEnabled(enabled);
saveBtnTitle.setEnabled(enabled);
} }
@Override private void checkWasChanged() {
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (!Algorithms.objectEquals(editedLayerName, nameEditText.getText().toString())
|| !Algorithms.objectEquals(urlToLoad, urlEditText.getText().toString())) {
}
@Override
public void afterTextChanged(Editable editable) {
if (nameEditText.getText() != null && urlEditText.getText() != null) {
String name = nameEditText.getText().toString().trim();
String url = urlEditText.getText().toString().trim();
if (Algorithms.isEmpty(name) || Algorithms.isEmpty(url)) {
saveBtn.setEnabled(false);
saveBtnTitle.setEnabled(false);
} else {
saveBtn.setEnabled(true);
saveBtnTitle.setEnabled(true);
}
if (Algorithms.objectEquals(editedLayerName, name) || Algorithms.objectEquals(urlToLoad, url)) {
wasChanged = true; wasChanged = true;
} }
} }
}
};
}
private void saveTemplate() { private void saveTemplate() {
try { try {
@ -414,8 +398,9 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
private void updateUi() { private void updateUi() {
nameEditText.setText(editedLayerName); nameEditText.setText(editedLayerName);
urlEditText.setText(urlToLoad); urlEditText.setText(urlToLoad);
nameEditText.addTextChangedListener(getTextWatcher()); nameEditText.addTextChangedListener(new MapSourceTextWatcher(nameInputLayout));
urlEditText.addTextChangedListener(getTextWatcher()); urlEditText.addTextChangedListener(new MapSourceTextWatcher(urlInputLayout));
setSaveBtnEnabled();
addConfigurationItems(ConfigurationItem.values()); addConfigurationItems(ConfigurationItem.values());
} }
@ -528,6 +513,35 @@ public class EditMapSourceDialogFragment extends BaseOsmAndDialogFragment
this.template = template; this.template = template;
} }
class MapSourceTextWatcher implements TextWatcher {
private TextInputLayout relatedInputLayout;
public MapSourceTextWatcher(TextInputLayout textInputLayout) {
this.relatedInputLayout = textInputLayout;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.toString().isEmpty()) {
relatedInputLayout.setError(relatedInputLayout.getHelperText());
} else {
relatedInputLayout.setError(null);
}
setSaveBtnEnabled();
checkWasChanged();
}
}
public interface OnMapSourceUpdateListener { public interface OnMapSourceUpdateListener {
void onMapSourceUpdated(); void onMapSourceUpdated();
} }