Skip to content

Commit

Permalink
Merge pull request #1347 from InfiniteCoder06/refractor-groups
Browse files Browse the repository at this point in the history
Renaming of Groups
  • Loading branch information
michaelschattgen committed Apr 20, 2024
2 parents 9b96bbd + a582c20 commit dee881b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
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;
Expand All @@ -25,6 +30,7 @@
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;
Expand All @@ -45,13 +51,24 @@ protected void onCreate(Bundle savedInstanceState) {
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);
Expand All @@ -67,6 +84,11 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

for(VaultGroup group: _renamedGroups) {
_adapter.replaceGroup(group.getUUID(), group);
}


_emptyStateView = findViewById(R.id.vEmptyList);
updateEmptyState();
}
Expand All @@ -78,10 +100,31 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
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)
Expand Down Expand Up @@ -126,12 +169,20 @@ private void saveAndFinish() {

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ public static void showSetPasswordDialog(ComponentActivity activity, PasswordSlo
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());
Expand Down Expand Up @@ -233,32 +236,36 @@ private static void showTextInputDialog(Context context, @StringRes int titleId,
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) {
showTextInputDialog(context, R.string.set_password, R.string.password, listener, true);
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -31,6 +32,13 @@ public void addGroup(VaultGroup group) {
}
}

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);
Expand All @@ -46,18 +54,32 @@ public GroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@

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);
}

public void setData(VaultGroup group) {
_slotName.setText(group.getName());
}

public void setOnEditClickListener(View.OnClickListener listener) {
_buttonEdit.setOnClickListener(listener);
}

public void setOnDeleteClickListener(View.OnClickListener listener) {
_buttonDelete.setOnClickListener(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public void removeGroup(UUID groupUuid) {
removeGroup(group);
}

public void renameGroup(VaultGroup renamedGroup) {
_vault.getGroups().replace(renamedGroup);
}

public void removeGroup(VaultGroup group) {
for (VaultEntry entry : getEntries()) {
entry.removeGroup(group.getUUID());
Expand Down
47 changes: 28 additions & 19 deletions app/src/main/res/layout/card_group.xml
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
<?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"
android:paddingStart="10dp"
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"
android:background="@android:color/darker_gray"
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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit dee881b

Please sign in to comment.