From e3024eda47c0a6de2c69d4552490941fa202a6a0 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Tue, 2 Jan 2018 14:36:56 +0100 Subject: [PATCH] Make replaceKey and removeKey functions rely on id's instead of instances --- .../java/me/impy/aegis/KeyProfileAdapter.java | 17 +++++++---- .../main/java/me/impy/aegis/MainActivity.java | 30 ++++++++----------- .../main/java/me/impy/aegis/db/Database.java | 19 +++++++----- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java index ecd6430d..ce730cd4 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java @@ -36,6 +36,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter 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 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"); diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index baf9e560..af054de0 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -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); } } } diff --git a/app/src/main/java/me/impy/aegis/db/Database.java b/app/src/main/java/me/impy/aegis/db/Database.java index 0b061057..abba70f6 100644 --- a/app/src/main/java/me/impy/aegis/db/Database.java +++ b/app/src/main/java/me/impy/aegis/db/Database.java @@ -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 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"); + } }