mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-04 12:24:49 +00:00
Decouple GroupManagerActivity from the global application state
This commit is contained in:
parent
ec5be68ff2
commit
b28691c274
4 changed files with 38 additions and 31 deletions
|
@ -162,6 +162,8 @@ public class DatabaseManager {
|
|||
}
|
||||
|
||||
public TreeSet<String> getGroups() {
|
||||
assertState(false, true);
|
||||
|
||||
TreeSet<String> groups = new TreeSet<>(Collator.getInstance());
|
||||
for (DatabaseEntry entry : getEntries()) {
|
||||
String group = entry.getGroup();
|
||||
|
@ -172,17 +174,6 @@ public class DatabaseManager {
|
|||
return groups;
|
||||
}
|
||||
|
||||
public void removeGroup(String groupName) {
|
||||
TreeSet<String> groups = new TreeSet<>(Collator.getInstance());
|
||||
for (DatabaseEntry entry : getEntries()) {
|
||||
String group = entry.getGroup();
|
||||
if (group != null && group.equals(groupName)) {
|
||||
entry.setGroup(null);
|
||||
_db.replaceEntry(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseFileCredentials getCredentials() {
|
||||
assertState(false, true);
|
||||
return _creds;
|
||||
|
|
|
@ -104,7 +104,7 @@ public class EditEntryActivity extends AegisActivity {
|
|||
Intent intent = getIntent();
|
||||
_origEntry = (DatabaseEntry) intent.getSerializableExtra("entry");
|
||||
_isNew = intent.getBooleanExtra("isNew", false);
|
||||
_groups = new TreeSet<String>(Collator.getInstance());
|
||||
_groups = new TreeSet<>(Collator.getInstance());
|
||||
_groups.addAll(intent.getStringArrayListExtra("groups"));
|
||||
if (_isNew) {
|
||||
setTitle(R.string.add_new_profile);
|
||||
|
|
|
@ -1,38 +1,32 @@
|
|||
package me.impy.aegis.ui;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import me.impy.aegis.AegisApplication;
|
||||
import me.impy.aegis.R;
|
||||
import me.impy.aegis.db.DatabaseFileCredentials;
|
||||
import me.impy.aegis.db.DatabaseManager;
|
||||
import me.impy.aegis.db.slots.Slot;
|
||||
import me.impy.aegis.ui.views.GroupAdapter;
|
||||
import me.impy.aegis.ui.views.SlotAdapter;
|
||||
|
||||
public class GroupManagerActivity extends AegisActivity implements GroupAdapter.Listener {
|
||||
private AegisApplication _app;
|
||||
private DatabaseManager _db;
|
||||
private GroupAdapter _adapter;
|
||||
|
||||
TreeSet<String> groups;
|
||||
private TreeSet<String> _groups;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_groups);
|
||||
|
||||
_app = (AegisApplication) getApplication();
|
||||
_db = _app.getDatabaseManager();
|
||||
|
||||
groups = _db.getGroups();
|
||||
Intent intent = getIntent();
|
||||
_groups = new TreeSet<>(Collator.getInstance());
|
||||
_groups.addAll(intent.getStringArrayListExtra("groups"));
|
||||
|
||||
ActionBar bar = getSupportActionBar();
|
||||
bar.setHomeAsUpIndicator(R.drawable.ic_close);
|
||||
|
@ -46,8 +40,7 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
|
|||
slotsView.setAdapter(_adapter);
|
||||
slotsView.setNestedScrollingEnabled(false);
|
||||
|
||||
// load the slots and masterKey
|
||||
for (String group : groups) {
|
||||
for (String group : _groups) {
|
||||
_adapter.addGroup(group);
|
||||
}
|
||||
}
|
||||
|
@ -58,8 +51,7 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
|
|||
.setTitle(R.string.remove_group)
|
||||
.setMessage(R.string.remove_group_description)
|
||||
.setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
|
||||
_db.removeGroup(group);
|
||||
groups.remove(group);
|
||||
_groups.remove(group);
|
||||
_adapter.removeGroup(group);
|
||||
})
|
||||
.setNegativeButton(android.R.string.no, null)
|
||||
|
@ -78,4 +70,12 @@ public class GroupManagerActivity extends AegisActivity implements GroupAdapter.
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("groups", new ArrayList<>(_groups));
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ import com.takisoft.preferencex.PreferenceFragmentCompat;
|
|||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
@ -50,6 +52,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
|
|||
private static final int CODE_IMPORT = 0;
|
||||
private static final int CODE_IMPORT_DECRYPT = 1;
|
||||
private static final int CODE_SLOTS = 2;
|
||||
private static final int CODE_GROUPS = 3;
|
||||
|
||||
// permission request codes
|
||||
private static final int CODE_PERM_IMPORT = 0;
|
||||
|
@ -197,7 +200,6 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
|
|||
return false;
|
||||
});
|
||||
|
||||
|
||||
_slotsPreference = findPreference("pref_slots");
|
||||
_slotsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
|
@ -214,7 +216,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
|
|||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
Intent intent = new Intent(getActivity(), GroupManagerActivity.class);
|
||||
startActivity(intent);
|
||||
intent.putExtra("groups", new ArrayList<>(_db.getGroups()));
|
||||
startActivityForResult(intent, CODE_GROUPS);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
@ -254,6 +257,9 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
|
|||
case CODE_SLOTS:
|
||||
onSlotManagerResult(resultCode, data);
|
||||
break;
|
||||
case CODE_GROUPS:
|
||||
onGroupManagerResult(resultCode, data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -417,6 +423,16 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
|
|||
updateEncryptionPreferences();
|
||||
}
|
||||
|
||||
private void onGroupManagerResult(int resultCode, Intent data) {
|
||||
HashSet<String> groups = new HashSet<>(data.getStringArrayListExtra("groups"));
|
||||
|
||||
for (DatabaseEntry entry : _db.getEntries()) {
|
||||
if (!groups.contains(entry.getGroup())) {
|
||||
entry.setGroup(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean saveDatabase() {
|
||||
try {
|
||||
_db.save();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue