Skip to content

Commit

Permalink
Make history id configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
joserebelo authored and ByteHamster committed Aug 18, 2023
1 parent c46a019 commit 20d7a93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SearchConfiguration {
private static final String ARGUMENT_INDEX_INDIVIDUAL_PREFERENCES = "individual_prefs";
private static final String ARGUMENT_FUZZY_ENABLED = "fuzzy";
private static final String ARGUMENT_HISTORY_ENABLED = "history_enabled";
private static final String ARGUMENT_HISTORY_ID = "history_id";
private static final String ARGUMENT_SEARCH_BAR_ENABLED = "search_bar_enabled";
private static final String ARGUMENT_BREADCRUMBS_ENABLED = "breadcrumbs_enabled";
private static final String ARGUMENT_REVEAL_ANIMATION_SETTING = "reveal_anim_setting";
Expand All @@ -32,6 +33,7 @@ public class SearchConfiguration {
private ArrayList<PreferenceItem> preferencesToIndex = new ArrayList<>();
private ArrayList<String> bannedKeys = new ArrayList<>();
private boolean historyEnabled = true;
private String historyId = null;
private boolean breadcrumbsEnabled = false;
private boolean fuzzySearchEnabled = true;
private boolean searchBarEnabled = true;
Expand Down Expand Up @@ -85,6 +87,7 @@ private Bundle toBundle() {
arguments.putString(ARGUMENT_TEXT_HINT, textHint);
arguments.putString(ARGUMENT_TEXT_CLEAR_HISTORY, textClearHistory);
arguments.putString(ARGUMENT_TEXT_NO_RESULTS, textNoResults);
arguments.putString(ARGUMENT_HISTORY_ID, historyId);
return arguments;
}

Expand All @@ -100,6 +103,7 @@ static SearchConfiguration fromBundle(Bundle bundle) {
config.textHint = bundle.getString(ARGUMENT_TEXT_HINT);
config.textClearHistory = bundle.getString(ARGUMENT_TEXT_CLEAR_HISTORY);
config.textNoResults = bundle.getString(ARGUMENT_TEXT_NO_RESULTS);
config.historyId = bundle.getString(ARGUMENT_HISTORY_ID);
return config;
}

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

/**
* Sets the id to use for saving the history. Preference screens with the same history id will share the same
* history. The default id is null (no id).
* @param historyId the history id
*/
public void setHistoryId(String historyId) {
this.historyId = historyId;
}

/**
* Allow to enable and disable fuzzy searching. Default is true
* @param fuzzySearchEnabled True if search should be fuzzy
Expand Down Expand Up @@ -241,6 +254,10 @@ boolean isHistoryEnabled() {
return historyEnabled;
}

String getHistoryId() {
return historyId;
}

boolean isBreadcrumbsEnabled() {
return breadcrumbsEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,46 @@ private void loadHistory() {
return;
}

int size = prefs.getInt("history_size", 0);
int size = prefs.getInt(historySizeKey(), 0);
for (int i = 0; i < size; i++) {
String title = prefs.getString("history_" + i, null);
String title = prefs.getString(historyEntryKey(i), null);
history.add(new HistoryItem(title));
}
}

private void saveHistory() {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("history_size", history.size());
editor.putInt(historySizeKey(), history.size());
for (int i = 0; i < history.size(); i++) {
editor.putString("history_" + i, history.get(i).getTerm());
editor.putString(historyEntryKey(i), history.get(i).getTerm());
}
editor.apply();
}

/**
* Gets the preference key for the history size, prefixed with the history ID, if set.
* @return the preference key for the history size
*/
private String historySizeKey() {
if (searchConfiguration.getHistoryId() != null) {
return searchConfiguration.getHistoryId() + "_history_size";
} else {
return "history_size";
}
}

/**
* Gets the preference key for a history entry, prefixed with the history ID, if set.
* @return the preference key for the history entry
*/
private String historyEntryKey(int i) {
if (searchConfiguration.getHistoryId() != null) {
return searchConfiguration.getHistoryId() + "_history_" + i;
} else {
return "history_" + i;
}
}

private void clearHistory() {
viewHolder.searchView.setText("");
history.clear();
Expand Down

0 comments on commit 20d7a93

Please sign in to comment.