From 8a4bb75be2e165be16d9d8e345d0dca5aca169ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Fri, 24 Apr 2020 23:15:26 +0200 Subject: [PATCH] Expand search history handling. --- src/com/hughes/android/dictionary/C.java | 1 + .../dictionary/DictionaryActivity.java | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/com/hughes/android/dictionary/C.java b/src/com/hughes/android/dictionary/C.java index 904618d..dfa4c8a 100644 --- a/src/com/hughes/android/dictionary/C.java +++ b/src/com/hughes/android/dictionary/C.java @@ -21,6 +21,7 @@ public class C { public static final String DICT_FILE = "dictFile"; public static final String INDEX_SHORT_NAME = "indexShortName"; public static final String SEARCH_TOKEN = "searchToken"; + public static final String SEARCH_HISTORY = "searchHistory"; public static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch"; public static final String SHOW_DOWNLOADABLE = "showLocal"; diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 4e1bf65..9a4e913 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -222,6 +222,7 @@ public class DictionaryActivity extends AppCompatActivity { Log.d(LOG, "onSaveInstanceState: " + searchView.getQuery().toString()); outState.putString(C.INDEX_SHORT_NAME, index.shortName); outState.putString(C.SEARCH_TOKEN, searchView.getQuery().toString()); + outState.putStringArrayList(C.SEARCH_HISTORY, searchHistory); } private int getMatchLen(String search, Index.IndexEntry e) { @@ -254,6 +255,16 @@ public class DictionaryActivity extends AppCompatActivity { finish(); } + private void saveSearchHistory() { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences.Editor ed = prefs.edit(); + for (int i = 0; i < searchHistory.size(); i++) { + ed.putString("history" + i, searchHistory.get(i)); + } + ed.remove("history" + searchHistory.size()); + ed.apply(); + } + private void addToSearchHistory(String text) { if (text == null || text.isEmpty()) return; int exists = searchHistory.indexOf(text); @@ -625,8 +636,22 @@ public class DictionaryActivity extends AppCompatActivity { t.setText(c.getString(1)); } }); + // Set up search history - addToSearchHistory(text); + ArrayList savedHistory = null; + if (savedInstanceState != null) savedHistory = savedInstanceState.getStringArrayList(C.SEARCH_HISTORY); + if (savedHistory != null && !savedHistory.isEmpty()) { + } else { + savedHistory = new ArrayList<>(); + for (int i = 0; i < MAX_SEARCH_HISTORY; i++) { + String h = prefs.getString("history" + i, null); + if (h == null) break; + savedHistory.add(h); + } + } + for (int i = savedHistory.size() - 1; i >= 0; i--) { + addToSearchHistory(savedHistory.get(i)); + } setSearchText(text, true); Log.d(LOG, "Trying to restore searchText=" + text); @@ -754,6 +779,12 @@ public class DictionaryActivity extends AppCompatActivity { prefs.commit(); } + @Override + protected void onPause() { + super.onPause(); + saveSearchHistory(); + } + @Override protected void onDestroy() { super.onDestroy(); @@ -1395,6 +1426,7 @@ public class DictionaryActivity extends AppCompatActivity { if (hideKeyboard) { hideKeyboard(); } + addToSearchHistory(text); } private void setSearchText(final String text, final boolean triggerSearch) { -- 2.43.0