Renaming of Groups

This commit is contained in:
Praveen Kumar 2024-04-09 12:33:37 +05:30
parent 3c124deae1
commit a582c2053c
No known key found for this signature in database
GPG Key ID: FAAA373053D29923
7 changed files with 130 additions and 30 deletions

View File

@ -13,9 +13,14 @@ import androidx.recyclerview.widget.RecyclerView;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
import com.beemdevelopment.aegis.ui.views.GroupAdapter;
import com.beemdevelopment.aegis.util.Cloner;
import com.beemdevelopment.aegis.vault.VaultEntryException;
import com.beemdevelopment.aegis.vault.VaultGroup;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@ -25,6 +30,7 @@ import java.util.UUID;
public class GroupManagerActivity extends AegisActivity implements GroupAdapter.Listener {
private GroupAdapter _adapter;
private HashSet<UUID> _removedGroups;
private HashSet<VaultGroup> _renamedGroups;
private RecyclerView _groupsView;
private View _emptyStateView;
private BackPressHandler _backPressHandler;
@ -45,13 +51,24 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
getOnBackPressedDispatcher().addCallback(this, _backPressHandler);
_removedGroups = new HashSet<>();
_renamedGroups = new HashSet<>();
if (savedInstanceState != null) {
List<String> groups = savedInstanceState.getStringArrayList("removedGroups");
if (groups != null) {
for (String uuid : groups) {
List<String> removedGroups = savedInstanceState.getStringArrayList("removedGroups");
List<String> renamedGroups = savedInstanceState.getStringArrayList("renamedGroups");
if (removedGroups != null) {
for (String uuid : removedGroups) {
_removedGroups.add(UUID.fromString(uuid));
}
}
if (renamedGroups != null) {
for (String groupObject : renamedGroups) {
try {
_renamedGroups.add(VaultGroup.fromJson(new JSONObject(groupObject)));
} catch (VaultEntryException | JSONException ignored) {
// This is intentionally ignored since the json object is valid
}
}
}
}
_adapter = new GroupAdapter(this);
@ -67,6 +84,11 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
}
}
for(VaultGroup group: _renamedGroups) {
_adapter.replaceGroup(group.getUUID(), group);
}
_emptyStateView = findViewById(R.id.vEmptyList);
updateEmptyState();
}
@ -78,10 +100,31 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
for (UUID uuid : _removedGroups) {
removed.add(uuid.toString());
}
ArrayList<String> renamed = new ArrayList<>();
for (VaultGroup group : _renamedGroups) {
renamed.add(group.toJson().toString());
}
outState.putStringArrayList("renamedGroups", renamed);
outState.putStringArrayList("removedGroups", removed);
}
@Override
public void onEditGroup(VaultGroup group) {
Dialogs.TextInputListener onEditGroup = text -> {
String newGroupName = new String(text).trim();
if (!newGroupName.isEmpty()) {
VaultGroup newGroup = Cloner.clone(group);
newGroup.setName(newGroupName);
_renamedGroups.add(newGroup);
_adapter.replaceGroup(group.getUUID(), newGroup);
_backPressHandler.setEnabled(true);
}
};
Dialogs.showTextInputDialog(GroupManagerActivity.this, R.string.rename_group, R.string.group_name_hint, onEditGroup, group.getName());
}
@Override
public void onRemoveGroup(VaultGroup group) {
Dialogs.showSecureDialog(new MaterialAlertDialogBuilder(this, R.style.ThemeOverlay_Aegis_AlertDialog_Warning)
@ -126,12 +169,20 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
saveAndBackupVault();
}
if (!_renamedGroups.isEmpty()) {
_renamedGroups.removeIf(group -> _removedGroups.contains(group.getUUID()));
for (VaultGroup renamedGroup : _renamedGroups) {
_vaultManager.getVault().renameGroup(renamedGroup);
}
saveAndBackupVault();
}
finish();
}
private void discardAndFinish() {
if (_removedGroups.isEmpty()) {
if (_removedGroups.isEmpty() && _renamedGroups.isEmpty()) {
finish();
return;
}

View File

@ -188,10 +188,13 @@ public class Dialogs {
showSecureDialog(dialog);
}
private static void showTextInputDialog(Context context, @StringRes int titleId, @StringRes int messageId, @StringRes int hintId, TextInputListener listener, DialogInterface.OnCancelListener cancelListener, boolean isSecret) {
private static void showTextInputDialog(Context context, @StringRes int titleId, @StringRes int messageId, @StringRes int hintId, TextInputListener listener, DialogInterface.OnCancelListener cancelListener, boolean isSecret,@Nullable String hint) {
final AtomicReference<Button> buttonOK = new AtomicReference<>();
View view = LayoutInflater.from(context).inflate(R.layout.dialog_text_input, null);
TextInputEditText input = view.findViewById(R.id.text_input);
if(hint != null) {
input.setText(hint);
}
input.addTextChangedListener(new SimpleTextWatcher(text -> {
if (buttonOK.get() != null) {
buttonOK.get().setEnabled(!text.toString().isEmpty());
@ -233,12 +236,16 @@ public class Dialogs {
showSecureDialog(dialog);
}
public static void showTextInputDialog(Context context, @StringRes int titleId, @StringRes int hintId, TextInputListener listener, String text) {
showTextInputDialog(context, titleId, 0, hintId, listener, null, false, text);
}
private static void showTextInputDialog(Context context, @StringRes int titleId, @StringRes int hintId, TextInputListener listener, boolean isSecret) {
showTextInputDialog(context, titleId, 0, hintId, listener, null, isSecret);
showTextInputDialog(context, titleId, 0, hintId, listener, null, isSecret, null);
}
public static void showTextInputDialog(Context context, @StringRes int titleId, @StringRes int hintId, TextInputListener listener, @Nullable DialogInterface.OnCancelListener onCancel) {
showTextInputDialog(context, titleId, 0, hintId, listener, onCancel, false);
showTextInputDialog(context, titleId, 0, hintId, listener, onCancel, false, null);
}
public static void showPasswordInputDialog(Context context, TextInputListener listener) {
@ -246,19 +253,19 @@ public class Dialogs {
}
public static void showPasswordInputDialog(Context context, TextInputListener listener, DialogInterface.OnCancelListener cancelListener) {
showTextInputDialog(context, R.string.set_password, 0, R.string.password, listener, cancelListener, true);
showTextInputDialog(context, R.string.set_password, 0, R.string.password, listener, cancelListener, true, null);
}
public static void showPasswordInputDialog(Context context, @StringRes int messageId, TextInputListener listener) {
showTextInputDialog(context, R.string.set_password, messageId, R.string.password, listener, null, true);
showTextInputDialog(context, R.string.set_password, messageId, R.string.password, listener, null, true, null);
}
public static void showPasswordInputDialog(Context context, @StringRes int messageId, TextInputListener listener, DialogInterface.OnCancelListener cancelListener) {
showTextInputDialog(context, R.string.set_password, messageId, R.string.password, listener, cancelListener, true);
showTextInputDialog(context, R.string.set_password, messageId, R.string.password, listener, cancelListener, true, null);
}
public static void showPasswordInputDialog(Context context, @StringRes int titleId, @StringRes int messageId, TextInputListener listener, DialogInterface.OnCancelListener cancelListener) {
showTextInputDialog(context, titleId, messageId, R.string.password, listener, cancelListener, true);
showTextInputDialog(context, titleId, messageId, R.string.password, listener, cancelListener, true, null);
}
public static void showCheckboxDialog(Context context, @StringRes int titleId, @StringRes int messageId, @StringRes int checkboxMessageId, CheckboxInputListener listener) {

View File

@ -10,6 +10,7 @@ import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.vault.VaultGroup;
import java.util.ArrayList;
import java.util.UUID;
public class GroupAdapter extends RecyclerView.Adapter<GroupHolder> {
private GroupAdapter.Listener _listener;
@ -31,6 +32,13 @@ public class GroupAdapter extends RecyclerView.Adapter<GroupHolder> {
}
}
public void replaceGroup(UUID uuid, VaultGroup newGroup) {
VaultGroup oldGroup = getGroupByUUID(uuid);
int position = _groups.indexOf(oldGroup);
_groups.set(position, newGroup);
notifyItemChanged(position);
}
public void removeGroup(VaultGroup group) {
int position = _groups.indexOf(group);
_groups.remove(position);
@ -46,18 +54,32 @@ public class GroupAdapter extends RecyclerView.Adapter<GroupHolder> {
@Override
public void onBindViewHolder(GroupHolder holder, int position) {
holder.setData(_groups.get(position));
holder.setOnEditClickListener(v -> {
int position12 = holder.getAdapterPosition();
_listener.onEditGroup(_groups.get(position12));
});
holder.setOnDeleteClickListener(v -> {
int position12 = holder.getAdapterPosition();
_listener.onRemoveGroup(_groups.get(position12));
});
}
private VaultGroup getGroupByUUID(UUID uuid) {
for (VaultGroup group : _groups) {
if (group.getUUID().equals(uuid)) {
return group;
}
}
return null;
}
@Override
public int getItemCount() {
return _groups.size();
}
public interface Listener {
void onEditGroup(VaultGroup group);
void onRemoveGroup(VaultGroup group);
}
}

View File

@ -11,11 +11,13 @@ import com.beemdevelopment.aegis.vault.VaultGroup;
public class GroupHolder extends RecyclerView.ViewHolder {
private TextView _slotName;
private ImageView _buttonEdit;
private ImageView _buttonDelete;
public GroupHolder(final View view) {
super(view);
_slotName = view.findViewById(R.id.text_group_name);
_buttonEdit = view.findViewById(R.id.button_edit);
_buttonDelete = view.findViewById(R.id.button_delete);
}
@ -23,6 +25,10 @@ public class GroupHolder extends RecyclerView.ViewHolder {
_slotName.setText(group.getName());
}
public void setOnEditClickListener(View.OnClickListener listener) {
_buttonEdit.setOnClickListener(listener);
}
public void setOnDeleteClickListener(View.OnClickListener listener) {
_buttonDelete.setOnClickListener(listener);
}

View File

@ -287,6 +287,10 @@ public class VaultRepository {
removeGroup(group);
}
public void renameGroup(VaultGroup renamedGroup) {
_vault.getGroups().replace(renamedGroup);
}
public void removeGroup(VaultGroup group) {
for (VaultEntry entry : getEntries()) {
entry.removeGroup(group.getUUID());

View File

@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/button_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:baselineAligned="false"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
@ -16,26 +17,32 @@
android:paddingEnd="10dp"
android:paddingTop="12.5dp"
android:paddingBottom="12.5dp"
android:foreground="?android:attr/selectableItemBackground">
android:background="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="0dp"
<TextView
android:id="@+id/text_group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/text_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/group_name_hint"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary" />
</LinearLayout>
android:text="@string/group_name_hint"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary" />
</LinearLayout>
<ImageView
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:contentDescription="@string/rename_group"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:paddingStart="15dp"
android:paddingTop="12.5dp"
android:paddingEnd="15dp"
android:paddingBottom="12.5dp"
android:src="@drawable/ic_outline_edit_24" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
@ -43,19 +50,21 @@
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:layout_marginTop="12.5dp"
android:layout_marginBottom="12.5dp"/>
android:layout_marginBottom="12.5dp" />
<ImageView
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:contentDescription="@string/remove_group"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:background="?android:attr/selectableItemBackground"
android:paddingStart="15dp"
android:paddingTop="12.5dp"
android:paddingEnd="15dp"
android:paddingBottom="12.5dp"
android:src="@drawable/ic_outline_delete_24" />
android:src="@drawable/ic_outline_delete_24"
app:tint="?attr/colorError" />
</LinearLayout>

View File

@ -286,6 +286,7 @@
<string name="unrelated_google_auth_batches_error">Export contains information for an unrelated batch. Try importing 1 batch at a time.</string>
<string name="no_tokens_can_be_imported">No tokens can be imported as a result</string>
<string name="unlocking_vault">Unlocking the vault</string>
<string name="rename_group">Rename Group</string>
<string name="remove_group">Remove group</string>
<string name="remove_group_description">Are you sure you want to remove this group? Entries in this group will automatically switch to \'No group\'.</string>
<string name="remove_unused_groups">Delete unused groups</string>