]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
profile editor: validate entered URL
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.java
index c0944f9a9261bd0aa1355a36fd2b7c9ba570df12..c6dbc6a587c1799244b6656de63d95c4bba9cf5f 100644 (file)
@@ -46,6 +46,8 @@ import net.ktnx.mobileledger.utils.Colors;
 
 import org.jetbrains.annotations.NotNull;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Objects;
 
@@ -146,6 +148,10 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
         if (profiles == null) throw new AssertionError();
         profiles.set(index, newProfile);
+
+        ProfilesRecyclerViewAdapter prva = ProfilesRecyclerViewAdapter.getInstance();
+        if (prva != null) prva.notifyItemChanged(index);
+
         if (mProfile.equals(Data.profile.getValue())) Data.profile.setValue(newProfile);
     }
     @Override
@@ -317,11 +323,20 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
         }
 
-        val = String.valueOf(url.getText());
-        if (val.trim().isEmpty()) {
+        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");
+        }
+        catch (MalformedURLException e) {
+            valid = false;
+            urlLayout.setError(getResources().getText(R.string.err_invalid_url));
+        }
         if (useAuthentication.isChecked()) {
             val = String.valueOf(userName.getText());
             if (val.trim().isEmpty()) {