]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Add preference for number of search history entries.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Thu, 21 May 2020 19:59:33 +0000 (21:59 +0200)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Thu, 21 May 2020 19:59:33 +0000 (21:59 +0200)
res/values-de/strings.xml
res/values/strings.xml
res/xml/preferences.xml
src/com/hughes/android/dictionary/DictionaryActivity.java

index 9865cd1830abbf313ddf9ea565b285142aa49c3d..9c49ebe7180bbbcd2bc499c21dedc575f78c1a17 100644 (file)
@@ -91,4 +91,7 @@
     <string name="themeSummary">Auswahl des Farbschemas für das Wörterbuch und die Menüs.</string>
     <string name="chosenNotWritable">Ausgewähltes Verzeichnis nicht beschreibbar, versuche eines von</string>
 
     <string name="themeSummary">Auswahl des Farbschemas für das Wörterbuch und die Menüs.</string>
     <string name="chosenNotWritable">Ausgewähltes Verzeichnis nicht beschreibbar, versuche eines von</string>
 
+    <string name="historySizeTitle">Einträge Sucherverlauf</string>
+    <string name="historySizeSummary">Wieviele Einträge der Sucherverlauf maximal speichern und anzeigen soll.</string>
+
 </resources>
 </resources>
index e0740712b714262a4791c94532de126242e55e67..fa89505f5cece0d5e9cd4c9d9d2438daa5e27818 100644 (file)
     <string name="themeSummary">User-interface color theme.</string>
     <string name="chosenNotWritable">Chosen directory not writable, try one of</string>
 
     <string name="themeSummary">User-interface color theme.</string>
     <string name="chosenNotWritable">Chosen directory not writable, try one of</string>
 
+    <string name="historySizeKey" translatable="false">historySize</string>
+    <string name="historySizeTitle">Search history entries</string>
+    <string name="historySizeSummary">The number of search history entries to save and display.</string>
+
 </resources>
 </resources>
index f9cb70dedb86501892320e86cdcd8b3cd2a88c7d..2d6f51264b89d76c947f48622f8cd25934437888 100644 (file)
     android:entryValues="@array/themeKeys"\r
   />\r
 \r
     android:entryValues="@array/themeKeys"\r
   />\r
 \r
+  <EditTextPreference\r
+    android:key="@string/historySizeKey"\r
+    android:title="@string/historySizeTitle"\r
+    android:summary="@string/historySizeSummary"\r
+    android:defaultValue="10"\r
+    android:persistent="true"\r
+  />\r
+\r
 </PreferenceScreen>  \r
 </PreferenceScreen>  \r
index 840f2d89ba085b0169bac448323a4fe79a3f93e8..15efdbe06c6f65f8d6308979546bfd02bd23dba1 100644 (file)
@@ -151,8 +151,10 @@ public class DictionaryActivity extends AppCompatActivity {
     });
 
     private SearchOperation currentSearchOperation = null;
     });
 
     private SearchOperation currentSearchOperation = null;
-    private final int MAX_SEARCH_HISTORY = 10;
-    private final ArrayList<String> 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<String> searchHistory = new ArrayList<>(DEFAULT_SEARCH_HISTORY);
     private MatrixCursor searchHistoryCursor = new MatrixCursor(new String[] {"_id", "search"});
 
     private TextToSpeech textToSpeech;
     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));
         }
         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();
     }
 
         ed.apply();
     }
 
@@ -269,10 +273,10 @@ public class DictionaryActivity extends AppCompatActivity {
     }
 
     private void addToSearchHistory(String text) {
     }
 
     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);
         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++) {
         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;
         }
 
             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());
 
         // ContextMenu.
         registerForContextMenu(getListView());
 
@@ -648,7 +659,7 @@ public class DictionaryActivity extends AppCompatActivity {
         if (savedHistory != null && !savedHistory.isEmpty()) {
         } else {
             savedHistory = new ArrayList<>();
         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);
                 String h = prefs.getString("history" + i, null);
                 if (h == null) break;
                 savedHistory.add(h);