From 75a1b9d9ca54bcb1dadf4f164adeea7894d1ee4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Thu, 21 May 2020 21:59:33 +0200 Subject: [PATCH] Add preference for number of search history entries. --- res/values-de/strings.xml | 3 +++ res/values/strings.xml | 4 ++++ res/xml/preferences.xml | 8 +++++++ .../dictionary/DictionaryActivity.java | 23 ++++++++++++++----- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index 9865cd1..9c49ebe 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -91,4 +91,7 @@ Auswahl des Farbschemas für das Wörterbuch und die Menüs. Ausgewähltes Verzeichnis nicht beschreibbar, versuche eines von + Einträge Sucherverlauf + Wieviele Einträge der Sucherverlauf maximal speichern und anzeigen soll. + diff --git a/res/values/strings.xml b/res/values/strings.xml index e074071..fa89505 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -104,4 +104,8 @@ User-interface color theme. Chosen directory not writable, try one of + historySize + Search history entries + The number of search history entries to save and display. + diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index f9cb70d..2d6f512 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -76,4 +76,12 @@ android:entryValues="@array/themeKeys" /> + + diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 840f2d8..15efdbe 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -151,8 +151,10 @@ public class DictionaryActivity extends AppCompatActivity { }); private SearchOperation currentSearchOperation = null; - private final int MAX_SEARCH_HISTORY = 10; - private final ArrayList searchHistory = new ArrayList<>(MAX_SEARCH_HISTORY); + private final int MAX_SEARCH_HISTORY = 100; + private final int DEFAULT_SEARCH_HISTORY = 10; + private int searchHistoryLimit; + private final ArrayList searchHistory = new ArrayList<>(DEFAULT_SEARCH_HISTORY); private MatrixCursor searchHistoryCursor = new MatrixCursor(new String[] {"_id", "search"}); private TextToSpeech textToSpeech; @@ -260,7 +262,9 @@ public class DictionaryActivity extends AppCompatActivity { for (int i = 0; i < searchHistory.size(); i++) { ed.putString("history" + i, searchHistory.get(i)); } - ed.remove("history" + searchHistory.size()); + for (int i = searchHistory.size(); i <= MAX_SEARCH_HISTORY; i++) { + ed.remove("history" + i); + } ed.apply(); } @@ -269,10 +273,10 @@ public class DictionaryActivity extends AppCompatActivity { } private void addToSearchHistory(String text) { - if (text == null || text.isEmpty()) return; + if (text == null || text.isEmpty() || searchHistoryLimit == 0) return; int exists = searchHistory.indexOf(text); if (exists >= 0) searchHistory.remove(exists); - else if (searchHistory.size() >= MAX_SEARCH_HISTORY) searchHistory.remove(searchHistory.size() - 1); + else if (searchHistory.size() >= searchHistoryLimit) searchHistory.remove(searchHistory.size() - 1); searchHistory.add(0, text); searchHistoryCursor = new MatrixCursor(new String[] {"_id", "search"}); for (int i = 0; i < searchHistory.size(); i++) { @@ -581,6 +585,13 @@ public class DictionaryActivity extends AppCompatActivity { fontSizeSp = 14; } + final String searchHistoryLimitStr = prefs.getString(getString(R.string.historySizeKey), "" + DEFAULT_SEARCH_HISTORY); + try { + searchHistoryLimit = Math.min(Integer.parseInt(searchHistoryLimitStr.trim()), MAX_SEARCH_HISTORY); + } catch (NumberFormatException e) { + searchHistoryLimit = DEFAULT_SEARCH_HISTORY; + } + // ContextMenu. registerForContextMenu(getListView()); @@ -648,7 +659,7 @@ public class DictionaryActivity extends AppCompatActivity { if (savedHistory != null && !savedHistory.isEmpty()) { } else { savedHistory = new ArrayList<>(); - for (int i = 0; i < MAX_SEARCH_HISTORY; i++) { + for (int i = 0; i < searchHistoryLimit; i++) { String h = prefs.getString("history" + i, null); if (h == null) break; savedHistory.add(h); -- 2.43.0