From 9f7366f1a427db97bbee8e86a9b07ef89a6af6e0 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 20 Jul 2019 22:17:47 +0300 Subject: [PATCH] streamline profile validation avoids insistent warnings about invalid URL --- .../ui/profiles/ProfileDetailFragment.java | 79 +++++++++++-------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 2e964d1c..f300da5d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -246,22 +246,6 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue rootView.findViewById(R.id.preferred_accounts_accounts_filter_layout); insecureWarningText = rootView.findViewById(R.id.insecure_scheme_text); - url.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - - } - @Override - public void afterTextChanged(Editable s) { - checkValidity(); - checkInsecureSchemeWithAuth(); - } - }); - useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> { debug("profiles", isChecked ? "auth enabled " : "auth disabled"); authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE); @@ -302,6 +286,23 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue profileThemeId = -1; } + checkInsecureSchemeWithAuth(); + + url.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + @Override + public void afterTextChanged(Editable s) { + checkInsecureSchemeWithAuth(); + } + }); + final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId; final int profileColor = Colors.getPrimaryColorForHue(hue); @@ -316,6 +317,31 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue }); return rootView; } + private boolean checkUrlValidity() { + boolean valid = true; + + String val = String.valueOf(url.getText()).trim(); + if (val.isEmpty()) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_profile_url_empty)); + } + try { + URL url = new URL(val); + String host = url.getHost(); + if (host == null || host.isEmpty()) throw new MalformedURLException("Missing host"); + String protocol = url.getProtocol().toUpperCase(); + if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_invalid_url)); + } + } + catch (MalformedURLException e) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_invalid_url)); + } + + return valid; + } private void checkInsecureSchemeWithAuth() { boolean showWarning = false; @@ -353,25 +379,8 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty)); } - val = String.valueOf(url.getText()).trim(); - if (val.isEmpty()) { - valid = false; - urlLayout.setError(getResources().getText(R.string.err_profile_url_empty)); - } - try { - URL url = new URL(val); - String host = url.getHost(); - if (host == null || host.isEmpty()) throw new MalformedURLException("Missing host"); - String protocol = url.getProtocol().toUpperCase(); - if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) { - valid = false; - urlLayout.setError(getResources().getText(R.string.err_invalid_url)); - } - } - catch (MalformedURLException e) { - valid = false; - urlLayout.setError(getResources().getText(R.string.err_invalid_url)); - } + if (!checkUrlValidity()) valid = false; + if (useAuthentication.isChecked()) { val = String.valueOf(userName.getText()); if (val.trim().isEmpty()) { -- 2.39.5