Skip to content

Commit 20d7a93

Browse files
joserebeloByteHamster
authored andcommitted
Make history id configurable
1 parent c46a019 commit 20d7a93

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchConfiguration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SearchConfiguration {
2121
private static final String ARGUMENT_INDEX_INDIVIDUAL_PREFERENCES = "individual_prefs";
2222
private static final String ARGUMENT_FUZZY_ENABLED = "fuzzy";
2323
private static final String ARGUMENT_HISTORY_ENABLED = "history_enabled";
24+
private static final String ARGUMENT_HISTORY_ID = "history_id";
2425
private static final String ARGUMENT_SEARCH_BAR_ENABLED = "search_bar_enabled";
2526
private static final String ARGUMENT_BREADCRUMBS_ENABLED = "breadcrumbs_enabled";
2627
private static final String ARGUMENT_REVEAL_ANIMATION_SETTING = "reveal_anim_setting";
@@ -32,6 +33,7 @@ public class SearchConfiguration {
3233
private ArrayList<PreferenceItem> preferencesToIndex = new ArrayList<>();
3334
private ArrayList<String> bannedKeys = new ArrayList<>();
3435
private boolean historyEnabled = true;
36+
private String historyId = null;
3537
private boolean breadcrumbsEnabled = false;
3638
private boolean fuzzySearchEnabled = true;
3739
private boolean searchBarEnabled = true;
@@ -85,6 +87,7 @@ private Bundle toBundle() {
8587
arguments.putString(ARGUMENT_TEXT_HINT, textHint);
8688
arguments.putString(ARGUMENT_TEXT_CLEAR_HISTORY, textClearHistory);
8789
arguments.putString(ARGUMENT_TEXT_NO_RESULTS, textNoResults);
90+
arguments.putString(ARGUMENT_HISTORY_ID, historyId);
8891
return arguments;
8992
}
9093

@@ -100,6 +103,7 @@ static SearchConfiguration fromBundle(Bundle bundle) {
100103
config.textHint = bundle.getString(ARGUMENT_TEXT_HINT);
101104
config.textClearHistory = bundle.getString(ARGUMENT_TEXT_CLEAR_HISTORY);
102105
config.textNoResults = bundle.getString(ARGUMENT_TEXT_NO_RESULTS);
106+
config.historyId = bundle.getString(ARGUMENT_HISTORY_ID);
103107
return config;
104108
}
105109

@@ -122,6 +126,15 @@ public void setHistoryEnabled(boolean historyEnabled) {
122126
this.historyEnabled = historyEnabled;
123127
}
124128

129+
/**
130+
* Sets the id to use for saving the history. Preference screens with the same history id will share the same
131+
* history. The default id is null (no id).
132+
* @param historyId the history id
133+
*/
134+
public void setHistoryId(String historyId) {
135+
this.historyId = historyId;
136+
}
137+
125138
/**
126139
* Allow to enable and disable fuzzy searching. Default is true
127140
* @param fuzzySearchEnabled True if search should be fuzzy
@@ -241,6 +254,10 @@ boolean isHistoryEnabled() {
241254
return historyEnabled;
242255
}
243256

257+
String getHistoryId() {
258+
return historyId;
259+
}
260+
244261
boolean isBreadcrumbsEnabled() {
245262
return breadcrumbsEnabled;
246263
}

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchPreferenceFragment.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,46 @@ private void loadHistory() {
114114
return;
115115
}
116116

117-
int size = prefs.getInt("history_size", 0);
117+
int size = prefs.getInt(historySizeKey(), 0);
118118
for (int i = 0; i < size; i++) {
119-
String title = prefs.getString("history_" + i, null);
119+
String title = prefs.getString(historyEntryKey(i), null);
120120
history.add(new HistoryItem(title));
121121
}
122122
}
123123

124124
private void saveHistory() {
125125
SharedPreferences.Editor editor = prefs.edit();
126-
editor.putInt("history_size", history.size());
126+
editor.putInt(historySizeKey(), history.size());
127127
for (int i = 0; i < history.size(); i++) {
128-
editor.putString("history_" + i, history.get(i).getTerm());
128+
editor.putString(historyEntryKey(i), history.get(i).getTerm());
129129
}
130130
editor.apply();
131131
}
132132

133+
/**
134+
* Gets the preference key for the history size, prefixed with the history ID, if set.
135+
* @return the preference key for the history size
136+
*/
137+
private String historySizeKey() {
138+
if (searchConfiguration.getHistoryId() != null) {
139+
return searchConfiguration.getHistoryId() + "_history_size";
140+
} else {
141+
return "history_size";
142+
}
143+
}
144+
145+
/**
146+
* Gets the preference key for a history entry, prefixed with the history ID, if set.
147+
* @return the preference key for the history entry
148+
*/
149+
private String historyEntryKey(int i) {
150+
if (searchConfiguration.getHistoryId() != null) {
151+
return searchConfiguration.getHistoryId() + "_history_" + i;
152+
} else {
153+
return "history_" + i;
154+
}
155+
}
156+
133157
private void clearHistory() {
134158
viewHolder.searchView.setText("");
135159
history.clear();

0 commit comments

Comments
 (0)