Skip to content

Commit

Permalink
Merge pull request #1498 from michaelschattgen/feature/single-tap-group
Browse files Browse the repository at this point in the history
Change group filter to a single selection
  • Loading branch information
alexbakker authored Sep 24, 2024
2 parents 9ef3315 + 7ea2f5c commit 8e3279b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void changeSort(@IdRes int resId) {

private void changeGroupFilter(String text) {
if (text == null) {
onView(allOf(withText(R.string.all), isDescendantOfA(withId(R.id.groupChipGroup)))).perform(click());
onView(allOf(withText(R.string.no_group), isDescendantOfA(withId(R.id.groupChipGroup)))).perform(click());
} else {
onView(allOf(withText(text), isDescendantOfA(withId(R.id.groupChipGroup)))).perform(click());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.beemdevelopment.aegis;

public enum GroupPlaceholderType {
ALL,
NO_GROUP;

public int getStringRes() {
switch (this) {
case ALL:
return R.string.all;
case NO_GROUP:
return R.string.no_group;
default:
throw new IllegalArgumentException("Unexpected placeholder type: " + this);
}
}
}
62 changes: 26 additions & 36 deletions app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.beemdevelopment.aegis.GroupPlaceholderType;
import com.beemdevelopment.aegis.Preferences;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.SortCategory;
Expand Down Expand Up @@ -271,12 +272,12 @@ public void setGroups(Collection<VaultGroup> groups) {
private void initializeGroups() {
_groupChip.removeAllViews();

addChipTo(_groupChip, new VaultGroupModel(getString(R.string.all)));

for (VaultGroup group : _groups) {
addChipTo(_groupChip, new VaultGroupModel(group));
}

GroupPlaceholderType placeholderType = GroupPlaceholderType.NO_GROUP;
addChipTo(_groupChip, new VaultGroupModel(this, placeholderType));
addSaveChip(_groupChip);
}

Expand All @@ -296,51 +297,40 @@ private void addChipTo(ChipGroup chipGroup, VaultGroupModel group) {
chip.setChecked(_groupFilter != null && _groupFilter.contains(group.getUUID()));

if (group.isPlaceholder()) {
chip.setId(0);
chip.setTag(null);
chip.setChecked(_groupFilter == null);
chip.setOnClickListener(v -> {
setSaveChipVisibility(true);

Chip checkedChip = (Chip) chipGroup.getChildAt(0);
boolean checkedState = checkedChip.isChecked();
chipGroup.clearCheck();

Set<UUID> groupFilter = getGroupFilter(chipGroup);
if (!checkedState) {
groupFilter = new HashSet<>();
groupFilter.add(null);

checkedChip.setChecked(false);
} else {
checkedChip.setChecked(true);
}

_groupFilter = groupFilter;
_entryListView.setGroupFilter(groupFilter, true);
});
GroupPlaceholderType groupPlaceholderType = group.getPlaceholderType();
chip.setTag(groupPlaceholderType);

chipGroup.addView(chip);
return;
if (groupPlaceholderType == GroupPlaceholderType.ALL) {
chip.setChecked(_groupFilter == null);
} else if (groupPlaceholderType == GroupPlaceholderType.NO_GROUP) {
chip.setChecked(_groupFilter != null && _groupFilter.contains(null));
}
} else {
chip.setTag(group);
}


chip.setOnCheckedChangeListener((group1, checkedId) -> {
chip.setOnCheckedChangeListener((group1, isChecked) -> {
Set<UUID> groupFilter = new HashSet<>();
setSaveChipVisibility(true);
Set<UUID> groupFilter = getGroupFilter(chipGroup);

if (groupFilter.isEmpty()) {
if (!isChecked) {
group1.setChecked(false);
_groupFilter = groupFilter;
_entryListView.setGroupFilter(groupFilter, false);
return;
}

Object chipTag = group1.getTag();
if (chipTag == GroupPlaceholderType.NO_GROUP) {
groupFilter.add(null);
} else {
Chip allGroupsChip = (Chip) chipGroup.getChildAt(0);
allGroupsChip.setChecked(false);
groupFilter = getGroupFilter(chipGroup);
}

_groupFilter = groupFilter;
_entryListView.setGroupFilter(groupFilter, true);
_entryListView.setGroupFilter(groupFilter, false);
});

chip.setTag(group);
chipGroup.addView(chip);
}

Expand Down Expand Up @@ -373,7 +363,7 @@ private static Set<UUID> getGroupFilter(ChipGroup chipGroup) {
return chipGroup.getCheckedChipIds().stream()
.map(i -> {
Chip chip = chipGroup.findViewById(i);
if (chip.getTag() != null) {
if (chip.getTag() instanceof VaultGroupModel) {
VaultGroupModel group = (VaultGroupModel) chip.getTag();
return group.getUUID();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.preference.Preference;

import com.beemdevelopment.aegis.BuildConfig;
import com.beemdevelopment.aegis.GroupPlaceholderType;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.helpers.DropdownHelper;
import com.beemdevelopment.aegis.importers.DatabaseImporter;
Expand Down Expand Up @@ -190,7 +191,7 @@ private void startExport() {
checkBoxExportAllGroups.setVisibility(View.VISIBLE);

ArrayList<VaultGroupModel> groupsArray = new ArrayList<>();
groupsArray.add(new VaultGroupModel(getString(R.string.no_group)));
groupsArray.add(new VaultGroupModel(requireContext(), GroupPlaceholderType.NO_GROUP));
groupsArray.addAll(groups.stream().map(VaultGroupModel::new).collect(Collectors.toList()));

groupsSelection.setCheckedItemsCountTextRes(R.plurals.export_groups_selected_count);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
package com.beemdevelopment.aegis.ui.models;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beemdevelopment.aegis.GroupPlaceholderType;
import com.beemdevelopment.aegis.vault.VaultGroup;

import java.io.Serializable;
import java.util.UUID;

public class VaultGroupModel implements Serializable {
private final VaultGroup _group;
private final String _placeholderName;
private final GroupPlaceholderType _placeholderType;
private final String _placeholderText;

public VaultGroupModel(VaultGroup group) {
_group = group;
_placeholderName = null;
_placeholderText = null;
_placeholderType = null;
}

public VaultGroupModel(String placeholderName) {
public VaultGroupModel(Context context, GroupPlaceholderType placeholderType) {
_group = null;
_placeholderName = placeholderName;
_placeholderType = placeholderType;
_placeholderText = context.getString(placeholderType.getStringRes());
}

public VaultGroup getGroup() {
return _group;
}

public String getName() {
return _group != null ? _group.getName() : _placeholderName;
return _group != null ? _group.getName() : _placeholderText;
}

public GroupPlaceholderType getPlaceholderType() {
return _placeholderType;
}

public boolean isPlaceholder() {
return _group == null;
return _placeholderType != null;
}

@Nullable
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
<com.google.android.material.chip.ChipGroup
android:id="@+id/groupChipGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.google.android.material.chip.ChipGroup>
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true"/>
</LinearLayout>
</HorizontalScrollView>
</com.google.android.material.appbar.AppBarLayout>
Expand Down

0 comments on commit 8e3279b

Please sign in to comment.