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) {
profile = getKeyByID(profile.getEntry().getID());
int position = _keyProfiles.indexOf(profile);
_keyProfiles.remove(position);
notifyItemRemoved(position);
@ -47,12 +48,16 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
}
public void replaceKey(KeyProfile newProfile) {
for (KeyProfile oldProfile : _keyProfiles) {
if (oldProfile.getEntry().getID() == newProfile.getEntry().getID()) {
int position = _keyProfiles.indexOf(oldProfile);
_keyProfiles.set(position, newProfile);
notifyItemChanged(position);
return;
KeyProfile oldProfile = getKeyByID(newProfile.getEntry().getID());
int position = _keyProfiles.indexOf(oldProfile);
_keyProfiles.set(position, newProfile);
notifyItemChanged(position);
}
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");

View file

@ -358,25 +358,21 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
private void onEditKeyInfoResult(int resultCode, Intent data) {
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");
try {
_db.replaceKey(profile.getEntry());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to update an entry", Toast.LENGTH_SHORT).show();
return;
}
_keyProfileView.replaceKey(profile);
// 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
// TODO: make _db.removeKey and _db.replaceKey -> _db.updateKey work with id's instead of instances
if (data.getBooleanExtra("delete", false)) {
deleteProfile(profile);
} else {
if (!data.getBooleanExtra("delete", false)) {
// 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
try {
_db.replaceKey(profile.getEntry());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to update an entry", Toast.LENGTH_SHORT).show();
return;
}
_keyProfileView.replaceKey(profile);
saveDatabase();
} else {
deleteProfile(profile);
}
}
}

View file

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