diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 2aa44f30..b9366499 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/app/src/main/java/com/readrops/app/AddFeedDialog.java b/app/src/main/java/com/readrops/app/AddFeedDialog.java index b1bebd54..f51c1750 100644 --- a/app/src/main/java/com/readrops/app/AddFeedDialog.java +++ b/app/src/main/java/com/readrops/app/AddFeedDialog.java @@ -11,6 +11,7 @@ import android.support.design.widget.TextInputEditText; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Patterns; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; @@ -18,6 +19,8 @@ import android.widget.ProgressBar; import com.readrops.readropslibrary.HtmlParser; import com.readrops.readropslibrary.ParsingResult; +import java.io.IOException; +import java.nio.charset.MalformedInputException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; @@ -53,19 +56,49 @@ public class AddFeedDialog extends Dialog implements View.OnClickListener { @Override public void onClick(View view) { - if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE) - recyclerView.setVisibility(View.GONE); - progressBar.setVisibility(View.VISIBLE); + if (isValidUrl()) { + if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE) + recyclerView.setVisibility(View.GONE); + progressBar.setVisibility(View.VISIBLE); - parseUrl(textInputEditText.getText().toString()); + parseUrl(); + } } - private void parseUrl(String url) { - Executors.newSingleThreadExecutor().execute(() -> { - List results = HtmlParser.getFeedLink(url); + private boolean isValidUrl() { + String url = textInputEditText.getText().toString().trim(); + + if (url.isEmpty()) { + textInputEditText.setError(getContext().getString(R.string.add_feed_empty_field)); + return false; + } else if (!Patterns.WEB_URL.matcher(url).matches()) { + textInputEditText.setError(getContext().getString(R.string.add_feed_wrong_url)); + return false; + } else + return true; + } + + private void parseUrl() { + String url = textInputEditText.getText().toString().trim(); + + final String finalUrl; + if (!(url.contains(Utils.HTTP_PREFIX) || url.contains(Utils.HTTPS_PREFIX))) + finalUrl = Utils.HTTPS_PREFIX + url; + else + finalUrl = url; + + + Executors.newSingleThreadExecutor().execute(() -> { + try { + List results = HtmlParser.getFeedLink(finalUrl); + + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(() -> displayResults(results)); + } catch (Exception e) { + e.printStackTrace(); + } + - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(() -> displayResults(results)); }); } diff --git a/app/src/main/java/com/readrops/app/Utils.java b/app/src/main/java/com/readrops/app/Utils.java new file mode 100644 index 00000000..63d34bdf --- /dev/null +++ b/app/src/main/java/com/readrops/app/Utils.java @@ -0,0 +1,25 @@ +package com.readrops.app; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.widget.Toast; + +public final class Utils { + + public static final String HTTP_PREFIX = "http://"; + + public static final String HTTPS_PREFIX = "https://"; + + public static void displayErrorInMainThread(Context context, String message) { + Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG); + + if (!(Looper.myLooper() == Looper.getMainLooper())) { + Handler handler = new Handler(Looper.getMainLooper()); + Looper.prepare(); + handler.post(toast::show); + } else + toast.show(); + + } +} diff --git a/app/src/main/res/layout/add_feed_layout.xml b/app/src/main/res/layout/add_feed_layout.xml index 2ce04fd0..d643b808 100644 --- a/app/src/main/res/layout/add_feed_layout.xml +++ b/app/src/main/res/layout/add_feed_layout.xml @@ -15,6 +15,9 @@ android:layout_marginEnd="10dp" android:text="@string/add_feed_title" android:textAlignment="center" + android:textSize="22sp" + android:textColor="#FF3D3A3A" + android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index d1e0af11..b077cdea 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -11,5 +11,7 @@ Ajouter un flux Adresse du flux Valider + Le champ ne peut pas ĂȘtre vide + La valeur n\'est pas une adresse web valide \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7ea603e9..5ab3b283 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,4 +12,6 @@ Add feed Feed url Validate + Field can\'t be empty + Input is not a valid URL diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java index d7ff6d18..d38f939a 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java @@ -20,30 +20,26 @@ public final class HtmlParser { * @param url url to request * @return a list of rss urls with their title */ - public static List getFeedLink(String url) { + public static List getFeedLink(String url) throws Exception { List results = new ArrayList<>(); - try { - Document document = Jsoup.connect(url).get(); - Elements elements = document.select("link"); + Document document = Jsoup.connect(url).get(); - for (Element element : elements) { - String type = element.attributes().get("type"); + Elements elements = document.select("link"); - if (isTypeRssFeed(type)) { - String feedUrl = element.attributes().get("href"); - String label = element.attributes().get("title"); + for (Element element : elements) { + String type = element.attributes().get("type"); - results.add(new ParsingResult(feedUrl, label)); - } + if (isTypeRssFeed(type)) { + String feedUrl = element.attributes().get("href"); + String label = element.attributes().get("title"); + + results.add(new ParsingResult(feedUrl, label)); } - - return results; - } catch (Exception e) { - e.printStackTrace(); } - return null; + return results; + } private static boolean isTypeRssFeed(String type) {