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 committed Sep 18, 2024
1 parent 17f106f commit f15cff6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
81 changes: 81 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 @@ -23,9 +23,13 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
Expand Down Expand Up @@ -67,6 +71,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 Down Expand Up @@ -95,6 +101,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
private String _pendingSearchQuery;

private List<VaultEntry> _selectedEntries;
private VaultGroupModel _selectedVaultGroupModel;

private Menu _menu;
private SearchView _searchView;
Expand Down Expand Up @@ -488,6 +495,78 @@ private void startAssignIconsActivity(List<VaultEntry> entries) {
assignIconsResultLauncher.launch(assignIconIntent);
}

private void startAssignGroupsDialog(List<VaultEntry> entries) {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_select_group, null);
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();
ArrayList<VaultGroupModel> groupsArray = new ArrayList<>();
groupsArray.add(new VaultGroupModel(getString(R.string.new_group)));
groupsArray.addAll(groups.stream().map(VaultGroupModel::new).collect(Collectors.toList()));

ArrayAdapter<VaultGroupModel> adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, groupsArray) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(getItem(position).toString());
return view;
}
};

groupsSelection.setAdapter(adapter);
groupsSelection.setOnItemClickListener((parent, view1, position, id) -> {
_selectedVaultGroupModel = (VaultGroupModel) parent.getItemAtPosition(position);
if(_selectedVaultGroupModel.isPlaceholder()) {
newGroupLayout.setVisibility(View.VISIBLE);
newGroupText.requestFocus();
} else {
newGroupLayout.setVisibility(View.GONE);
}
});

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 -> {

if (_selectedVaultGroupModel != null && _selectedVaultGroupModel.isPlaceholder()) {
String newGroupName = newGroupText.getText().toString().trim();
if (!newGroupName.isEmpty()) {
VaultGroup group = new VaultGroup(newGroupName);
_vaultManager.getVault().addGroup(group);

_selectedVaultGroupModel = new VaultGroupModel(group);
} else {
newGroupText.setError(getString(R.string.error_required_field));
return;
}
}

for (VaultEntry selectedEntry : _selectedEntries) {
selectedEntry.addGroup(_selectedVaultGroupModel.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 +1434,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(_selectedEntries);
} 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/set_group"
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 group</string>
<string name="assign_groups_dialog_summary">Select a group that you wish to add to the selected entries.</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 f15cff6

Please sign in to comment.