Adding url field verification to add feed dialog (still lacks other http errors)

This commit is contained in:
Shinokuni 2019-01-31 15:04:31 +00:00
parent 5f837d6edb
commit 6656724a53
7 changed files with 86 additions and 25 deletions

View File

@ -11,6 +11,7 @@ import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Patterns;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -18,6 +19,8 @@ import android.widget.ProgressBar;
import com.readrops.readropslibrary.HtmlParser; import com.readrops.readropslibrary.HtmlParser;
import com.readrops.readropslibrary.ParsingResult; import com.readrops.readropslibrary.ParsingResult;
import java.io.IOException;
import java.nio.charset.MalformedInputException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -53,19 +56,49 @@ public class AddFeedDialog extends Dialog implements View.OnClickListener {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE) if (isValidUrl()) {
recyclerView.setVisibility(View.GONE); if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE)
progressBar.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
parseUrl(textInputEditText.getText().toString()); parseUrl();
}
} }
private void parseUrl(String url) { private boolean isValidUrl() {
Executors.newSingleThreadExecutor().execute(() -> { String url = textInputEditText.getText().toString().trim();
List<ParsingResult> results = HtmlParser.getFeedLink(url);
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<ParsingResult> 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));
}); });
} }

View File

@ -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();
}
}

View File

@ -15,6 +15,9 @@
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:text="@string/add_feed_title" android:text="@string/add_feed_title"
android:textAlignment="center" android:textAlignment="center"
android:textSize="22sp"
android:textColor="#FF3D3A3A"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

View File

@ -11,5 +11,7 @@
<string name="add_feed_title">Ajouter un flux</string> <string name="add_feed_title">Ajouter un flux</string>
<string name="add_feed_url">Adresse du flux</string> <string name="add_feed_url">Adresse du flux</string>
<string name="add_feed_validate">Valider</string> <string name="add_feed_validate">Valider</string>
<string name="add_feed_empty_field">Le champ ne peut pas être vide</string>
<string name="add_feed_wrong_url">La valeur n\'est pas une adresse web valide</string>
</resources> </resources>

View File

@ -12,4 +12,6 @@
<string name="add_feed_title">Add feed</string> <string name="add_feed_title">Add feed</string>
<string name="add_feed_url">Feed url</string> <string name="add_feed_url">Feed url</string>
<string name="add_feed_validate">Validate</string> <string name="add_feed_validate">Validate</string>
<string name="add_feed_empty_field">Field can\'t be empty</string>
<string name="add_feed_wrong_url">Input is not a valid URL</string>
</resources> </resources>

View File

@ -20,30 +20,26 @@ public final class HtmlParser {
* @param url url to request * @param url url to request
* @return a list of rss urls with their title * @return a list of rss urls with their title
*/ */
public static List<ParsingResult> getFeedLink(String url) { public static List<ParsingResult> getFeedLink(String url) throws Exception {
List<ParsingResult> results = new ArrayList<>(); List<ParsingResult> 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) { Elements elements = document.select("link");
String type = element.attributes().get("type");
if (isTypeRssFeed(type)) { for (Element element : elements) {
String feedUrl = element.attributes().get("href"); String type = element.attributes().get("type");
String label = element.attributes().get("title");
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) { private static boolean isTypeRssFeed(String type) {