Make replaceKey and removeKey functions rely on id's instead of instances

This commit is contained in:
Alexander Bakker 2018-01-02 14:36:56 +01:00
parent 7bc4f19cf0
commit e3024eda47
3 changed files with 36 additions and 30 deletions

View file

@ -36,6 +36,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
} }
public void removeKey(KeyProfile profile) { public void removeKey(KeyProfile profile) {
profile = getKeyByID(profile.getEntry().getID());
int position = _keyProfiles.indexOf(profile); int position = _keyProfiles.indexOf(profile);
_keyProfiles.remove(position); _keyProfiles.remove(position);
notifyItemRemoved(position); notifyItemRemoved(position);
@ -47,12 +48,16 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
} }
public void replaceKey(KeyProfile newProfile) { public void replaceKey(KeyProfile newProfile) {
for (KeyProfile oldProfile : _keyProfiles) { KeyProfile oldProfile = getKeyByID(newProfile.getEntry().getID());
if (oldProfile.getEntry().getID() == newProfile.getEntry().getID()) { int position = _keyProfiles.indexOf(oldProfile);
int position = _keyProfiles.indexOf(oldProfile); _keyProfiles.set(position, newProfile);
_keyProfiles.set(position, newProfile); notifyItemChanged(position);
notifyItemChanged(position); }
return;
private KeyProfile getKeyByID(long id) {
for (KeyProfile profile : _keyProfiles) {
if (profile.getEntry().getID() == id) {
return profile;
} }
} }
throw new AssertionError("no key profile found with the same id"); throw new AssertionError("no key profile found with the same id");

View file

@ -358,25 +358,21 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
private void onEditKeyInfoResult(int resultCode, Intent data) { private void onEditKeyInfoResult(int resultCode, Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
// this profile has been serialized/deserialized and is no longer the same instance it once was
// to deal with this, the replaceKey functions are used
KeyProfile profile = (KeyProfile) data.getSerializableExtra("KeyProfile"); KeyProfile profile = (KeyProfile) data.getSerializableExtra("KeyProfile");
try { if (!data.getBooleanExtra("delete", false)) {
_db.replaceKey(profile.getEntry()); // this profile has been serialized/deserialized and is no longer the same instance it once was
} catch (Exception e) { // to deal with this, the replaceKey functions are used
e.printStackTrace(); try {
Toast.makeText(this, "An error occurred while trying to update an entry", Toast.LENGTH_SHORT).show(); _db.replaceKey(profile.getEntry());
return; } catch (Exception e) {
} e.printStackTrace();
_keyProfileView.replaceKey(profile); Toast.makeText(this, "An error occurred while trying to update an entry", Toast.LENGTH_SHORT).show();
return;
// because of what's explained in the comment above, we had to replace the key before we can delete it }
// this is an ugly solution and should be improved at some point _keyProfileView.replaceKey(profile);
// TODO: make _db.removeKey and _db.replaceKey -> _db.updateKey work with id's instead of instances
if (data.getBooleanExtra("delete", false)) {
deleteProfile(profile);
} else {
saveDatabase(); saveDatabase();
} else {
deleteProfile(profile);
} }
} }
} }

View file

@ -66,17 +66,13 @@ public class Database {
} }
public void removeKey(DatabaseEntry entry) { public void removeKey(DatabaseEntry entry) {
entry = getKeyByID(entry.getID());
_entries.remove(entry); _entries.remove(entry);
} }
public void replaceKey(DatabaseEntry newEntry) { public void replaceKey(DatabaseEntry newEntry) {
for (DatabaseEntry oldEntry : _entries) { DatabaseEntry oldEntry = getKeyByID(newEntry.getID());
if (oldEntry.getID() == newEntry.getID()) { _entries.set(_entries.indexOf(oldEntry), newEntry);
_entries.set(_entries.indexOf(oldEntry), newEntry);
return;
}
}
throw new AssertionError("no entry found with the same id");
} }
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) { public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
@ -86,4 +82,13 @@ public class Database {
public List<DatabaseEntry> getKeys() { public List<DatabaseEntry> getKeys() {
return Collections.unmodifiableList(_entries); return Collections.unmodifiableList(_entries);
} }
private DatabaseEntry getKeyByID(long id) {
for (DatabaseEntry entry : _entries) {
if (entry.getID() == id) {
return entry;
}
}
throw new AssertionError("no entry found with the same id");
}
} }