Skip to content

Commit

Permalink
Add ability to easily assign groups
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelschattgen authored and alexbakker committed Sep 24, 2024
1 parent 17f106f commit 4a9f189
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
77 changes: 77 additions & 0 deletions app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
Expand All @@ -42,6 +43,7 @@
import com.beemdevelopment.aegis.Preferences;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.SortCategory;
import com.beemdevelopment.aegis.helpers.DropdownHelper;
import com.beemdevelopment.aegis.helpers.FabScrollHelper;
import com.beemdevelopment.aegis.helpers.PermissionHelper;
import com.beemdevelopment.aegis.otp.GoogleAuthInfo;
Expand All @@ -67,6 +69,8 @@
import com.google.android.material.color.MaterialColors;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.common.base.Strings;

import java.util.ArrayList;
Expand All @@ -79,6 +83,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

public class MainActivity extends AegisActivity implements EntryListView.Listener {
Expand Down Expand Up @@ -488,6 +493,76 @@ private void startAssignIconsActivity(List<VaultEntry> entries) {
assignIconsResultLauncher.launch(assignIconIntent);
}

private void startAssignGroupsDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_select_group, null);
TextInputLayout groupSelectionLayout = view.findViewById(R.id.group_selection_layout);
AutoCompleteTextView groupsSelection = view.findViewById(R.id.group_selection_dropdown);
TextInputLayout newGroupLayout = view.findViewById(R.id.text_group_name_layout);
TextInputEditText newGroupText = view.findViewById(R.id.text_group_name);

Collection<VaultGroup> groups = _vaultManager.getVault().getUsedGroups();
List<VaultGroupModel> groupModels = new ArrayList<>();
groupModels.add(new VaultGroupModel(getString(R.string.new_group)));
groupModels.addAll(groups.stream().map(VaultGroupModel::new).collect(Collectors.toList()));
DropdownHelper.fillDropdown(this, groupsSelection, groupModels);

AtomicReference<VaultGroupModel> groupModelRef = new AtomicReference<>();
groupsSelection.setOnItemClickListener((parent, view1, position, id) -> {
VaultGroupModel groupModel = (VaultGroupModel) parent.getItemAtPosition(position);
groupModelRef.set(groupModel);

if (groupModel.isPlaceholder()) {
newGroupLayout.setVisibility(View.VISIBLE);
newGroupText.requestFocus();
} else {
newGroupLayout.setVisibility(View.GONE);
}

groupSelectionLayout.setError(null);
});

AlertDialog dialog = new MaterialAlertDialogBuilder(this)
.setTitle(R.string.assign_groups)
.setView(view)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
.create();

dialog.setOnShowListener(d -> {
Button btnPos = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
btnPos.setOnClickListener(v -> {
VaultGroupModel groupModel = groupModelRef.get();
if (groupModel == null) {
groupSelectionLayout.setError(getString(R.string.error_required_field));
return;
}

if (groupModel.isPlaceholder()) {
String newGroupName = newGroupText.getText().toString().trim();
if (newGroupName.isEmpty()) {
newGroupLayout.setError(getString(R.string.error_required_field));
return;
}

VaultGroup group = new VaultGroup(newGroupName);
_vaultManager.getVault().addGroup(group);
groupModel = new VaultGroupModel(group);
}

for (VaultEntry selectedEntry : _selectedEntries) {
selectedEntry.addGroup(groupModel.getUUID());
}

dialog.dismiss();
saveAndBackupVault();
_actionMode.finish();
setGroups(_vaultManager.getVault().getUsedGroups());
});
});

Dialogs.showSecureDialog(dialog);
}

private void startIntroActivity() {
if (!_isDoingIntro) {
Intent intro = new Intent(this, IntroActivity.class);
Expand Down Expand Up @@ -1355,6 +1430,8 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
} else if (itemId == R.id.action_assign_icons) {
startAssignIconsActivity(_selectedEntries);
mode.finish();
} else if (itemId == R.id.action_assign_groups) {
startAssignGroupsDialog();
} else {
return false;
}
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/res/layout/dialog_select_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:text="@string/assign_groups_dialog_summary" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/group_selection_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:layout_marginTop="15dp"
android:hint="@string/assign_groups_dialog_dropdown"
style="?attr/dropdownStyle">
<AutoCompleteTextView
android:id="@+id/group_selection_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_group_name_layout"
android:hint="@string/group_name_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:layout_marginTop="10dp"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"/>
</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
</ScrollView>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/menu_action_mode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
android:orderInCategory="100"
app:showAsAction="never"/>

<item
android:id="@+id/action_assign_groups"
android:title="@string/assign_groups"
android:orderInCategory="100"
app:showAsAction="never"/>

<item
android:id="@+id/action_share_qr"
android:title="@string/action_transfer"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
<string name="edit">Edit</string>
<string name="select_all">Select all</string>
<string name="assign_icons">Assign icons</string>
<string name="assign_groups">Assign to group</string>
<string name="assign_groups_dialog_summary">Select a group you wish to assign the selected entries to.</string>
<string name="assign_groups_dialog_dropdown">Select group</string>
<string name="favorite" comment="Verb">Favorite</string>
<string name="unfavorite" comment="Verb">Unfavorite</string>
<string name="error_all_caps">ERROR</string>
Expand Down Expand Up @@ -251,6 +254,7 @@
<string name="copied">Copied</string>
<string name="errors_copied">Errors copied to the clipboard</string>
<string name="version_copied">Version copied to the clipboard</string>
<string name="error_required_field">This field is required</string>
<string name="error_occurred">An error occurred</string>
<string name="decryption_error">An error occurred while trying to unlock the vault</string>
<string name="decryption_corrupt_error">An error occurred while trying to unlock the vault. Your vault file might be corrupt.</string>
Expand Down

0 comments on commit 4a9f189

Please sign in to comment.