Remove unnecessary collection from WikivoyageLocalDataHelper

This commit is contained in:
Alex Sytnyk 2018-04-04 17:48:53 +03:00
parent 1793a40ece
commit 21f2406dcc

View file

@ -19,12 +19,11 @@ public class WikivoyageLocalDataHelper {
private WikivoyageLocalDataDbHelper dbHelper; private WikivoyageLocalDataDbHelper dbHelper;
private TLongObjectHashMap<WikivoyageSearchHistoryItem> historyMap = new TLongObjectHashMap<>(); private TLongObjectHashMap<WikivoyageSearchHistoryItem> historyMap;
private List<WikivoyageSearchHistoryItem> historyItems;
private WikivoyageLocalDataHelper(OsmandApplication app) { private WikivoyageLocalDataHelper(OsmandApplication app) {
dbHelper = new WikivoyageLocalDataDbHelper(app); dbHelper = new WikivoyageLocalDataDbHelper(app);
loadHistory(); historyMap = dbHelper.getAllHistoryMap();
} }
public static WikivoyageLocalDataHelper getInstance(OsmandApplication app) { public static WikivoyageLocalDataHelper getInstance(OsmandApplication app) {
@ -35,7 +34,19 @@ public class WikivoyageLocalDataHelper {
} }
public List<WikivoyageSearchHistoryItem> getAllHistory() { public List<WikivoyageSearchHistoryItem> getAllHistory() {
return new ArrayList<>(historyItems); List<WikivoyageSearchHistoryItem> res = new ArrayList<>(historyMap.valueCollection());
Collections.sort(res, new Comparator<WikivoyageSearchHistoryItem>() {
@Override
public int compare(WikivoyageSearchHistoryItem item1, WikivoyageSearchHistoryItem item2) {
if (item1.lastAccessed > item2.lastAccessed) {
return -1;
} else if (item1.lastAccessed == item2.lastAccessed) {
return 0;
}
return 1;
}
});
return res;
} }
public void addToHistory(WikivoyageArticle article) { public void addToHistory(WikivoyageArticle article) {
@ -55,34 +66,10 @@ public class WikivoyageLocalDataHelper {
item.lastAccessed = System.currentTimeMillis(); item.lastAccessed = System.currentTimeMillis();
if (newItem) { if (newItem) {
dbHelper.addHistoryItem(item); dbHelper.addHistoryItem(item);
historyItems.add(item);
historyMap.put(item.cityId, item); historyMap.put(item.cityId, item);
} else { } else {
dbHelper.updateHistoryItem(item); dbHelper.updateHistoryItem(item);
} }
sortHistory();
}
private void loadHistory() {
historyItems = dbHelper.getAllHistory();
sortHistory();
for (WikivoyageSearchHistoryItem item : historyItems) {
historyMap.put(item.cityId, item);
}
}
private void sortHistory() {
Collections.sort(historyItems, new Comparator<WikivoyageSearchHistoryItem>() {
@Override
public int compare(WikivoyageSearchHistoryItem item1, WikivoyageSearchHistoryItem item2) {
if (item1.lastAccessed > item2.lastAccessed) {
return -1;
} else if (item1.lastAccessed == item2.lastAccessed) {
return 0;
}
return 1;
}
});
} }
private static class WikivoyageLocalDataDbHelper { private static class WikivoyageLocalDataDbHelper {
@ -148,15 +135,16 @@ public class WikivoyageLocalDataHelper {
} }
@NonNull @NonNull
List<WikivoyageSearchHistoryItem> getAllHistory() { TLongObjectHashMap<WikivoyageSearchHistoryItem> getAllHistoryMap() {
List<WikivoyageSearchHistoryItem> res = new ArrayList<>(); TLongObjectHashMap<WikivoyageSearchHistoryItem> res = new TLongObjectHashMap<>();
SQLiteConnection conn = openConnection(true); SQLiteConnection conn = openConnection(true);
if (conn != null) { if (conn != null) {
try { try {
SQLiteCursor cursor = conn.rawQuery(HISTORY_TABLE_SELECT, null); SQLiteCursor cursor = conn.rawQuery(HISTORY_TABLE_SELECT, null);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
res.add(readHistoryItem(cursor)); WikivoyageSearchHistoryItem item = readHistoryItem(cursor);
res.put(item.cityId, item);
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
} finally { } finally {