Skip to content

Commit

Permalink
Improve search feature for better UX
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelschattgen committed Nov 25, 2024
1 parent 3e3df91 commit 503ce87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
_searchView.setOnCloseListener(() -> {
boolean enabled = _submittedSearchQuery != null;
_searchViewBackPressHandler.setEnabled(enabled);
_groupChip.setVisibility(_groups.isEmpty() ? View.GONE : View.VISIBLE);
return false;
});

Expand Down Expand Up @@ -963,6 +964,7 @@ public boolean onQueryTextChange(String s) {
});
_searchView.setOnSearchClickListener(v -> {
String query = _submittedSearchQuery != null ? _submittedSearchQuery : _pendingSearchQuery;
_groupChip.setVisibility(View.GONE);
_searchView.setQuery(query, false);
});

Expand Down Expand Up @@ -1028,6 +1030,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void collapseSearchView() {
_groupChip.setVisibility(_groups.isEmpty() ? View.GONE : View.VISIBLE);
_searchView.setQuery(null, false);
_searchView.setIconified(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.beemdevelopment.aegis.vault.VaultGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -196,6 +197,19 @@ private boolean isEntryFiltered(VaultEntry entry) {
String name = entry.getName().toLowerCase();
String note = entry.getNote().toLowerCase();

if (_searchFilter != null) {
String[] tokens = _searchFilter.toLowerCase().split("\\s+");

// Return true if not all tokens match at least one of the relevant fields
return !Arrays.stream(tokens)
.allMatch(token ->
((_searchBehaviorMask & Preferences.SEARCH_IN_ISSUER) != 0 && issuer.contains(token)) ||
((_searchBehaviorMask & Preferences.SEARCH_IN_NAME) != 0 && name.contains(token)) ||
((_searchBehaviorMask & Preferences.SEARCH_IN_NOTE) != 0 && note.contains(token)) ||
((_searchBehaviorMask & Preferences.SEARCH_IN_GROUPS) != 0 && doesAnyGroupMatchSearchFilter(groups, token))
);
}

if (!_groupFilter.isEmpty()) {
if (groups.isEmpty() && !_groupFilter.contains(null)) {
return true;
Expand All @@ -205,14 +219,7 @@ private boolean isEntryFiltered(VaultEntry entry) {
}
}

if (_searchFilter == null) {
return false;
}

return ((_searchBehaviorMask & Preferences.SEARCH_IN_ISSUER) == 0 || !issuer.contains(_searchFilter))
&& ((_searchBehaviorMask & Preferences.SEARCH_IN_NAME) == 0 || !name.contains(_searchFilter))
&& ((_searchBehaviorMask & Preferences.SEARCH_IN_NOTE) == 0 || !note.contains(_searchFilter))
&& ((_searchBehaviorMask & Preferences.SEARCH_IN_GROUPS) == 0 || !doesAnyGroupMatchSearchFilter(entry.getGroups(), _searchFilter));
return false;
}

private boolean doesAnyGroupMatchSearchFilter(Set<UUID> entryGroupUUIDs, String searchFilter) {
Expand Down

0 comments on commit 503ce87

Please sign in to comment.