Add an option to save the current group filter

This commit is contained in:
Alexander Bakker 2021-05-16 13:04:58 +02:00
parent 51eade900c
commit fc25312d12
6 changed files with 83 additions and 22 deletions

View file

@ -6,8 +6,12 @@ import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -224,4 +228,18 @@ public class Preferences {
public boolean isCopyOnTapEnabled() { public boolean isCopyOnTapEnabled() {
return _prefs.getBoolean("pref_copy_on_tap", false); return _prefs.getBoolean("pref_copy_on_tap", false);
} }
public void setGroupFilter(List<String> groupFilter) {
String filter = TextUtils.join(",", groupFilter);
_prefs.edit().putString("pref_group_filter", filter).apply();
}
public List<String> getGroupFilter() {
String filter = _prefs.getString("pref_group_filter", null);
if (filter == null || filter.isEmpty()) {
return Collections.emptyList();
}
return Arrays.asList(filter.split(","));
}
} }

View file

@ -119,6 +119,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
_entryListView.setSortCategory(getPreferences().getCurrentSortCategory(), false); _entryListView.setSortCategory(getPreferences().getCurrentSortCategory(), false);
_entryListView.setViewMode(getPreferences().getCurrentViewMode()); _entryListView.setViewMode(getPreferences().getCurrentViewMode());
_entryListView.setIsCopyOnTapEnabled(getPreferences().isCopyOnTapEnabled()); _entryListView.setIsCopyOnTapEnabled(getPreferences().isCopyOnTapEnabled());
_entryListView.setPrefGroupFilter(getPreferences().getGroupFilter());
FloatingActionButton fab = findViewById(R.id.fab); FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(v -> { fab.setOnClickListener(v -> {
@ -723,6 +724,11 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
@Override @Override
public void onListChange() { _fabScrollHelper.setVisible(true); } public void onListChange() { _fabScrollHelper.setVisible(true); }
@Override
public void onSaveGroupFilter(List<String> groupFilter) {
getPreferences().setGroupFilter(groupFilter);
}
@Override @Override
public void onLocked(boolean userInitiated) { public void onLocked(boolean userInitiated) {
if (_actionMode != null) { if (_actionMode != null) {

View file

@ -283,10 +283,6 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
_viewMode = viewMode; _viewMode = viewMode;
} }
public void setGroups(TreeSet<String> groups) {
_view.setGroups(groups);
}
@Override @Override
public void onItemDismiss(int position) { public void onItemDismiss(int position) {

View file

@ -67,6 +67,7 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
private LinearLayout _emptyStateView; private LinearLayout _emptyStateView;
private Chip _groupChip; private Chip _groupChip;
private List<String> _groupFilter; private List<String> _groupFilter;
private List<String> _prefGroupFilter;
private UiRefresher _refresher; private UiRefresher _refresher;
@ -130,7 +131,6 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
}); });
_emptyStateView = view.findViewById(R.id.vEmptyList); _emptyStateView = view.findViewById(R.id.vEmptyList);
return view; return view;
} }
@ -269,6 +269,10 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
@Override @Override
public void onListChange() { _listener.onListChange(); } public void onListChange() { _listener.onListChange(); }
public void setPrefGroupFilter(List<String> groupFilter) {
_prefGroupFilter = groupFilter;
}
public void setCodeGroupSize(int codeGrouping) { public void setCodeGroupSize(int codeGrouping) {
_adapter.setCodeGroupSize(codeGrouping); _adapter.setCodeGroupSize(codeGrouping);
} }
@ -375,9 +379,19 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
ChipGroup chipGroup = view.findViewById(R.id.groupChipGroup); ChipGroup chipGroup = view.findViewById(R.id.groupChipGroup);
Button clearButton = view.findViewById(R.id.btnClear); Button clearButton = view.findViewById(R.id.btnClear);
Button saveButton = view.findViewById(R.id.btnSave);
clearButton.setOnClickListener(v -> { clearButton.setOnClickListener(v -> {
chipGroup.clearCheck(); chipGroup.clearCheck();
setGroupFilter(Collections.emptyList(), true); List<String> groupFilter = Collections.emptyList();
_listener.onSaveGroupFilter(groupFilter);
setGroupFilter(groupFilter, true);
dialog.dismiss();
});
saveButton.setOnClickListener(v -> {
List<String> groupFilter = getGroupFilter(chipGroup);
_listener.onSaveGroupFilter(groupFilter);
setGroupFilter(groupFilter, true);
dialog.dismiss(); dialog.dismiss();
}); });
@ -394,9 +408,7 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
chip.setChipBackgroundColorResource(R.color.bg_chip_color); chip.setChipBackgroundColorResource(R.color.bg_chip_color);
chip.setTextColor(colorStateList); chip.setTextColor(colorStateList);
chip.setOnCheckedChangeListener((group1, checkedId) -> { chip.setOnCheckedChangeListener((group1, checkedId) -> {
List<String> groupFilter = chipGroup.getCheckedChipIds().stream() List<String> groupFilter = getGroupFilter(chipGroup);
.map(i -> ((Chip) view.findViewById(i)).getText().toString())
.collect(Collectors.toList());
setGroupFilter(groupFilter, true); setGroupFilter(groupFilter, true);
}); });
@ -407,6 +419,12 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
}); });
} }
private static List<String> getGroupFilter(ChipGroup chipGroup) {
return chipGroup.getCheckedChipIds().stream()
.map(i -> ((Chip) chipGroup.findViewById(i)).getText().toString())
.collect(Collectors.toList());
}
private void updateGroupChip() { private void updateGroupChip() {
if (_groupFilter.isEmpty()) { if (_groupFilter.isEmpty()) {
_groupChip.setText(R.string.groups); _groupChip.setText(R.string.groups);
@ -425,17 +443,26 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
_groupChip.setVisibility(_groups.isEmpty() ? View.GONE : View.VISIBLE); _groupChip.setVisibility(_groups.isEmpty() ? View.GONE : View.VISIBLE);
updateDividerDecoration(); updateDividerDecoration();
if (_groupFilter != null) { if (_prefGroupFilter != null) {
List<String> groupFilter = _groupFilter.stream() List<String> groupFilter = cleanGroupFilter(_prefGroupFilter);
.filter(g -> _groups.contains(g)) _prefGroupFilter = null;
.collect(Collectors.toList()); if (!groupFilter.isEmpty()) {
setGroupFilter(groupFilter, false);
}
} else if (_groupFilter != null) {
List<String> groupFilter = cleanGroupFilter(_groupFilter);
if (!_groupFilter.equals(groupFilter)) { if (!_groupFilter.equals(groupFilter)) {
setGroupFilter(groupFilter, true); setGroupFilter(groupFilter, true);
} }
} }
} }
private List<String> cleanGroupFilter(List<String> groupFilter) {
return groupFilter.stream()
.filter(g -> _groups.contains(g))
.collect(Collectors.toList());
}
private void updateDividerDecoration() { private void updateDividerDecoration() {
if (_dividerDecoration != null) { if (_dividerDecoration != null) {
_recyclerView.removeItemDecoration(_dividerDecoration); _recyclerView.removeItemDecoration(_dividerDecoration);
@ -474,6 +501,7 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
void onSelect(VaultEntry entry); void onSelect(VaultEntry entry);
void onDeselect(VaultEntry entry); void onDeselect(VaultEntry entry);
void onListChange(); void onListChange();
void onSaveGroupFilter(List<String> groupFilter);
} }
private class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration { private class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingTop="12dp" android:paddingTop="12dp"
@ -43,13 +42,26 @@
</com.google.android.material.chip.ChipGroup> </com.google.android.material.chip.ChipGroup>
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorSecondary"
android:text="@string/save"
style="?android:attr/borderlessButtonStyle" />
<Button <Button
android:id="@+id/btnClear" android:id="@+id/btnClear"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="end"
style="?android:attr/borderlessButtonStyle"
android:textColor="@color/colorSecondary" android:textColor="@color/colorSecondary"
android:paddingEnd="16dp" android:text="@string/clear"
android:text="Clear" /> style="?android:attr/borderlessButtonStyle" />
</LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -252,6 +252,7 @@
<string name="group_name_hint">Group name</string> <string name="group_name_hint">Group name</string>
<string name="preference_manage_groups">Edit groups</string> <string name="preference_manage_groups">Edit groups</string>
<string name="preference_manage_groups_summary">Manage and delete your groups here</string> <string name="preference_manage_groups_summary">Manage and delete your groups here</string>
<string name="clear">Clear</string>
<string name="pref_highlight_entry_title">Highlight tokens when tapped</string> <string name="pref_highlight_entry_title">Highlight tokens when tapped</string>
<string name="pref_highlight_entry_summary">Make tokens easier to distinguish from each other by temporarily highlighting them when tapped</string> <string name="pref_highlight_entry_summary">Make tokens easier to distinguish from each other by temporarily highlighting them when tapped</string>