Add option to set maximum number of local items parsed per feed

This commit is contained in:
Shinokuni 2019-09-19 23:47:16 +02:00
parent 7d329bf0fc
commit 8828f1fe18
7 changed files with 103 additions and 42 deletions

View File

@ -29,12 +29,14 @@ import io.reactivex.schedulers.Schedulers;
public abstract class ARepository<T> {
protected Application application;
protected Database database;
protected Account account;
protected T api;
protected ARepository(@NonNull Application application, @Nullable Account account) {
this.application = application;
this.database = Database.getInstance(application);
this.account = account;
}

View File

@ -6,14 +6,15 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.app.database.entities.account.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.database.entities.account.Account;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.FeedMatcher;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ItemMatcher;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.localfeed.AFeed;
import com.readrops.readropslibrary.localfeed.RSSQuery;
@ -43,7 +44,6 @@ public class LocalFeedRepository extends ARepository<Void> {
public LocalFeedRepository(@NonNull Application application, @Nullable Account account) {
super(application, account);
}
@Override
@ -106,40 +106,40 @@ public class LocalFeedRepository extends ARepository<Void> {
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return Single.create(emitter -> {
List<FeedInsertionResult> insertionResults = new ArrayList<>();
return Single.create(emitter -> {
List<FeedInsertionResult> insertionResults = new ArrayList<>();
for (ParsingResult parsingResult : results) {
FeedInsertionResult insertionResult = new FeedInsertionResult();
for (ParsingResult parsingResult : results) {
FeedInsertionResult insertionResult = new FeedInsertionResult();
try {
RSSQuery rssNet = new RSSQuery();
RSSQueryResult queryResult = rssNet.queryUrl(parsingResult.getUrl(), new HashMap<>());
try {
RSSQuery rssNet = new RSSQuery();
RSSQueryResult queryResult = rssNet.queryUrl(parsingResult.getUrl(), new HashMap<>());
if (queryResult != null && queryResult.getException() == null) {
if (queryResult != null && queryResult.getException() == null) {
Feed feed = insertFeed(queryResult.getFeed(), queryResult.getRssType());
if (feed != null) {
insertionResult.setFeed(feed);
insertionResults.add(insertionResult);
}
} else if (queryResult != null && queryResult.getException() != null) {
insertionResult.setParsingResult(parsingResult);
insertionResult.setInsertionError(getErrorFromException(queryResult.getException()));
} else if (queryResult != null && queryResult.getException() != null) {
insertionResult.setParsingResult(parsingResult);
insertionResult.setInsertionError(getErrorFromException(queryResult.getException()));
insertionResults.add(insertionResult);
} else {
// error 304
}
} catch (Exception e) {
if (e instanceof IOException)
insertionResult.setInsertionError(FeedInsertionResult.FeedInsertionError.NETWORK_ERROR);
else
insertionResult.setInsertionError(FeedInsertionResult.FeedInsertionError.PARSE_ERROR);
insertionResults.add(insertionResult);
} else {
// error 304
}
} catch (Exception e) {
if (e instanceof IOException)
insertionResult.setInsertionError(FeedInsertionResult.FeedInsertionError.NETWORK_ERROR);
else
insertionResult.setInsertionError(FeedInsertionResult.FeedInsertionError.PARSE_ERROR);
insertionResult.setParsingResult(parsingResult);
insertionResults.add(insertionResult);
}
}
insertionResult.setParsingResult(parsingResult);
insertionResults.add(insertionResult);
}
}
emitter.onSuccess(insertionResults);
});
@ -165,8 +165,12 @@ public class LocalFeedRepository extends ARepository<Void> {
}
database.feedDao().updateHeaders(dbFeed.getEtag(), dbFeed.getLastModified(), dbFeed.getId());
Collections.sort(items, Item::compareTo);
int maxItems = Integer.parseInt(SharedPreferencesManager.readString(application, SharedPreferencesManager.SharedPrefKey.ITEMS_TO_PARSE_MAX_NB));
if (maxItems > 0 && items.size() > maxItems)
items = items.subList(items.size() - maxItems, items.size());
insertItems(items, dbFeed);
}
@ -194,7 +198,7 @@ public class LocalFeedRepository extends ARepository<Void> {
dbFeed.setEtag(null);
dbFeed.setLastModified(null);
dbFeed.setId((int)(database.feedDao().insert(dbFeed)));
dbFeed.setId((int) (database.feedDao().insert(dbFeed)));
return dbFeed;
}

View File

@ -2,17 +2,16 @@ package com.readrops.app.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
public final class SharedPreferencesManager {
private static final String PREFS = "com.readrops.app.uniquepreferences";
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}
public static void writeValue(Context context, SharedPrefKey key, Object value) {
writeValue(context, key.toString(), value);
return PreferenceManager.getDefaultSharedPreferences(context);
}
public static void writeValue(Context context, String key, Object value) {
@ -27,18 +26,18 @@ public final class SharedPreferencesManager {
editor.apply();
}
public static int readInt(Context context, SharedPrefKey key) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getInt(key.toString(), 0);
public static void writeValue(Context context, SharedPrefKey sharedPrefKey, Object value) {
writeValue(context, sharedPrefKey.key, value);
}
public static boolean readBoolean(Context context, SharedPrefKey key) {
public static int readInt(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getBoolean(key.toString(), false);
return sharedPreferences.getInt(sharedPrefKey.key, sharedPrefKey.getIntDefaultValue());
}
public static String readString(Context context, SharedPrefKey key) {
return readString(context, key.toString());
public static boolean readBoolean(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getBoolean(sharedPrefKey.key, sharedPrefKey.getBooleanDefaultValue());
}
public static String readString(Context context, String key) {
@ -46,7 +45,35 @@ public final class SharedPreferencesManager {
return sharedPreferences.getString(key, null);
}
public static String readString(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getString(sharedPrefKey.key, sharedPrefKey.getStringDefaultValue());
}
public enum SharedPrefKey {
SHOW_READ_ARTICLES
SHOW_READ_ARTICLES("show_read_articles", false),
ITEMS_TO_PARSE_MAX_NB("items_to_parse_max_nb", "20");
@NonNull
private String key;
@NonNull
private Object defaultValue;
public Boolean getBooleanDefaultValue() {
return (Boolean) defaultValue;
}
public String getStringDefaultValue() {
return (String) defaultValue;
}
public int getIntDefaultValue() {
return (int) defaultValue;
}
SharedPrefKey(@NonNull String key, @NonNull Object defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
}
}

View File

@ -75,5 +75,8 @@
<string name="login_failed">La connexion a échoué. Veuillez vérifier vos identifiants</string>
<string name="new_account">Nouveau compte</string>
<string name="app_licence">App distribuée sous la licence GPLv3</string>
<string name="number_items_to_parse">Nombre maximum d\'articles par flux</string>
<string name="unlimited">Illimité</string>
<string name="local">Local</string>
</resources>

View File

@ -1,9 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="filter_items">
<item>@string/filter_newest</item>
<item>@string/filter_oldest</item>
</string-array>
<string-array name="items_per_feed_numbers_values">
<item>20</item>
<item>50</item>
<item>100</item>
<item>0</item>
</string-array>
<string-array name="items_per_feed_numbers">
<item>20</item>
<item>50</item>
<item>100</item>
<item>@string/unlimited</item>
</string-array>
</resources>

View File

@ -82,4 +82,7 @@
<string name="new_account">New account</string>
<string name="app_licence">App released under the GPLv3 licence</string>
<string name="source_url" translatable="false">https://github.com/readrops/Readrops</string>
<string name="number_items_to_parse">Maximum number of items per feed</string>
<string name="unlimited">Unlimited</string>
<string name="local">Local</string>
</resources>

View File

@ -1,4 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/local">
<ListPreference
android:defaultValue="20"
android:entries="@array/items_per_feed_numbers"
android:entryValues="@array/items_per_feed_numbers_values"
android:key="items_to_parse_max_nb"
android:title="@string/number_items_to_parse" />
</PreferenceCategory>
</PreferenceScreen>