Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to reorder groups #1481

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
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;
Expand All @@ -30,7 +27,6 @@
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 @@ -51,44 +47,52 @@ protected void onCreate(Bundle savedInstanceState) {
getOnBackPressedDispatcher().addCallback(this, _backPressHandler);

_removedGroups = new HashSet<>();
_renamedGroups = new HashSet<>();
if (savedInstanceState != null) {
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
}
}
}
}

ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(
@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder) {

return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
int draggedItemIndex = viewHolder.getBindingAdapterPosition();
int targetIndex = target.getBindingAdapterPosition();

_adapter.onItemMove(draggedItemIndex, targetIndex);

return true;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { }
});

_adapter = new GroupAdapter(this);
_groupsView = findViewById(R.id.list_groups);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
_groupsView.setLayoutManager(layoutManager);
_groupsView.setAdapter(_adapter);
_groupsView.setNestedScrollingEnabled(false);
touchHelper.attachToRecyclerView(_groupsView);

for (VaultGroup group : _vaultManager.getVault().getGroups()) {
if (!_removedGroups.contains(group.getUUID())) {
_adapter.addGroup(group);
}
}

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


_emptyStateView = findViewById(R.id.vEmptyList);
updateEmptyState();
}
Expand All @@ -100,12 +104,7 @@ 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);
}

Expand All @@ -116,7 +115,6 @@ public void onEditGroup(VaultGroup group) {
if (!newGroupName.isEmpty()) {
VaultGroup newGroup = Cloner.clone(group);
newGroup.setName(newGroupName);
_renamedGroups.add(newGroup);
_adapter.replaceGroup(group.getUUID(), newGroup);
_backPressHandler.setEnabled(true);
}
Expand Down Expand Up @@ -166,23 +164,16 @@ private void saveAndFinish() {
for (UUID uuid : _removedGroups) {
_vaultManager.getVault().removeGroup(uuid);
}

saveAndBackupVault();
}
if (!_renamedGroups.isEmpty()) {
_renamedGroups.removeIf(group -> _removedGroups.contains(group.getUUID()));
for (VaultGroup renamedGroup : _renamedGroups) {
_vaultManager.getVault().renameGroup(renamedGroup);
}

saveAndBackupVault();
}
_vaultManager.getVault().replaceGroups(_adapter.getGroups());
saveAndBackupVault();

finish();
}

private void discardAndFinish() {
if (_removedGroups.isEmpty() && _renamedGroups.isEmpty()) {
if (_removedGroups.isEmpty()) {
finish();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import androidx.recyclerview.widget.RecyclerView;

import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.helpers.ItemTouchHelperAdapter;
import com.beemdevelopment.aegis.util.CollectionUtils;
import com.beemdevelopment.aegis.vault.VaultGroup;

import java.util.ArrayList;
import java.util.UUID;

public class GroupAdapter extends RecyclerView.Adapter<GroupHolder> {
public class GroupAdapter extends RecyclerView.Adapter<GroupHolder> implements ItemTouchHelperAdapter {
private GroupAdapter.Listener _listener;
private ArrayList<VaultGroup> _groups;

Expand All @@ -32,6 +34,10 @@ public void addGroup(VaultGroup group) {
}
}

public ArrayList<VaultGroup> getGroups() {
return _groups;
}

public void replaceGroup(UUID uuid, VaultGroup newGroup) {
VaultGroup oldGroup = getGroupByUUID(uuid);
int position = _groups.indexOf(oldGroup);
Expand Down Expand Up @@ -64,6 +70,18 @@ public void onBindViewHolder(GroupHolder holder, int position) {
});
}

@Override
public void onItemMove(int firstPosition, int secondPosition) {
CollectionUtils.move(_groups, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
}

@Override
public void onItemDismiss(int position) { }

@Override
public void onItemDrop(int position) { }

private VaultGroup getGroupByUUID(UUID uuid) {
for (VaultGroup group : _groups) {
if (group.getUUID().equals(uuid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ public void removeGroup(UUID groupUuid) {
removeGroup(group);
}

public void renameGroup(VaultGroup renamedGroup) {
_vault.getGroups().replace(renamedGroup);
public void replaceGroups(Collection<VaultGroup> groups) {
_vault.getGroups().wipe();
for (VaultGroup group : groups) {
_vault.getGroups().add(group);
}
}

public void removeGroup(VaultGroup group) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/card_group.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:orientation="horizontal">

<LinearLayout
Expand Down