Add limitation for wikivoyage search history size

This commit is contained in:
Alex Sytnyk 2018-04-05 13:09:18 +03:00
parent 9ef3f5144b
commit 9480d571a0

View file

@ -15,6 +15,8 @@ import gnu.trove.map.hash.TLongObjectHashMap;
public class WikivoyageLocalDataHelper {
private static final int HISTORY_ITEMS_LIMIT = 300;
private static WikivoyageLocalDataHelper instance;
private WikivoyageLocalDataDbHelper dbHelper;
@ -70,6 +72,12 @@ public class WikivoyageLocalDataHelper {
} else {
dbHelper.updateHistoryItem(item);
}
if (historyMap.size() > HISTORY_ITEMS_LIMIT) {
List<WikivoyageSearchHistoryItem> allHistory = getAllHistory();
WikivoyageSearchHistoryItem lastItem = allHistory.get(allHistory.size() - 1);
dbHelper.removeHistoryItem(lastItem);
historyMap.remove(lastItem.cityId);
}
}
private static class WikivoyageLocalDataDbHelper {
@ -183,6 +191,18 @@ public class WikivoyageLocalDataHelper {
}
}
void removeHistoryItem(WikivoyageSearchHistoryItem item) {
SQLiteConnection conn = openConnection(false);
if (conn != null) {
try {
conn.execSQL("DELETE FROM " + HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_CITY_ID + " = ?",
new Object[]{item.cityId});
} finally {
conn.close();
}
}
}
private WikivoyageSearchHistoryItem readHistoryItem(SQLiteCursor cursor) {
WikivoyageSearchHistoryItem res = new WikivoyageSearchHistoryItem();